pdf的roc图片识别并写入word
背景
女朋友考研,有一本专业课的电子版书籍,还有一个知识点手册,她需要根据知识点手册的重点词汇来找的该内容在电子版书籍的哪一页,或者是在哪一块
前提
1.安装 pytesseract库,fitz库
2.下载tesseract.exe
首先我将pdf中的图片一个一个存入某个文件夹,这里应用了fitz库进行pdf的图片提取,代码如下:
import fitz
import re
import os
def pdf2pic(path, pic_path):
'''
# 从pdf中提取图片
:param path: pdf的路径
:param pic_path: 图片保存的路径
:return:
'''
# 打开pdf
doc = fitz.open(path)
nums = doc.xref_length()
imgcount = 0 # 图像计数
# 遍历每一个对象
for i in range(1, nums):
text = doc.xref_object(i)
# print(i, text)
# 过滤无用图片
if ('Width 2550' in text) and ('Height 3300' in text) or ('thumbnail' in text):
continue
# 使用正则表达式来查找图片
checkXO = r"/Type(?= */XObject)"
checkIM = r"/Subtype(?= */Image)"
isXObject = re