1
0

加入pandoc解析docx

This commit is contained in:
2026-02-15 21:54:54 +08:00
parent f167aa2111
commit 4324699a3d
3 changed files with 54 additions and 6 deletions

View File

@@ -6,10 +6,11 @@
该解析器按优先级尝试多种解析方法,确保最大兼容性:
1. **MarkItDown** (微软官方库) - 推荐使用,格式最规范
2. **python-docx / python-pptx / pandas** (成熟的 Python 库) - 输出最详细
3. **unstructured / pypdf** (成熟的 PDF 库) - PDF 专用
4. **XML 原生解析** (备选方案) - 无需依赖
1. **pypandoc-binary** (DOCX 专用,内置 Pandoc) - 生成结构化 Markdown
2. **MarkItDown** (微软官方库) - 推荐使用,格式最规范
3. **python-docx / python-pptx / pandas** (成熟的 Python 库) - 输出最详细
4. **unstructured / pypdf** (成熟的 PDF 库) - PDF 专用
5. **XML 原生解析** (备选方案) - 无需依赖
### 特性
@@ -44,6 +45,16 @@ scripts/
uv run parser.py file.docx
```
### 使用 pypandoc-binaryDOCX
```bash
# 使用 uv 自动安装
uv run --with pypandoc-binary parser.py file.docx
# 或手动安装
pip install pypandoc-binary
```
### 使用 MarkItDown
```bash
@@ -81,7 +92,7 @@ pip install pypdf
```bash
# 安装所有解析库
uv run --with markitdown --with python-docx --with python-pptx --with pandas --with tabulate --with unstructured --with pypdf parser.py file.pdf
uv run --with pypandoc-binary --with markitdown --with python-docx --with python-pptx --with pandas --with tabulate --with unstructured --with pypdf parser.py file.pdf
```
## 命令行用法
@@ -125,6 +136,7 @@ uv run parser.py report.pdf
uv run parser.py report.docx > output.md
# 使用特定依赖
uv run --with pypandoc-binary parser.py report.docx > output.md
uv run --with python-docx parser.py report.docx > output.md
uv run --with pypdf parser.py report.pdf > output.md
```
@@ -195,8 +207,16 @@ uv run --with "markitdown[pdf]" parser.py report.pdf -s "重要内容" -n 2
### DOCX 解析器
DOCX 文件会按以下优先级依次尝试解析:
1. pypandoc-binary
2. MarkItDown
3. python-docx
4. XML 原生
| 解析器 | 优点 | 缺点 | 适用场景 |
|---------|------|--------|---------|
| **pypandoc-binary** | • 自带 Pandoc可直接使用<br>• 输出 Markdown 结构整洁<br>• 错误信息清晰易排查 | • 仅适用于 DOCX<br>• 依赖包体积较大 | • 需要标准化 Markdown 输出<br>• 首选解析路径 |
| **MarkItDown** | • 格式规范<br>• 微软官方支持<br>• 兼容性好 | • 需要安装<br>• 输出较简洁 | • 需要标准格式输出<br>• 自动化文档处理 |
| **python-docx** | • 输出最详细<br>• 保留完整结构<br>• 支持复杂样式 | • 需要安装<br>• 可能包含多余空行 | • 需要精确控制输出<br>• 分析文档结构 |
| **XML 原生** | • 无需依赖<br>• 运行速度快<br>• 输出原始内容 | • 格式可能不统一<br>• 样式处理有限 | • 依赖不可用时<br>• 快速提取内容 |
@@ -533,6 +553,7 @@ A: 大文件建议使用 XML 原生解析(最快),或在脚本外部处理
### 最新版本
- DOCX 解析新增 pypandoc-binary 方案并设置为最高优先级
- 将单体脚本拆分为模块化结构common.py, docx.py, pptx.py, xlsx.py, parser.py
- 添加 XLSX 文件支持
- 添加 PDF 文件支持MarkItDown、unstructured、pypdf

View File

@@ -1,5 +1,5 @@
#!/usr/bin/env python3
"""DOCX 文件解析模块,提供种解析方法。"""
"""DOCX 文件解析模块,提供种解析方法。"""
import xml.etree.ElementTree as ET
import zipfile
@@ -8,6 +8,32 @@ from typing import Any, List, Optional, Tuple
from common import build_markdown_table, parse_with_markitdown, safe_open_zip
def parse_docx_with_pypandoc(file_path: str) -> Tuple[Optional[str], Optional[str]]:
"""使用 pypandoc-binary 库解析 DOCX 文件。"""
try:
import pypandoc
except ImportError:
return None, "pypandoc-binary 库未安装"
try:
content = pypandoc.convert_file(
source_file=file_path,
to="md",
format="docx",
outputfile=None,
extra_args=["--wrap=none"],
)
except OSError as exc:
return None, f"pypandoc-binary 缺少 Pandoc 可执行文件: {exc}"
except RuntimeError as exc:
return None, f"pypandoc-binary 解析失败: {exc}"
content = content.strip()
if not content:
return None, "文档为空"
return content, None
def parse_docx_with_markitdown(file_path: str) -> Tuple[Optional[str], Optional[str]]:
"""使用 MarkItDown 库解析 DOCX 文件"""
return parse_with_markitdown(file_path)

View File

@@ -64,6 +64,7 @@ def main() -> None:
if file_type == "docx":
parsers = [
("pypandoc-binary", docx_parser.parse_docx_with_pypandoc),
("MarkItDown", docx_parser.parse_docx_with_markitdown),
("python-docx", docx_parser.parse_docx_with_python_docx),
("XML 原生解析", docx_parser.parse_docx_with_xml),