PDF OCR Tool - Intelligent PDF to Markdown Converter
Uses the Ollama GLM-OCR model to intelligently recognize text, tables, and figures in PDF pages, applying the most appropriate prompts for OCR processing and outputting structured Markdown documents.
Features
- - ✅ Smart Content Detection: Automatically identifies page content type (text/table/figure)
- ✅ Mixed Mode: Splits pages into multiple regions for processing different content types
- ✅ Multiple Processing Modes: Supports text, table, figure, mixed, and auto modes
- ✅ PDF Page-by-Page Processing: Converts PDF to images and processes each page
- ✅ Image OCR: Supports OCR for single images
- ✅ Custom Prompts: Adjustable OCR prompts based on requirements
- ✅ Flexible Configuration: Customizable Ollama host, port, and model
- ✅ uv Package Management: Uses uv for Python dependency management
Installation
1. Prerequisites
CODEBLOCK0
2. Install with uv (Recommended)
CODEBLOCK1
3. Install via ClawHub
CODEBLOCK2
4. Manual Installation
CODEBLOCK3
Usage
Basic Usage
CODEBLOCK4
Advanced Configuration
CODEBLOCK5
Environment Configuration
CODEBLOCK6
Processing Modes
| Mode | Description | Use Case |
|---|
| INLINECODE0 | Auto-detect content type | General use (default) |
| INLINECODE1 |
Pure text recognition | Academic papers, articles, reports |
|
table | Table recognition | Data tables, financial reports |
|
figure | Chart/figure recognition | Statistical charts, flowcharts, diagrams |
|
mixed | Mixed mode | Pages with multiple content types |
Mixed Mode (Granularity)
When using --granularity region:
- - Page is split vertically into multiple regions (default: 3)
- Each region is independently analyzed for content type
- Corresponding prompts are used for OCR
- Final results are combined into complete Markdown
Output Format
PDF Output Example
CODEBLOCK7
Image Output Example
CODEBLOCK8
Prompt Templates
The tool includes four built-in prompt templates in the prompts/ directory:
Text Mode (prompts/text.md)
CODEBLOCK9
Table Mode (prompts/table.md)
CODEBLOCK10
Figure Mode (prompts/figure.md)
CODEBLOCK11
Using in OpenClaw
CODEBLOCK12
Troubleshooting
Model Not Installed
CODEBLOCK13
Service Not Running
CODEBLOCK14
Missing pdftoppm
CODEBLOCK15
Poor OCR Results
- - Try different modes:
--mode text or INLINECODE11 - Use custom prompts: INLINECODE12
- Check image quality (resolution, clarity)
- Try mixed mode: INLINECODE13
Dependency Issues
CODEBLOCK16
Related Resources
Version History
- - v1.2.0 - English prompts, install-deps.sh, fixed .gitignore
- v1.1.0 - Added mixed mode, region splitting, pyproject.toml
- v1.0.0 - Initial version with basic OCR functionality
Credits
This tool is developed and maintained by the OpenClaw community.
License
MIT License
PDF OCR 工具 - 智能PDF转Markdown转换器
使用Ollama GLM-OCR模型智能识别PDF页面中的文本、表格和图形,应用最合适的提示词进行OCR处理,并输出结构化的Markdown文档。
功能特性
- - ✅ 智能内容检测:自动识别页面内容类型(文本/表格/图形)
- ✅ 混合模式:将页面分割成多个区域,处理不同类型的内容
- ✅ 多种处理模式:支持文本、表格、图形、混合和自动模式
- ✅ PDF逐页处理:将PDF转换为图像并逐页处理
- ✅ 图像OCR:支持单张图像的OCR识别
- ✅ 自定义提示词:可根据需求调整OCR提示词
- ✅ 灵活配置:可自定义Ollama主机、端口和模型
- ✅ uv包管理:使用uv管理Python依赖
安装
1. 前置条件
bash
安装Ollama
curl -fsSL https://ollama.com/install.sh | sh
ollama pull glm-ocr:q8_0
安装poppler-utils(用于PDF转图像)
sudo apt install poppler-utils # Debian/Ubuntu
brew install poppler # macOS
安装uv包管理器
curl -LsSf https://astral.sh/uv/install.sh | sh
2. 使用uv安装(推荐)
bash
cd skills/pdf-ocr-tool
uv venv
source .venv/bin/activate
uv add requests Pillow
3. 通过ClawHub安装
bash
npx clawhub install pdf-ocr-tool
4. 手动安装
bash
克隆或下载技能
git clone
~/.openclaw/workspace/skills/pdf-ocr-tool
创建虚拟环境并安装依赖
cd ~/.openclaw/workspace/skills/pdf-ocr-tool
uv venv
source .venv/bin/activate
uv add requests Pillow
运行安装后脚本
bash hooks/post-install.sh
使用方法
基本用法
bash
自动检测内容类型(推荐)
python ocr_tool.py --input document.pdf --output result.md
指定处理模式
python ocr_tool.py --input document.pdf --output result.md --mode text
python ocr_tool.py --input document.pdf --output result.md --mode table
python ocr_tool.py --input document.pdf --output result.md --mode figure
混合模式:将页面分割成区域
python ocr_tool.py --input document.pdf --output result.md --granularity region
处理单张图像
python ocr_tool.py --input image.png --output result.md --mode mixed
高级配置
bash
指定Ollama主机和端口
python ocr_tool.py --input document.pdf --output result.md \
--host localhost --port 11434
使用不同模型
python ocr_tool.py --input document.pdf --output result.md \
--model glm-ocr:q8_0
自定义提示词
python ocr_tool.py --input image.png --output result.md \
--prompt 将此表格转换为Markdown格式,保持行列对齐
保存图形区域图像
python ocr_tool.py --input document.pdf --output result.md --save-images
环境配置
bash
设置默认配置
export OLLAMA_HOST=localhost
export OLLAMA_PORT=11434
export OCRMODEL=glm-ocr:q80
运行
python ocr_tool.py --input document.pdf --output result.md
处理模式
| 模式 | 描述 | 使用场景 |
|---|
| auto | 自动检测内容类型 | 通用(默认) |
| text |
纯文本识别 | 学术论文、文章、报告 |
| table | 表格识别 | 数据表格、财务报表 |
| figure | 图表/图形识别 | 统计图表、流程图、示意图 |
| mixed | 混合模式 | 包含多种内容类型的页面 |
混合模式(粒度)
使用 --granularity region 时:
- - 页面垂直分割成多个区域(默认:3个)
- 每个区域独立分析内容类型
- 使用相应的提示词进行OCR处理
- 最终结果合并为完整的Markdown
输出格式
PDF输出示例
markdown
PDF转Markdown结果
总页数: 15
模型: glm-ocr:q8_0
模式: auto
生成时间: 2026-02-27T01:00:00+08:00
第1页
类型: mixed
区域1(文本)
[OCR识别的文本内容]
区域2(表格)
区域3(图形)
[图表描述]
图像输出示例
markdown
image.png OCR结果
模型: glm-ocr:q8_0
模式: table
[OCR识别结果]
提示词模板
工具在 prompts/ 目录中包含四个内置提示词模板:
文本模式(prompts/text.md)
将此区域中的文本转换为Markdown格式。
- - 保留段落结构和标题层级
- 正确处理列表
- 保留数学公式
- 保持引用和参考文献格式
表格模式(prompts/table.md)
将此区域中的表格转换为Markdown表格格式。
- - 保持行列对齐
- 保留所有数据和数值
- 处理合并单元格
- 保留表头和单位
图形模式(prompts/figure.md)
分析此区域中的图表或图像:
- 1. 图表类型(柱状图、折线图、饼图、流程图等)
- 标题和坐标轴标签
- 数据趋势和关键观察
- 重要数值和异常点
以Markdown格式描述。
在OpenClaw中使用
python
import subprocess
from pathlib import Path
处理PDF(自动模式)
subprocess.run([
python, skills/pdf-ocr-tool/ocr_tool.py,
--input, /path/to/document.pdf,
--output, /tmp/result.md,
--mode, auto
])
读取结果
with open(/tmp/result.md, r) as f:
markdown_content = f.read()
处理单张图像(表格模式)
subprocess.run([
python, skills/pdf-ocr-tool/ocr_tool.py,
--input, /path/to/table.png,
--output, /tmp/table.md,
--mode, table
])
复杂PDF的混合模式
subprocess.run([
python, skills/pdf-ocr-tool/ocr_tool.py,
--input, /path/to/mixed.pdf,
--output, /tmp/mixed.md,
--granularity, region, # 分割成区域
--save-images # 保存图形图像
])
故障排除
模型未安装
bash
ollama pull glm-ocr:q8_0
服务未运行
bash
ollama serve
缺少pdftoppm
bash
sudo apt install poppler-utils # Debian/Ubuntu
brew install poppler # macOS
OCR效果不佳
- - 尝试不同模式:--mode text 或 --mode mixed
- 使用自定义提示词:--prompt 您的提示词
- 检查图像质量(分辨率、清晰度)
- 尝试混合模式:--granularity region
依赖问题
bash
cd skills/pdf-ocr-tool
source .venv/bin/activate
uv sync # 重新安装所有依赖
相关资源
版本历史
- - v1.2.0 - 英文提示词、install-deps.sh、修复.gitignore
- v1.1.0 - 添加混合模式、区域分割、pyproject.toml
- v1.0.0 - 初始版本,包含基本OCR功能
致谢
本工具由OpenClaw社区开发和维护。
许可证
MIT许可证