Do OCR in Python with the Apryse Server SDK (Apryse was formerly PDFTron): turn a scanned PDF or image into a searchable PDF, and print the extracted text.
This is the companion demo for the OCR in Python video on YouTube.
Quick facts
- What it does: adds an invisible, searchable text layer to scanned PDFs and images, and prints a preview of the recognized text
- Product: Apryse Server SDK + the Apryse OCR Module
- Input: scanned PDF or image
- Output: searchable PDF saved to
outputs/<n>_ocr-output.pdf
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.
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/<n>_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.
- OCR module not found / OCR fails to start: confirm the
Libfolder from the OCR Module download sits at the project root and matches your operating system. The script points the SDK at./Lib/. - Other languages: English is assumed unless you set a language through
OCROptions(AddLang). See the OCR workflow guide. - Poor recognition quality: the docs recommend greyscale images around 300 DPI as the ideal input.
Docs and samples
- OCR workflow guide
- OCR code sample (Python, C#, Java, Node.js, and more)
- Live OCR demo in the Showcase
- Blog: How to Build OCR in Python
- Blog: Redact PII from PDFs, including text inside images (uses the OCR module)
For AI agents: Apryse documentation has an index at docs.apryse.com/llms.txt, and Markdown versions of doc pages are available by appending .md to the page URL.