DocuPurse / src /pdf_parser.py
Zayeemk's picture
Initial commit: DocuParse AI complete implementation
26b8deb
Raw History Blame Contribute Delete
2.75 kB
"""PDF text extraction using PyMuPDF with OCR fallback."""
from __future__ import annotations
import io
import logging
from typing import Tuple
try:
import fitz # PyMuPDF
FITZ_AVAILABLE = True
except ImportError:
FITZ_AVAILABLE = False
try:
from PIL import Image
PIL_AVAILABLE = True
except ImportError:
PIL_AVAILABLE = False
from .ocr import ocr_image
logger = logging.getLogger(__name__)
MIN_TEXT_LENGTH = 50 # chars
def extract_native_text(pdf_path: str) -> Tuple[str, int]:
"""Extract native text from a PDF. Returns (text, page_count)."""
if not FITZ_AVAILABLE:
raise RuntimeError("PyMuPDF (fitz) is not installed. Run: pip install pymupdf")
doc = fitz.open(pdf_path)
page_count = len(doc)
pages = []
for i, page in enumerate(doc):
text = page.get_text("text")
if text.strip():
pages.append(f"--- Page {i + 1} ---\n{text.strip()}")
doc.close()
return "\n\n".join(pages), page_count
def render_pdf_page_to_image(pdf_path: str, page_index: int, dpi: int = 200) -> "Image.Image":
"""Render a PDF page to a PIL Image for OCR."""
if not FITZ_AVAILABLE:
raise RuntimeError("PyMuPDF is not installed.")
if not PIL_AVAILABLE:
raise RuntimeError("Pillow is not installed.")
doc = fitz.open(pdf_path)
page = doc[page_index]
mat = fitz.Matrix(dpi / 72, dpi / 72)
pix = page.get_pixmap(matrix=mat, colorspace=fitz.csGRAY)
img_data = pix.tobytes("png")
doc.close()
return Image.open(io.BytesIO(img_data))
def extract_text_from_pdf(pdf_path: str) -> Tuple[str, str, int]:
"""
Extract text from a PDF, falling back to OCR if native text is insufficient.
Returns (text, method, page_count).
method: 'native_pdf_text' or 'ocr'
"""
if not FITZ_AVAILABLE:
raise RuntimeError("PyMuPDF is not installed.")
native_text, page_count = extract_native_text(pdf_path)
if len(native_text.strip()) >= MIN_TEXT_LENGTH:
logger.info("Using native PDF text extraction (%d chars)", len(native_text))
return native_text, "native_pdf_text", page_count
logger.info("Native text insufficient (%d chars); falling back to OCR", len(native_text))
doc = fitz.open(pdf_path)
n_pages = len(doc)
doc.close()
ocr_pages = []
for i in range(n_pages):
try:
img = render_pdf_page_to_image(pdf_path, i)
page_text = ocr_image(img)
ocr_pages.append(f"--- Page {i + 1} ---\n{page_text}")
except Exception as e:
logger.warning("OCR failed on page %d: %s", i + 1, e)
ocr_pages.append(f"--- Page {i + 1} ---\n[OCR failed]")
return "\n\n".join(ocr_pages), "ocr", page_count