PERI-AI-dev / src /readers /docx.py
LIANJie-Jason's picture
Claude Opus 4.6
fix: Round 14 — 28 Tier 1 + Tier 2 bugs fixed across 17 source files
f95cdb7
Raw History Blame Contribute Delete
975 Bytes
"""Reader for Word .docx files."""
def read_docx(file_path: str) -> list[dict]:
try:
from docx import Document
doc = Document(file_path)
paragraphs = [p.text.strip() for p in doc.paragraphs if p.text.strip()]
# Extract embedded tables
for table in doc.tables:
for row in table.rows:
seen_elements = set()
cells = []
for cell in row.cells:
if id(cell._element) not in seen_elements:
seen_elements.add(id(cell._element))
if cell.text.strip():
cells.append(cell.text.strip())
if cells:
paragraphs.append(" | ".join(cells))
if not paragraphs:
return []
return [{"page": 1, "text": "\n\n".join(paragraphs)}]
except Exception as e:
print(f"Warning: Could not read {file_path}: {e}")
return []