Files
PDF-to-DWG-converter/app/ocr_support.py
T
Michael-A-SloanandCursor 91625af67f Add portable PDF to DXF converter for BricsCAD workflows.
Includes vector/vectorize/raster modes, page review before convert, bundled OCR data, and a single-file AppData launcher build.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-30 13:14:57 -04:00

68 lines
1.9 KiB
Python

"""Locate bundled Tesseract language data (no separate Tesseract install)."""
from __future__ import annotations
import os
import sys
from pathlib import Path
def resolve_tessdata_dir() -> Path | None:
"""Return folder containing eng.traineddata, or None."""
candidates: list[Path] = []
if getattr(sys, "frozen", False):
meipass = Path(getattr(sys, "_MEIPASS", Path(sys.executable).parent))
exe_dir = Path(sys.executable).resolve().parent
candidates.extend(
[
meipass / "tessdata",
exe_dir / "_internal" / "tessdata",
exe_dir / "tessdata",
]
)
else:
root = Path(__file__).resolve().parent.parent
candidates.append(root / "vendor" / "tessdata")
# Also honor an explicit env override
env = os.environ.get("TESSDATA_PREFIX")
if env:
candidates.insert(0, Path(env))
for path in candidates:
try:
if (path / "eng.traineddata").is_file():
return path.resolve()
except OSError:
continue
return None
def configure_bundled_ocr() -> Path | None:
"""
Point PyMuPDF / MuPDF OCR at our shipped tessdata.
Call before OCR runs (ideally at process start). Returns the tessdata
path when available.
"""
tessdata = resolve_tessdata_dir()
if tessdata is None:
return None
# MuPDF / PyMuPDF read this when tessdata= is omitted; we also pass it
# explicitly in converter OCR calls.
os.environ["TESSDATA_PREFIX"] = str(tessdata)
try:
import fitz
fitz.TESSDATA_PREFIX = str(tessdata) # type: ignore[attr-defined]
except Exception:
pass
return tessdata
def tessdata_arg() -> str | None:
"""Value for get_textpage_ocr(..., tessdata=...)."""
path = resolve_tessdata_dir()
return str(path) if path else None