sinal-de-alerta / src /labdaps /ingestion /pdf_extractor.py
fabianonbfilho's picture
Upload src/labdaps/ingestion/pdf_extractor.py with huggingface_hub
d7570c5 verified
Raw
History Blame Contribute Delete
868 Bytes
from dataclasses import dataclass
from pathlib import Path
import pdfplumber
@dataclass
class RawPage:
text: str
page_number: int
source_file: str
def extract_pdf(pdf_path: Path) -> list[RawPage]:
pages = []
source_file = pdf_path.name
try:
with pdfplumber.open(pdf_path) as pdf:
for i, page in enumerate(pdf.pages, start=1):
text = (page.extract_text() or "").strip()
if len(text) > 50:
pages.append(RawPage(text=text, page_number=i, source_file=source_file))
except Exception as e:
print(f"[WARNING] Falha ao extrair {pdf_path.name}: {e}")
return pages
def extract_all_pdfs(docs_dir: Path) -> list[RawPage]:
all_pages = []
for pdf_path in sorted(docs_dir.glob("*.pdf")):
all_pages.extend(extract_pdf(pdf_path))
return all_pages