Spaces:
Paused
Paused
Download src/readers/docx.py from CGIAR/PERI-AI-dev: direct link, hf CLI and curl.
- Browser
- Download file 975 Bytes
-
https://huggingface.co/spaces/CGIAR/PERI-AI-dev/resolve/main/src/readers/docx.py
- Command line
-
hf download hf://spaces/CGIAR/PERI-AI-dev/src/readers/docx.py
-
curl -L -o docx.py https://huggingface.co/spaces/CGIAR/PERI-AI-dev/resolve/main/src/readers/docx.py
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 [] | |