uofa-demo / src /uofa_cli /readers /pdf_reader.py
cloudronin's picture
push build context (uofa source + packs + space app)
a28ec65 verified
Raw
History Blame Contribute Delete
1.35 kB
"""PDF reader β€” extracts text per page via pdfplumber."""
from __future__ import annotations
from pathlib import Path
from uofa_cli.document_reader import DocumentChunk
def read_pdf(path: Path) -> list[DocumentChunk]:
"""Read a PDF and return one chunk per page with page numbers."""
import pdfplumber
chunks: list[DocumentChunk] = []
with pdfplumber.open(path) as pdf:
if not pdf.pages:
return [DocumentChunk(
text="(empty PDF)",
source_file=path.name,
source_path=str(path),
format="pdf",
)]
has_text = False
for i, page in enumerate(pdf.pages, start=1):
text = page.extract_text() or ""
if text.strip():
has_text = True
chunks.append(DocumentChunk(
text=text,
source_file=path.name,
source_path=str(path),
page_number=i,
format="pdf",
))
if not has_text:
# Image-only PDF warning β€” return empty chunk
return [DocumentChunk(
text="(image-only PDF β€” no extractable text)",
source_file=path.name,
source_path=str(path),
format="pdf",
)]
return chunks