Intelligent PDF to Markdown converter using glmocr SDK
Project description
markitdown-glmocr
智能 PDF 转 Markdown 插件,使用 glmocr SDK(智谱 GLM-OCR)驱动的图片和表格提取。
特性
- 🔍 智能检测:自动识别每页内容类型(纯文本 vs 图片/表格)
- 📄 默认解析:纯文本页面使用 pdfplumber/pdfminer 提取,速度快、成本低
- 🤖 AI 增强:复杂页面(图片、表格)使用 glmocr SDK 转换为 Markdown
- ⚡ 一行调用:
glmocr.parse("document.pdf")完成 OCR,无需手动截图编码 - 📊 结构化输出:返回 Markdown + JSON 结构(含区域标签、边界框)
安装
# 基础安装
pip install markitdown-glmocr
# 安装 AI 功能
pip install markitdown-glmocr[glmocr]
配置
环境变量(推荐)
# 必需:智谱 API Key
export ZHIPU_API_KEY="your-zhipu-api-key"
# 可选
export GLMOCR_MODEL="glm-ocr" # 模型名称
export GLMOCR_TIMEOUT="600" # 请求超时(秒)
export GLMOCR_ENABLE_LAYOUT="true" # 启用布局检测
export GLMOCR_LOG_LEVEL="INFO" # 日志级别
配置优先级
构造函数参数 > 环境变量 > .env 文件 > config.yaml > 内置默认值
本地敏感配置
# 创建 .env 文件(自动读取)
echo "ZHIPU_API_KEY=your-api-key" > .env
使用方法
命令行(推荐)
# 1. 设置 API Key
export ZHIPU_API_KEY="sk-xxx"
# 2. 查看已安装插件
markitdown --list-plugins
# 3. 使用插件转换 PDF
markitdown -p document.pdf
# 4. 保存到文件
markitdown -p document.pdf -o output.md
Python API
from markitdown import MarkItDown
from markitdown_glmocr import GlmOcrConverter
# 方式1:自动从环境变量读取 ZHIPU_API_KEY
converter = GlmOcrConverter()
md = MarkItDown(enable_plugins=False)
md.register_converter(converter, priority=-1.0)
result = md.convert("document.pdf")
print(result.markdown)
# 方式2:手动传入 API Key
converter = GlmOcrConverter(api_key="sk-xxx")
md = MarkItDown(enable_plugins=False)
md.register_converter(converter, priority=-1.0)
result = md.convert("document.pdf")
print(result.markdown)
# 方式3:直接使用 glmocr SDK(更简单)
import glmocr
result = glmocr.parse("document.pdf")
print(result.markdown_result) # Markdown 输出
print(result.json_result) # 结构化 JSON(区域标签、边界框)
处理结果
import glmocr
result = glmocr.parse("report.pdf")
# 获取 Markdown
print(result.markdown_result)
# 获取结构化数据(按页分组)
for page_idx, page_regions in enumerate(result.json_result):
print(f"Page {page_idx + 1}: {len(page_regions)} regions")
for region in page_regions:
print(f" [{region['label']}] {region['content'][:60]}")
# 按标签筛选
tables = [r for r in result.json_result[0] if r["label"] == "table"]
formulas = [r for r in result.json_result[0] if r["label"] == "formula"]
# 保存到磁盘
result.save(output_dir="./output")
配置选项
GlmOcrConverter 参数
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
api_key |
str | 环境变量 ZHIPU_API_KEY |
智谱 API Key |
timeout |
int | 1800 | 请求超时(秒) |
enable_layout |
bool | False | 启用布局检测 |
force_ai |
bool | False | 强制所有页面使用 AI |
环境变量
| 变量 | 说明 | 示例 |
|---|---|---|
ZHIPU_API_KEY |
API Key(必需) | sk-abc123 |
GLMOCR_MODEL |
模型名称 | glm-ocr |
GLMOCR_TIMEOUT |
请求超时(秒) | 600 |
GLMOCR_ENABLE_LAYOUT |
布局检测 | true |
GLMOCR_LOG_LEVEL |
日志级别 | INFO |
工作原理
PDF 输入
│
▼
逐页分析内容类型
│
├─ 纯文本页面 ──► pdfplumber 提取文本
│
└─ 复杂页面(图片/表格)
│
└─► glmocr.parse() 一行调用
│
├─ 内置截图渲染
├─ 内置 base64 编码
└─ 内置 OCR 识别
│
▼
合并输出完整 Markdown
区域标签(json_result)
glmocr SDK 返回的结构化数据支持以下标签:
| 标签 | 说明 |
|---|---|
title |
标题 |
text |
正文文本 |
table |
表格 |
figure |
图片 |
formula |
公式 |
header |
页眉 |
footer |
页脚 |
page_number |
页码 |
reference |
参考文献 |
seal |
印章 |
技术架构
- glmocr: 智谱 OCR SDK,一行代码完成 PDF/图片解析
- pdfplumber: PDF 页面分析和纯文本提取
- pdfminer: 纯文本页面提取备用
依赖
markitdown>=0.1.0- 基础框架pdfplumber>=0.11.9- PDF 解析和截图pdfminer.six>=20251230- 文本提取备用Pillow>=9.0.0- 图像处理glmocr- 智谱 OCR SDK(可选,AI 功能需要)
许可证
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file markitdown_glmocr-0.1.0.tar.gz.
File metadata
- Download URL: markitdown_glmocr-0.1.0.tar.gz
- Upload date:
- Size: 8.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
41c6353e219d738f6cfddb031c8893939fcc991734605460df5e039e3cece058
|
|
| MD5 |
7bd64013e043ceb4ad8678305ee6c7fb
|
|
| BLAKE2b-256 |
060b7c81121431044e0d8b74133863b04809c0973c7fc349687431ad1b471bc2
|
File details
Details for the file markitdown_glmocr-0.1.0-py3-none-any.whl.
File metadata
- Download URL: markitdown_glmocr-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d5d259e3f2b04b5b9f411f30c170dac97744a3efe0a3611e95911a21f9b4c073
|
|
| MD5 |
d1de1e5e93a675301d6f5a4673846877
|
|
| BLAKE2b-256 |
56def41962283cb062b208a2c0867254d752dd75d62a84897bf0011396280ede
|