Skip to content

About

Demo accompanying video on OCR in Python with Apryse

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

Repository files navigation

OCR in Python with Apryse

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

Prerequisites (requirements.txt)

apryse-sdk
python-dotenv

Configure Python Environment

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

Download Apryse OCR Module

  1. Obtain a free trial license key for the Apryse Server SDK.
  2. Copy the .env.example file and update it with your Server key. cp .env.example .env
  3. Download the Apryse OCR Module for your target operating system.
  4. Copy the contents of the Lib folder from the extracted zip archive into a new Lib folder at the root of the project directory.
  5. Add sample scanned PDFs or images to the project directory.

Run the OCR Script

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.

How it works

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 input

See demo.py for the full script, including error handling and text extraction.

Troubleshooting

  • OCR module not found / OCR fails to start: confirm the Lib folder 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.

Related resources

Docs and samples

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.

About

Demo accompanying video on OCR in Python with Apryse

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages