Do OCR in Python with the Apryse Server SDK: turn a scanned PDF or image into a searchable PDF, and print the extracted text.
apryse-sdk
python-dotenv
Create and activate a virtual environment, then install the Apryse SDK from the dedicated index to enable OCR capabilities in Python.
python3 -m venv venv
source venv/bin/activate # On Windows use: venv\Scripts\activate
python -m pip install apryse-sdk --extra-index-url=https://pypi.apryse.com- Obtain a free trial license key for the Apryse Server SDK.
- Copy the .env.example file and update it with your Server key.
cp .env.example .env - Download the Apryse OCR Module for your target operating system.
- Copy the contents of the
Libfolder from the extracted zip archive into a newLibfolder at the root of the project directory. - Add sample scanned PDFs or images to the project directory (supported file types).
Pass the target file (a scanned PDF or an image) as an argument. Scanned PDFs get OCR'd in place via OCRModule.ProcessPDF; images are converted into a new searchable PDF via OCRModule.ImageToPDF.
python demo.py <path_to_scanned_pdf_or_image>The script prints a preview of the recognized text to the console and saves a searchable PDF to outputs/<name>_ocr-output.pdf.
from apryse_sdk import PDFNet, PDFDoc, OCRModule
PDFNet.Initialize(server_key)
PDFNet.AddResourceSearchPath("./Lib/")
doc = PDFDoc(input_path) # for a scanned PDF
OCRModule.ProcessPDF(doc, None) # adds a searchable text layer
OCRModule.ImageToPDF(doc, input_path, None) # alternative: for image inputSee demo.py for the full script, including error handling and text extraction.