1
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from docx import Document
|
||||
|
||||
from backend.app.services.markdown_builder import title_from_path
|
||||
from backend.app.services.processors.base import ProcessedContent
|
||||
|
||||
|
||||
class DocxProcessor:
|
||||
def process(self, path: Path) -> ProcessedContent:
|
||||
doc = Document(path)
|
||||
blocks: list[str] = []
|
||||
for paragraph in doc.paragraphs:
|
||||
text = paragraph.text.strip()
|
||||
if text:
|
||||
blocks.append(text)
|
||||
for table in doc.tables:
|
||||
rows = [[cell.text.strip().replace("\n", " ") for cell in row.cells] for row in table.rows]
|
||||
if rows:
|
||||
blocks.append(self._table_to_markdown(rows))
|
||||
return ProcessedContent(title=title_from_path(path), body="\n\n".join(blocks), processor="docx")
|
||||
|
||||
def _table_to_markdown(self, rows: list[list[str]]) -> str:
|
||||
header = rows[0]
|
||||
lines = [f"| {' | '.join(header)} |", f"| {' | '.join(['---'] * len(header))} |"]
|
||||
for row in rows[1:]:
|
||||
lines.append(f"| {' | '.join(row)} |")
|
||||
return "\n".join(lines)
|
||||
Reference in New Issue
Block a user