## 功能特性 - 建立统一的项目结构,包含 core/、readers/、utils/、tests/ 模块 - 迁移 lyxy-reader-office 的所有解析器(docx、xlsx、pptx、pdf) - 迁移 lyxy-reader-html 的所有解析器(html、url 下载) - 统一 CLI 入口为 lyxy_document_reader.py - 统一 Markdown 后处理逻辑 - 按文件类型组织 readers,每个解析器独立文件 - 依赖分组按文件类型细分(docx、xlsx、pptx、pdf、html、http) - PDF OCR 解析器优先,无参数控制 - 使用 logging 模块替代简单 print - 设计完整的单元测试结构 - 重写项目文档 ## 新增目录/文件 - core/ - 核心模块(异常体系、Markdown 工具、解析调度器) - readers/ - 格式阅读器(base.py + docx/xlsx/pptx/pdf/html) - utils/ - 工具函数(文件类型检测) - tests/ - 测试(conftest.py + test_core/ + test_readers/ + test_utils/) - lyxy_document_reader.py - 统一 CLI 入口 ## 依赖分组 - docx - DOCX 文档解析支持 - xlsx - XLSX 文档解析支持 - pptx - PPTX 文档解析支持 - pdf - PDF 文档解析支持(含 OCR) - html - HTML/URL 解析支持 - http - HTTP/URL 下载支持 - office - Office 格式组合(docx/xlsx/pptx/pdf) - web - Web 格式组合(html/http) - full - 完整功能 - dev - 开发依赖
119 lines
4.2 KiB
Python
119 lines
4.2 KiB
Python
"""使用 python-docx 库解析 DOCX 文件"""
|
|
|
|
from typing import Any, List, Optional, Tuple
|
|
|
|
from core import build_markdown_table
|
|
|
|
|
|
def parse(file_path: str) -> Tuple[Optional[str], Optional[str]]:
|
|
"""使用 python-docx 库解析 DOCX 文件"""
|
|
try:
|
|
from docx import Document
|
|
except ImportError:
|
|
return None, "python-docx 库未安装"
|
|
|
|
try:
|
|
doc = Document(file_path)
|
|
|
|
_HEADING_LEVELS = {
|
|
"Title": 1, "Heading 1": 1, "Heading 2": 2, "Heading 3": 3,
|
|
"Heading 4": 4, "Heading 5": 5, "Heading 6": 6,
|
|
}
|
|
|
|
def get_heading_level(para: Any) -> int:
|
|
if para.style and para.style.name:
|
|
return _HEADING_LEVELS.get(para.style.name, 0)
|
|
return 0
|
|
|
|
_LIST_STYLES = {
|
|
"Bullet": "bullet", "Number": "number",
|
|
}
|
|
|
|
def get_list_style(para: Any) -> Optional[str]:
|
|
if not para.style or not para.style.name:
|
|
return None
|
|
style_name = para.style.name
|
|
if style_name in _LIST_STYLES:
|
|
return _LIST_STYLES[style_name]
|
|
if style_name.startswith("List Bullet"):
|
|
return "bullet"
|
|
if style_name.startswith("List Number"):
|
|
return "number"
|
|
return None
|
|
|
|
def convert_runs_to_markdown(runs: List[Any]) -> str:
|
|
result = []
|
|
for run in runs:
|
|
text = run.text
|
|
if not text:
|
|
continue
|
|
if run.bold:
|
|
text = f"**{text}**"
|
|
if run.italic:
|
|
text = f"*{text}*"
|
|
if run.underline:
|
|
text = f"<u>{text}</u>"
|
|
result.append(text)
|
|
return "".join(result)
|
|
|
|
def convert_table_to_markdown(table: Any) -> str:
|
|
rows_data = []
|
|
for row in table.rows:
|
|
row_data = []
|
|
for cell in row.cells:
|
|
cell_text = cell.text.strip().replace("\n", " ")
|
|
row_data.append(cell_text)
|
|
rows_data.append(row_data)
|
|
return build_markdown_table(rows_data)
|
|
|
|
markdown_lines = []
|
|
prev_was_list = False
|
|
|
|
from docx.table import Table as DocxTable
|
|
from docx.text.paragraph import Paragraph
|
|
|
|
for element in doc.element.body:
|
|
if element.tag.endswith('}p'):
|
|
para = Paragraph(element, doc)
|
|
text = convert_runs_to_markdown(para.runs)
|
|
if not text.strip():
|
|
continue
|
|
|
|
heading_level = get_heading_level(para)
|
|
if heading_level > 0:
|
|
markdown_lines.append(f"{'#' * heading_level} {text}")
|
|
prev_was_list = False
|
|
else:
|
|
list_style = get_list_style(para)
|
|
if list_style == "bullet":
|
|
if not prev_was_list and markdown_lines:
|
|
markdown_lines.append("")
|
|
markdown_lines.append(f"- {text}")
|
|
prev_was_list = True
|
|
elif list_style == "number":
|
|
if not prev_was_list and markdown_lines:
|
|
markdown_lines.append("")
|
|
markdown_lines.append(f"1. {text}")
|
|
prev_was_list = True
|
|
else:
|
|
if prev_was_list and markdown_lines:
|
|
markdown_lines.append("")
|
|
markdown_lines.append(text)
|
|
markdown_lines.append("")
|
|
prev_was_list = False
|
|
|
|
elif element.tag.endswith('}tbl'):
|
|
table = DocxTable(element, doc)
|
|
table_md = convert_table_to_markdown(table)
|
|
if table_md:
|
|
markdown_lines.append(table_md)
|
|
markdown_lines.append("")
|
|
prev_was_list = False
|
|
|
|
content = "\n".join(markdown_lines)
|
|
if not content.strip():
|
|
return None, "文档为空"
|
|
return content, None
|
|
except Exception as e:
|
|
return None, f"python-docx 解析失败: {str(e)}"
|