# OCR
OCR(光学字符识别)自动化办公的实现,依赖的第三方库是:poocr
源码地址:https://github.com/CoderWanFeng/poocr
安装:
pip install poocr
# 使用前准备
poocr 使用腾讯云 OCR 接口,使用前需要:
- 注册 腾讯云 账号
- 进入 腾讯云 OCR 控制台 开通服务
- 获取
SecretId和SecretKey
# 1、通用 OCR 识别
对图片中的文字进行识别,返回识别结果。
import poocr
tencent_id = '你的腾讯云SecretId'
tencent_key = '你的腾讯云SecretKey'
res = poocr.ocr.LicensePlateOCR(
img_path=r'D:\test\车牌图片.png',
id=tencent_id,
key=tencent_key
)
print(res)
参数说明:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
img_path | str | 是 | 待识别的图片路径 |
id | str | 是 | 腾讯云 SecretId |
key | str | 是 | 腾讯云 SecretKey |
💡
LicensePlateOCR是车牌识别的接口,poocr 提供了 100+ 种识别接口,涵盖发票、身份证、银行卡、营业执照、车牌、通用文字等场景。详见 全部功能说明。
# 2、发票识别并导出 Excel
批量识别发票(支持图片和 PDF 格式),自动保存为 Excel 文件。
import poocr
tencent_id = '你的腾讯云SecretId'
tencent_key = '你的腾讯云SecretKey'
poocr.ocr2excel.VatInvoiceOCR2Excel(
input_path=r'D:\发票\发票文件夹', # 可以是文件夹(批量)或单个文件路径
output_excel=r'D:\output\发票识别结果.xlsx',
id=tencent_id,
key=tencent_key
)
参数说明:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
input_path | str | 是 | 发票文件路径或文件夹路径(支持 .jpg/.png/.pdf) |
output_excel | str | 是 | 输出的 Excel 文件路径 |
id | str | 是 | 腾讯云 SecretId |
key | str | 是 | 腾讯云 SecretKey |
output_path | str | 否 | 按原始文件名保存的输出目录 |
file_name | bool | 否 | 是否使用原文件名命名,默认 True |
# 3、支持的 OCR2Excel 识别类型
除了发票识别,还支持以下证照的批量识别并导出 Excel:
| 调用方法 | 功能说明 |
|---|---|
VatInvoiceOCR2Excel | 增值税发票识别 |
IDCardOCR2Excel | 身份证识别 |
TrainTicketOCR2Excel | 火车票识别 |
BankCardOCR2Excel | 银行卡识别 |
LicensePlateOCR2Excel | 车牌号识别 |
用法与发票识别相同,只需替换方法名即可:
poocr.ocr2excel.IDCardOCR2Excel(
input_path=r'D:\身份证\',
output_excel=r'D:\output\身份证识别结果.xlsx',
id=tencent_id,
key=tencent_key
)
# 4、PDF 发票识别
支持直接识别 PDF 格式的发票文件:
import poocr
tencent_id = '你的腾讯云SecretId'
tencent_key = '你的腾讯云SecretKey'
pdf_path = r'D:\发票\滴滴发票.pdf'
output_path = r'D:\识别结果'
poocr.ocr2excel.VatInvoiceOCR2Excel(
input_path=pdf_path,
output_path=output_path,
id=tencent_id,
key=tencent_key,
file_name=False
)
