1
0

增加参数控制是否使用ocr模式解析pdf

This commit is contained in:
2026-02-17 21:58:27 +08:00
parent c693e23888
commit a21f5063c8
3 changed files with 141 additions and 48 deletions

View File

@@ -46,6 +46,12 @@ python parser.py <file_path> [options]
|--------|--------|------|
| `-n <num>` | `--context <num>` | 每个匹配结果包含的前后非空行数默认2 |
PDF 专用参数:
| 长选项 | 说明 |
|--------|------|
| `--high-res` | 启用 OCR 版面分析(需要额外依赖,处理较慢) |
### 退出码
| 退出码 | 含义 |
@@ -178,23 +184,25 @@ uv run --with docling --with "unstructured[xlsx]" --with markdownify --with "mar
### PDF
优先级Docling → unstructured → MarkItDown → pypdf
默认优先级Docling → unstructured (fast) → MarkItDown → pypdf
`--high-res` 优先级Docling OCR → unstructured OCR (hi_res) → Docling → unstructured (fast) → MarkItDown → pypdf
```bash
# pip - 基础文本提取(使用 fast 策略,无需 OCR
pip install docling "unstructured[pdf]"" markdownify "markitdown[pdf]" pypdf
# pip - 基础文本提取fast 策略,无需 OCR
pip install docling "unstructured[pdf]" markdownify "markitdown[pdf]" pypdf
# pip - OCR 版面分析(使用 hi_res 策略 + PaddleOCR
# pip - OCR 版面分析(--high-res 所需依赖)
pip install docling "unstructured[pdf]" unstructured-paddleocr "paddlepaddle==2.6.2" ml-dtypes markdownify "markitdown[pdf]" pypdf
# uv - 基础文本提取
uv run --with docling --with "unstructured[pdf]" --with markdownify --with "markitdown[pdf]" --with pypdf parser.py report.pdf
# uv - OCR 版面分析
uv run --with docling --with "unstructured[pdf]" --with unstructured-paddleocr --with "paddlepaddle==2.6.2" --with ml-dtypes --with markdownify --with "markitdown[pdf]" --with pypdf parser.py report.pdf
uv run --with docling --with "unstructured[pdf]" --with unstructured-paddleocr --with "paddlepaddle==2.6.2" --with ml-dtypes --with markdownify --with "markitdown[pdf]" --with pypdf parser.py report.pdf --high-res
```
> PDF 无内置 XML 原生解析,至少需要安装 pypdf。unstructured 的 `hi_res` 策略需要额外安装 `unstructured-paddleocr`、`paddlepaddle==2.6.2`、`ml-dtypes`,不可用时自动回退 `fast` 策略。PaddlePaddle 必须锁定 2.x 版本3.x 在 Windows 上有 OneDNN 兼容问题。
> PDF 无内置 XML 原生解析,至少需要安装 pypdf。默认模式下 Docling 不启用 OCRunstructured 使用 fast 策略。指定 `--high-res` 后Docling 启用 OCRunstructured 使用 hi_res 策略配合 PaddleOCR 进行版面分析。hi_res 策略需要额外安装 `unstructured-paddleocr`、`paddlepaddle==2.6.2`、`ml-dtypes`。PaddlePaddle 必须锁定 2.x 版本3.x 在 Windows 上有 OneDNN 兼容问题。
>
### 安装所有依赖
@@ -225,7 +233,7 @@ pip install "markitdown[pdf]" # PDF
pip install "markitdown[docx,pptx,xlsx,pdf]" # 全部
```
**Docling**首次运行会自动下载 OCR/视觉模型到缓存目录,需保持网络连通。
**Docling**DOCX/PPTX/XLSX 使用 SimplePipeline 直接解析 XML 结构,不涉及 OCR。PDF 默认不启用 OCR`do_ocr=False`),指定 `--high-res` 后启用 OCR`do_ocr=True`)。首次运行 OCR 模式会自动下载模型到缓存目录,需保持网络连通。
**unstructured**:需同时安装 `markdownify`。支持按文档类型安装特定 extras 以减少依赖量:
@@ -284,7 +292,7 @@ pip install "markitdown[docx,pptx,xlsx,pdf]" # 全部
**XLSX** — 以 `## SheetName` 区分工作表,数据以 Markdown 表格呈现:
```markdown
# Excel数据转换结果
# Excel数据转换结果 (原生XML解析)
## Sheet1
@@ -309,9 +317,9 @@ pip install "markitdown[docx,pptx,xlsx,pdf]" # 全部
|------|------|
| 图片移除 | 删除 `![alt](url)` 语法 |
| 空行规范化 | 连续多个空行合并为一个 |
| RGB 噪声过滤 | 移除 `R:255 G:128 B:0` 格式的颜色值行 |
| 页码噪声过滤 | 移除 `— 3 —` 格式的页码行 |
| 页眉/页脚过滤 | unstructured 解析器自动跳过 Header/Footer 元素 |
| RGB 噪声过滤 | 移除 `R:255 G:128 B:0` 格式的颜色值行(仅 unstructured 解析器) |
| 页码噪声过滤 | 移除 `— 3 —` 格式的页码行(仅 unstructured 解析器) |
| 页眉/页脚过滤 | 自动跳过 Header/Footer 元素(仅 unstructured 解析器) |
## 错误处理
@@ -384,12 +392,14 @@ $ python parser.py report.docx -s "[invalid"
### PDF
| 解析器 | 优点 | 缺点 | 适用场景 |
|---------|------|--------|---------|
| **Docling** | 内置 OCR结构化 Markdown表格/图片占位 | 模型体积大OCR 耗时长 | 扫描版 PDF多语言 |
| **unstructured** | hi_res 版面分析 + PaddleOCR元素感知自动回退 fast | 需额外 PaddleOCR 依赖 | 版面分析OCR |
| **MarkItDown** | 微软官方;格式规范 | 输出简洁 | 标准格式 |
| **pypdf** | 轻量;速度快;安装简单 | 功能简单 | 快速文本提取 |
| 解析器 | 模式 | 优点 | 缺点 | 适用场景 |
|---------|------|------|--------|---------|
| **Docling** | 默认 | 结构化 Markdown表格/图片占位 | 首次需下载模型 | 有文本层的 PDF |
| **Docling OCR** | `--high-res` | 内置 OCR结构化 Markdown | 模型体积大OCR 耗时长 | 扫描版 PDF多语言 |
| **unstructured** | 默认 | fast 策略;速度快 | 不做版面分析;标题不可靠 | 快速文本提取 |
| **unstructured OCR** | `--high-res` | hi_res 版面分析 + PaddleOCR标题识别 | 需额外 PaddleOCR 依赖 | 版面分析OCR |
| **MarkItDown** | 通用 | 微软官方;格式规范 | 输出简洁 | 标准格式 |
| **pypdf** | 通用 | 轻量;速度快;安装简单 | 功能简单 | 快速文本提取 |
## 常见问题
@@ -399,7 +409,7 @@ $ python parser.py report.docx -s "[invalid"
### PDF 文件没有标题层级?
PDF 是版面描述格式不包含语义化标题结构。Docling 或 unstructured hi_res 策略通过版面分析识别部分标题,准确度取决于排版质量。建议用 `-s` 搜索定位内容,或用 `-c` / `-l` 了解文档规模。
PDF 是版面描述格式,不包含语义化标题结构。使用 `--high-res` 参数可启用 Docling OCR 或 unstructured hi_res 策略通过版面分析识别部分标题,准确度取决于排版质量。默认模式下建议用 `-s` 搜索定位内容,或用 `-c` / `-l` 了解文档规模。
### 表格格式不正确?