Convert Files with the Document Converter - Python

This tutorial shows how to convert between raster and document file formats in a Python application using the LEADTOOLS Document Converter SDK.

The raster to raster conversion is between JPEG file format and TIFF and for the raster to document conversion is between TIF file format and searchable PDF.

Overview  
Summary This tutorial covers how to use the DocumentConverter class in a Python application.
Completion Time 20 minutes
Visual Studio Project Download tutorial project (2 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 Convert Files with the Document Converter - Python tutorial.

Create the Project and Add the 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 .NET 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 File Conversion Code

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

In the Solution Explorer, open Project-Name.py and place the following references below the "Add references to LEADTOOLS" comment

# Add reference to LEADTOOLS 
from leadtools import LibraryLoader 
LibraryLoader.add_reference("Leadtools") 
from Leadtools import * 
LibraryLoader.add_reference("Leadtools.Codecs") 
from Leadtools.Codecs import * 
LibraryLoader.add_reference("Leadtools.Document.Converter") 
from Leadtools.Document.Converter import * 
LibraryLoader.add_reference("Leadtools.Document.Writer") 
from Leadtools.Document.Writer import * 
LibraryLoader.add_reference("Leadtools.Ocr") 
from Leadtools.Ocr import * 
from System.IO import * 

A valid OCR engine is required for converting raster images to document formats. Add the below code to initialize the IOcrEngine and the DocumentConverter.

def main(): 
     
    Support.set_license("C:/LEADTOOLS22/Support/Common/License") 
 
    directory = r"C:\LEADTOOLS22\Resources\Images" 
    ocr_engine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD) 
    ocr_engine.Startup(None, None, None, r"C:\LEADTOOLS22\Bin\Common\OcrLEADRuntime") 
    doc_converter = DocumentConverter() 
    doc_converter.SetOcrEngineInstance(ocr_engine, False) 
 
    # Change the RasterImageFormat enumeration to whichever is needed 
    convert_to_raster(directory, doc_converter, RasterImageFormat.Tif) 
 
    # Change the DocumentFormat enumeration to whichever is needed 
    convert_to_document(directory, doc_converter, DocumentFormat.Pdf) 

Next add two new methods named convert_to_raster(directory, doc_converter, image_format) and convert_to_document(directory, doc_converter, doc_format). These methods will be called in the main() method, as shown above. Below is the code for the 2 new methods:

Convert Between Raster Images

# Convert files to raster images 
def convert_to_raster(directory, doc_converter, image_format): 
 
    # Pull all JPEG files from directory to convert to TIFF 
    files = Directory.GetFiles(directory, "*.jpg") 
    output_dir = Path.Combine(directory, "Converted") 
 
    if(not Directory.Exists(output_dir)): 
        Directory.CreateDirectory(output_dir) 
 
    for file in files: 
        print(f"Converting {file}...") 
        filename = Path.GetFileNameWithoutExtension(file) 
        ext = RasterCodecs.GetExtension(image_format) 
        out_file = Path.Combine(output_dir, f"{filename}.{ext}") 
 
        job_data = DocumentConverterJobs.CreateJobData(file, out_file, image_format) 
        job_data.JobName = "Convert to image job" 
 
        job = doc_converter.Jobs.CreateJob(job_data) 
        doc_converter.Jobs.RunJob(job) 
 
        if(job.Errors.Count > 0): 
            for error in job.Errors: 
                print(f"Error during conversion: {error.Error.Message}\n") 
        else: 
            print(f"Successfully converted {file} to {out_file}\n") 

Convert Between Raster Images and Searchable PDF Documents

# Convert files to searchable PDF 
def convert_to_document(directory, doc_converter, doc_format): 
 
    # Pull all TIFF files from directory to convert to searchable PDF 
    files = Directory.GetFiles(directory, "*.tif") 
    output_dir = Path.Combine(directory, "Converted") 
 
    if(not Directory.Exists(output_dir)): 
        Directory.CreateDirectory(output_dir) 
 
    for file in files: 
        print(f"Converting {file}...") 
        filename = Path.GetFileNameWithoutExtension(file) 
        ext = DocumentWriter.GetFormatFileExtension(doc_format) 
        out_file = Path.Combine(output_dir, f"{filename}.{ext}") 
 
        job_data = DocumentConverterJobs.CreateJobData(file, out_file, doc_format) 
        job_data.JobName = "Convert to document job" 
 
        job = doc_converter.Jobs.CreateJob(job_data) 
        doc_converter.Jobs.RunJob(job) 
 
        if(job.Errors.Count > 0): 
            for error in job.Errors: 
                print(f"Error during conversion: {error.Error.Message}\n") 
        else: 
            print(f"Successfully converted {file} to {out_file}\n") 

Run the Project

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

If the steps were followed correctly, the application runs and converts all the JPEG files in the Images directory to TIFF, and converts all the TIFF files in the Images directory to searchable PDF. All converted files should output to this folder: <INSTALL_DIR>\LEADTOOLS22\Resources\Images\Converted

Wrap-up

This tutorial showed how to convert files to raster image formats and document formats using the DocumentConverter class.

See Also

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

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