GLM-OCR Text Extraction Skill
Extract text from images and PDFs using the GLM-OCR layout parsing API.
When to Use
- - Extract text from images (PNG, JPG, PDF)
- Convert screenshots to text
- Process scanned documents
- OCR photos containing text (including handwritten text)
- Recognize tables and formulas in documents
- User mentions "OCR", "文字识别", "文档解析"
Key Features
- - Table recognition: Detects and converts tables to Markdown format
- Formula extraction: LaTeX format output
- Handwriting support: Strong recognition for handwritten text
- Local file & URL: Supports both local files and remote URLs
Resource Links
https://github.com/zai-org/GLM-OCR |
Prerequisites
- - ZHIPUAPIKEY configured (see Setup below)
Security Notes
- - No runtime package installation is performed by the scripts.
- OCR requests use the fixed official GLM endpoint and do not accept custom API URLs.
- Only
ZHIPU_API_KEY (and optional timeout) is read from environment variables.
⛔ MANDATORY RESTRICTIONS - DO NOT VIOLATE ⛔
- 1. ONLY use GLM-OCR API - Execute the script INLINECODE1
- NEVER parse documents directly - Do NOT try to extract text yourself
- NEVER offer alternatives - Do NOT suggest "I can try to analyze it" or similar
- IF API fails - Display the error message and STOP immediately
- NO fallback methods - Do NOT attempt text extraction any other way
Setup
- 1. Get your API key: https://www.bigmodel.cn/usercenter/proj-mgmt/apikeys
- Configure:
CODEBLOCK0
How to Use
Extract from URL
CODEBLOCK1
Extract from Local File
CODEBLOCK2
Save result to file (recommended)
CODEBLOCK3
CLI Reference
CODEBLOCK4
| Parameter | Required | Description |
|---|
| INLINECODE2 | One of | URL to image/PDF |
| INLINECODE3 |
One of | Local file path to image/PDF |
|
--output,
-o | No | Save result JSON to file |
|
--pretty | No | Pretty-print JSON output |
Response Format
CODEBLOCK5
Key fields:
- -
ok — whether extraction succeeded - INLINECODE8 — extracted text in Markdown (use this for display)
- INLINECODE9 — layout analysis details
- INLINECODE10 — raw API response
- INLINECODE11 — error details on failure
Error Handling
API key not configured:
Error: ZHIPU_API_KEY not configured. Get your API key at: https://www.bigmodel.cn/usercenter/proj-mgmt/apikeys
→ Show exact error to user, guide them to configure
Authentication failed (401/403): API key invalid/expired → reconfigure
Rate limit (429): Quota exhausted → inform user to wait
File not found: Local file missing → check path
Reference
- -
references/output_schema.md — detailed output format specification
GLM-OCR 文本提取技能
使用GLM-OCR布局解析API从图像和PDF中提取文本。
使用场景
- - 从图像(PNG、JPG、PDF)中提取文本
- 将截图转换为文本
- 处理扫描文档
- 对包含文字的图片进行OCR识别(包括手写文字)
- 识别文档中的表格和公式
- 用户提及OCR、文字识别、文档解析
主要特性
- - 表格识别:检测并将表格转换为Markdown格式
- 公式提取:LaTeX格式输出
- 手写支持:对手写文本有较强的识别能力
- 本地文件与URL:支持本地文件和远程URL
资源链接
https://github.com/zai-org/GLM-OCR |
前置条件
安全说明
- - 脚本不会执行运行时包安装。
- OCR请求使用固定的官方GLM端点,不接受自定义API URL。
- 仅从环境变量读取ZHIPUAPIKEY(以及可选的超时设置)。
⛔ 强制性限制 - 不得违反 ⛔
- 1. 仅使用GLM-OCR API - 执行脚本 python scripts/glmocrcli.py
- 绝不直接解析文档 - 不要尝试自行提取文本
- 绝不提供替代方案 - 不要建议我可以尝试分析或类似内容
- 如果API失败 - 显示错误信息并立即停止
- 无备用方法 - 不要尝试任何其他方式的文本提取
设置
- 1. 获取您的API密钥:https://www.bigmodel.cn/usercenter/proj-mgmt/apikeys
- 配置:
bash
python scripts/config_setup.py setup --api-key 您的密钥
使用方法
从URL提取
bash
python scripts/glmocrcli.py --file-url 用户提供的URL
从本地文件提取
bash
python scripts/glmocrcli.py --file /path/to/image.jpg
保存结果到文件(推荐)
bash
python scripts/glmocrcli.py --file-url URL --output result.json
CLI参考
python {baseDir}/scripts/glmocrcli.py (--file-url URL | --file PATH) [--output FILE] [--pretty]
| 参数 | 必需 | 描述 |
|---|
| --file-url | 二选一 | 图像/PDF的URL |
| --file |
二选一 | 图像/PDF的本地文件路径 |
| --output, -o | 否 | 将结果JSON保存到文件 |
| --pretty | 否 | 美化JSON输出格式 |
响应格式
json
{
ok: true,
text: # 提取的Markdown文本...,
layout_details: [[...]],
result: { rawapiresponse: ... },
error: null,
source: /path/to/file.jpg,
source_type: file
}
关键字段:
- - ok — 提取是否成功
- text — 提取的Markdown文本(用于显示)
- layout_details — 布局分析详情
- result — 原始API响应
- error — 失败时的错误详情
错误处理
API密钥未配置:
Error: ZHIPUAPIKEY not configured. Get your API key at: https://www.bigmodel.cn/usercenter/proj-mgmt/apikeys
→ 向用户显示确切错误,引导其进行配置
认证失败(401/403): API密钥无效/已过期 → 重新配置
速率限制(429): 配额已用尽 → 告知用户等待
文件未找到: 本地文件缺失 → 检查路径
参考
- - references/output_schema.md — 详细输出格式规范