Abbyy Finereader Python Page

1. Introduction ABBYY FineReader is a powerful optical character recognition (OCR) software that converts scanned documents, PDFs, and images into editable and searchable formats. While FineReader has a rich GUI, it also provides automation capabilities that can be controlled via Python, enabling batch processing, workflow integration, and custom document handling.

def process_one(img_path): out_name = output_folder / f"img_path.stem_ocr" fine_read_cli(str(img_path), str(out_name), "txt")

# Configure PDF export settings export_params = "PDFExportMode": 1, # 1 = Text and pictures (searchable) "PDFAComplianceMode": 1, # PDF/A-1b "PreserveOriginalPageSize": True

result = fine_read_cli(input_path, "temp", "txt") # Your OCR call with open(cache_file, 'wb') as f: pickle.dump(result, f) abbyy finereader python

def download_result(self, task_id, output_path): """Download OCR result.""" response = self.session.get(f"self.base_url/api/v1/tasks/task_id/result") with open(output_path, 'wb') as f: f.write(response.content) return output_path

Args: input_path: Path to image or PDF output_path: Output file path (without extension) output_format: pdf, docx, xlsx, txt, html """ fine_cmd = r"C:\Program Files (x86)\ABBYY FineReader\FineReaderCmd.exe"

def __del__(self): self.app.Quit() pythoncom.CoUninitialize() fr = FineReaderCOM() text = fr.get_recognized_text("invoice.jpg") print(text[:500]) Zonal OCR example (extract specific invoice fields) zones = [(100, 200, 400, 230), # Invoice number (100, 300, 400, 330), # Date (500, 500, 800, 800)] # Total amount invoice_data = fr.zonal_ocr("invoice.jpg", zones) print(invoice_data) Advanced: PDF Searchable Creation def create_searchable_pdf(input_pdf_path, output_pdf_path): """Convert image-only PDF to searchable PDF/A.""" fr = FineReaderCOM() doc = fr.app.CreateDocument() # Load PDF pages doc.AddImageFile(input_pdf_path, 0) Args: zones: list of (x1, y1, x2, y2)

if result.returncode == 0: print(f"OCR successful: output_path.output_format") else: print(f"Error: result.stderr")

return result import logging from functools import wraps logging.basicConfig(level=logging.INFO) logger = logging.getLogger( name )

with ThreadPoolExecutor(max_workers=max_workers) as executor: list(tqdm(executor.map(process_one, image_files), total=len(image_files))) batch_ocr_cli("./scans", "./ocr_output", max_workers=2) 5. Method 2: COM Automation (Windows, Deep Control) This method gives you programmatic access to FineReader's object model. Initialize FineReader COM Object import win32com.client import pythoncom import os class FineReaderCOM: def init (self): pythoncom.CoInitialize() self.app = win32com.client.Dispatch("FineReader.Application") self.app.Visible = False # Run in background Args: zones: list of (x1

client.wait_and_download("document.pdf", "ocr_result.docx") import re from datetime import datetime from pathlib import Path class InvoiceProcessor: def init (self, fine_reader_com): self.fr = fine_reader_com self.zones = 'invoice_number': (500, 100, 700, 130), 'invoice_date': (500, 140, 650, 165), 'due_date': (500, 170, 650, 195), 'total_amount': (600, 750, 750, 775), 'vendor_name': (100, 100, 400, 130), 'vendor_address': (100, 140, 400, 220)

results = [] for image in Path(input_folder).glob("*.jpg"): print(f"Processing: image.name") # OCR text = fr.get_recognized_text(str(image)) # Save text txt_path = Path(output_folder) / f"image.stem.txt" txt_path.write_text(text, encoding='utf-8') # Save metadata results.append( "file": image.name, "text_length": len(text), "timestamp": datetime.now().isoformat() )

cmd = [ fine_cmd, input_path, f"/out:output_path", f"/fmt:output_format", "/lang:English", # Use multiple: "/lang:English,French,German" "/recognize", "/auto", # Automatic document analysis "/close" ]

def _parse_amount(self, raw): match = re.search(r'\$\s*[\d,]+\.?\d0,2', raw) if match: amount = match.group(0).replace('$', '').replace(',', '') return float(amount) return 0.0

def zonal_ocr(self, input_path, zones, language="English"): """ OCR only specific zones (regions) on the page. Args: zones: list of (x1, y1, x2, y2) tuples in pixels """ doc = self.app.CreateDocument() page = doc.AddImageFile(input_path, 0) # Clear auto-detected regions page.Regions.Clear() # Add custom zones for (x1, y1, x2, y2) in zones: region = page.Regions.Add(x1, y1, x2, y2) region.Type = 1 # 1 = Text region # Recognize only these zones doc.Recognize(language) results = [] for region in page.Regions: results.append(region.Text) doc.Close() return results