-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractor.py
More file actions
57 lines (44 loc) · 1.75 KB
/
Copy pathextractor.py
File metadata and controls
57 lines (44 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""Minimal document extractor."""
from pathlib import Path
from docx import Document
from schemas import DocumentBlock, StructureResult
def extract_document_info(file_path: str) -> StructureResult:
"""Read a .docx file and extract paragraphs and table cells."""
path = Path(file_path)
if not path.exists():
raise FileNotFoundError(f"File not found: {file_path}")
if path.suffix.lower() != ".docx":
raise ValueError("Only .docx files are supported.")
doc = Document(file_path)
blocks: list[DocumentBlock] = []
for index, paragraph in enumerate(doc.paragraphs):
blocks.append(
DocumentBlock(
block_id=str(index),
text=paragraph.text,
role=None,
)
)
for table_index, table in enumerate(doc.tables):
table_id = f"table_{table_index}"
for row_index, row in enumerate(table.rows):
for col_index, cell in enumerate(row.cells):
blocks.append(
DocumentBlock(
block_id=f"t_{table_index}_{row_index}_{col_index}",
text=cell.text,
role="table_cell",
table_id=table_id,
row=row_index,
col=col_index,
)
)
return StructureResult(blocks=blocks)
def print_structure(result: StructureResult) -> None:
"""Print a short preview of extracted blocks."""
print(f"Total blocks: {len(result.blocks)}")
for block in result.blocks[:5]:
preview = block.text.replace("\n", " ").strip()
if len(preview) > 40:
preview = preview[:40] + "..."
print(f"{block.block_id} | {preview}")