Advin Programmer Logo

Universal Programmer,
EPROM Programmer


Abbyy Finereader Python Here

def submit_ocr_task(self, file_path, output_format="pdf"): """Submit a file for OCR processing.""" with open(file_path, 'rb') as f: files = 'file': (Path(file_path).name, f) data = 'outputFormat': output_format, 'language': 'English', 'recognitionAccuracy': 'high', 'documentProcessingMode': 'auto' response = self.session.post( f"self.base_url/api/v1/tasks", files=files, data=data ) return response.json()['taskId']

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"

doc.Recognize("English") doc.Export(output_pdf_path, "PDF", export_params) doc.Close()

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 abbyy finereader python

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

def ocr_with_retry(max_retries=3): def decorator(func): @wraps(func) def wrapper(*args, **kwargs): for attempt in range(max_retries): try: return func(*args, **kwargs) except Exception as e: logger.error(f"Attempt attempt+1 failed: e") if attempt == max_retries - 1: raise time.sleep(2 ** attempt) # Exponential backoff return wrapper return decorator

return result import logging from functools import wraps logging.basicConfig(level=logging.INFO) logger = logging.getLogger( name ) f) data = 'outputFormat': output_format

if cache_file.exists(): with open(cache_file, 'rb') as f: return pickle.load(f)

def _clean_invoice_number(self, raw): match = re.search(r'INV[-_]?\d5,10', raw) return match.group(0) if match else raw

def _parse_date(self, raw): match = re.search(r'\d1,2[/-]\d1,2[/-]\d2,4', raw) if match: return match.group(0) return None export_params) doc.Close() def download_result(self

def get_recognized_text(self, input_path): """Return recognized text as string without saving to file.""" doc = self.app.CreateDocument() doc.AddImageFile(input_path, 0) doc.AnalyzeLayout() doc.Recognize("English") # Extract text from all pages full_text = [] for i in range(doc.Pages.Count): full_text.append(doc.Pages[i].Text) doc.Close() return "\n\n".join(full_text)

result = subprocess.run(cmd, capture_output=True, text=True)

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

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() )