小紫 OCR 文字识别插件
v1.0.0 · 文档处理 · 官方
简介
小紫 OCR 文字识别插件 v1.0,本地 OCR 提取图片与 PDF 扫描件中的文字:
- recognize 单张图片或 PDF 扫描页 OCR
- batch 批量识别多个文件或整个目录
- info 检查 pip / Tesseract 环境是否就绪
- 支持 Win / macOS / Linux,默认 中英文(
chi_sim+eng) - 本地处理,不上传云端
与 pdf_tool 区别:
- pdf_tool:PDF 里本身有可选文字层 → 直接 read
- ocr_tool:扫描版 PDF、拍照、截图 → OCR 识图取字
安装两步:
- pip:
pytesseract Pillow pymupdf - 系统程序:Tesseract OCR(Windows 安装包 / Linux apt / macOS brew)
典型流程:info 检查环境 → recognize 识别单文件 → batch 批量导出 txt
操作说明
| action | 功能 |
|---|---|
recognize | 单文件 OCR,支持图片和 PDF |
batch | 批量 OCR,按目录或文件列表 |
info | 环境检测、语言列表、文件信息 |
支持格式: png/jpg/jpeg/bmp/tiff/webp/gif/pdf
recognize 参数:
filepath必填lang默认chi_sim+eng,可选eng、chi_sim、jpnpagesPDF 页码如1-3,5dpiPDF 渲染精度,默认 200,识别差可试 300max_chars限制返回字数(AI 场景防超长)output导出 txt 路径tesseract_cmdWindows 可指定C:\Program Files\Tesseract-OCR\tesseract.exe
batch 参数:
filepathsJSON 数组,或directory+pattern(如*.png)output_dir批量导出 txt 目录
安装
- 解压到
D:\xiaozi-plugins\文档处理\OCR文字识别插件\ - 复制
python/到本目录 - 安装 pip 依赖:
python\python.exe -m pip install pytesseract Pillow pymupdf
- 安装 Tesseract OCR:
- Windows:https://github.com/UB-Mannheim/tesseract/wiki(勾选 chi_sim)
- Linux:sudo apt install tesseract-ocr tesseract-ocr-chi-sim
- macOS:brew install tesseract tesseract-lang
- 测试:
run.bat --action info
| 文件 | 说明 |
|---|---|
ocr_plugin.py | 主程序 |
run.bat / run.sh | 启动脚本 |
OCR文字识别插件.md | 用户文档 |
config/ocr_config.example.json | 配置示例 |
命令行
run.bat --action info
run.bat --action recognize --filepath D:\scan\发票.jpg
run.bat --action recognize --filepath D:\scan\合同.pdf --pages 1-3 --output D:\scan\合同.txt
run.bat --action batch --directory D:\scan --pattern "*.jpg" --output_dir D:\scan\out
run.bat --action recognize --filepath D:\scan\doc.png --lang eng
注册技能 ocr_tool
名称: ocr_tool
描述: OCR v1.0:recognize/batch/info。图片和PDF扫描件OCR提取中英文。用户识别截图、扫描件、拍照文档时使用。需filepath。依赖Tesseract及pytesseract/Pillow/pymupdf。可选文字PDF请用pdf_tool。
脚本: D:\xiaozi-plugins\文档处理\OCR文字识别插件\run.bat
参数 JSON:
{
"type": "object",
"properties": {
"action": {"type": "string", "enum": ["recognize", "batch", "info"], "description": "操作类型"},
"filepath": {"type": "string", "description": "文件路径"},
"filepaths": {"type": "string", "description": "batch JSON数组"},
"directory": {"type": "string", "description": "batch目录"},
"pattern": {"type": "string", "description": "batch匹配*.png"},
"lang": {"type": "string", "description": "chi_sim+eng/eng"},
"pages": {"type": "string", "description": "PDF页码"},
"dpi": {"type": "string", "description": "PDF DPI默认200"},
"max_chars": {"type": "string", "description": "最大字符数"},
"output": {"type": "string", "description": "输出txt"},
"output_dir": {"type": "string", "description": "batch输出目录"},
"tesseract_cmd": {"type": "string", "description": "Tesseract路径"}
},
"required": ["action"]
}
对话示例
| 场景 | 说法 |
|---|---|
| 识图 | 识别 D:\scan\发票.jpg 里的文字 |
| 扫 PDF | 把 D:\scan\合同.pdf 前 3 页 OCR 成文字 |
| 批量 | 批量识别 D:\scan 下所有 jpg,导出到 out 目录 |
| 查环境 | 检查一下 OCR 是否可用 |
| 英文 | 用英文识别这张 png |
常见错误
| 现象 | 处理 |
|---|---|
| 缺 pip 依赖 | pip install pytesseract Pillow pymupdf |
| 未找到 Tesseract | 安装系统 Tesseract 或 tesseract_cmd 指定路径 |
| 无中文 | 安装 chi_sim 语言包 |
| PDF 效果差 | 提高 dpi 300 或只 OCR 关键页 |
| 有文字层 PDF | 改用 pdf_tool read |
能力边界
| 支持 | 不支持 |
|---|---|
| 图片/PDF扫描 OCR | 手写高精度/表格结构化 |
| 中英文 batch | 云端 OCR API |
| 本地隐私安全 | 视频 OCR |
与其他插件配合
- pdf_tool:有文字层的 PDF 直接 read
- file_convert:格式转换后再 OCR
- word_tool:OCR 结果整理进 Word
详见包内 OCR文字识别插件.md。