Python—OCR识别PDF文件内容

一、准备工作

1、下载OCR识别引擎

参考一下博客分享安装

Tesseract-OCR 下载安装和使用_tesseract-ocr下载-CSDN博客

2、安装poppler工具集

2-1下载链接:

Release Release v21.03.0 · oschwartz10612/poppler-windows · GitHub

安装包

安装步骤:

1、下载后解压

2、配置环境变量

2-1、系统高级设置

2-2、修改Path变量,双击Path

2-3、新建一个变量:D:\Program Files\poppler-21.03.0\Library\bin(例如我的poppler工具集解压路径)

2-4、完成上面操作后做好重启一下电脑

3、实现代码

# coding=utf-8
from pdf2image import convert_from_path
import pytesseract
#import numpy
#from PIL import Image

# PDF文件路径
pdf_path = r'C:\Users\Administrator\Desktop\20240709020427319.pdf'

# 使用pdf2image将PDF的每一页转换为图像
images = convert_from_path(pdf_path)

# OCR引擎的路径(根据你的安装位置可能不同)
pytesseract.pytesseract.tesseract_cmd = r'D:\Program Files\Tesseract-OCR\tesseract.exe'

# 遍历每一页图像并进行OCR
for img_num, img in enumerate(images, start=1):
    # 使用pytesseract进行OCR
    text = pytesseract.image_to_string(img,lang='chi_sim')
    print(f"Page {img_num}:\n{text}")

如果运行有报错则按一下步骤检查问题

1、安装python第三方库poppler-utils,用于调用poppler工具集。

没有报错则会得到一下结果

当然,最终结果根据大家选择的PDF内容而定,以上只是个人的一些学习心得!!!

作者:qq_28230529

物联沃分享整理
物联沃-IOTWORD物联网 » Python—OCR识别PDF文件内容

发表回复