Parse Data with the Document Analyzer - Python

This tutorial shows how to retrieve data from an unstructured document in a Python Console application using the LEADTOOLS SDK.

The data is extracted using a ruleset defined in a JSON file paired with the LEADTOOLS Document Analyzer.

Summary This tutorial covers how to retrieve data from an unstructured document utilizing the DocumentAnalyzer in a Python Console application.
Completion Time 20 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform Python Console Application
IDE Visual Studio 2022
Runtime Target Python 3.10 or higher
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Parse Data Using Document Analyzer - Python tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If the project is not available, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project.

This tutorial requires the following DLLs:

For a complete list of which DLL files are required for your application, refer to Files to be Included with your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Add the Code to Parse Unstructured Document Data

With the project created, the references added, and the license set, coding can begin.

In the Solution Explorer, open and place the following references below the "Add references to LEADTOOLS" comment

# Add references to LEADTOOLS 
from leadtools import LibraryLoader 
from Leadtools import * 
from Leadtools.Document import * 
from Leadtools.Document.Analytics import * 
from Leadtools.Document.Unstructured import * 
from Leadtools.Ocr import * 
from Leadtools.Document.Data import * 

Add a new method named analyze_document(ruleset, input_file). Call the new method in the main() method as seen below:

def main(): 
    Support.set_license(os.path.join(DemosTools.get_root(), "C:/LEADTOOLS23/Support/Common/License")) 
    ruleset = r"C:\LEADTOOLS23\Resources\Images\Forms\Unstructured\MedicareCard.json" 
    input_file = r"C:\LEADTOOLS23\Resources\Images\Forms\Unstructured\MedicareCard.png" 
    analyze_document(ruleset, input_file) 

Add the following code to the analyze_document() method to initialize the IOcrEngine, load the unstructured document, and display the extracted data to the console using the DocumentAnalyzer class.

def analyze_document(ruleset, input_file): 
    ocr_engine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD) 
    ocr_engine.Startup(None, None, None, "C:\LEADTOOLS23\Bin\Common\OcrLEADRuntime") 
    document = DocumentFactory.LoadFromFile(input_file, LoadDocumentOptions()) 
    document.Text.OcrEngine = ocr_engine 
    # Create Analyzer 
    analyzer = DocumentAnalyzer() 
    analyzer.Reader = UnstructuredDataReader() 
    analyzer.QueryContext = FileRepositoryContext(ruleset) 
    options = DocumentAnalyzerRunOptions() 
    options.ElementQuery = RepositoryQuery() 
    results = analyzer.Run(document, options) 
    for result in results: 
        for item in result.Items: 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the console appears and the application displays the extracted data from an unstructured document. For the purposes of this tutorial, the below sample files were used for testing.


This tutorial showed how to extract and display information about the unstructured document utilizing the DocumentAnalyzer with a JSON ruleset.

See Also

Help Version 23.0.2024.5.22
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.