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 |
|
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.
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:
Leadtools.dllLeadtools.Codecs.dllLeadtools.Document.Converter.dllLeadtools.Document.Writer.dllLeadtools.Ocr.dllFor a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.
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:
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 LEADTOOLSfrom leadtools import LibraryLoaderLibraryLoader.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:\LEADTOOLS23\Support\Common\License")directory = r"C:\LEADTOOLS23\Resources\Images"ocr_engine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD)ocr_engine.Startup(None, None, None, r"C:\LEADTOOLS23\Bin\Common\OcrLEADRuntime")doc_converter = DocumentConverter()doc_converter.SetOcrEngineInstance(ocr_engine, False)# Change the RasterImageFormat enumeration to whichever is neededconvert_to_raster(directory, doc_converter, RasterImageFormat.Tif)# Change the DocumentFormat enumeration to whichever is neededconvert_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 files to raster imagesdef convert_to_raster(directory, doc_converter, image_format):# Pull all JPEG files from directory to convert to TIFFfiles = 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 files to searchable PDFdef convert_to_document(directory, doc_converter, doc_format):# Pull all TIFF files from directory to convert to searchable PDFfiles = 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 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>\LEADTOOLS23\Resources\Images\Converted
This tutorial showed how to convert files to raster image formats and document formats using the DocumentConverter class.