transcription/src/ingest/extractors/docx_extractor.py
keboss-m 36c9be48be Add document ingestion pipeline, chat analytics modes, and auth fixes
Ingest MD/PDF/DOCX/XLSX into org-scoped documents with classify and RAG indexing. Add compare/timeline chat modes and UI upload. Filter WebSocket progress by user ACL and normalize admin project slugs consistently.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-01 19:16:23 +03:00

33 lines
1.1 KiB
Python

"""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",
)