transcription/src/ingest/extractors/docx_extractor.py

33 lines
1.1 KiB
Python
Raw Normal View History

"""DOCX extractor."""
from pathlib import Path
from docx import Document
from src.ingest.models import DocumentChunk, NormalizedDocument
def extract_docx(path: Path, document_id: str, project: str, doc_type: str) -> NormalizedDocument:
doc = Document(str(path))
parts: list[str] = []
for para in doc.paragraphs:
line = para.text.strip()
if line:
parts.append(line)
for table in doc.tables:
for row in table.rows:
cells = [cell.text.strip() for cell in row.cells if cell.text.strip()]
if cells:
parts.append(" | ".join(cells))
full_text = "\n".join(parts).strip()
return NormalizedDocument(
document_id=document_id,
filename=path.name,
doc_type=doc_type,
project=project,
full_text=full_text,
chunks=[DocumentChunk(text=full_text, source=path.name)] if full_text else [],
metadata={"format": "docx", "paragraphs": len(doc.paragraphs), "tables": len(doc.tables)},
mime_hint="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
)