前提安装是上一篇,langchain的加载和分割
参考博客:使用paddleOCR批量识别pdf_paddleocr pdf-CSDN博客
遇到问题如下图。个人怀疑文档中有长表内容(是倒立的那种长表)--补充编辑,确实如此,解决方案后续优化了再发
状态:目前未解决。
在上一篇博客的基础上新增pip
pip install paddlepaddle
pip install paddlehub (这里本来参考的这位博主,但是我觉得不够新,就去官网找了安装方式,也就是下一个 Paddle OCR 安装使用教程_paddleocr安装-CSDN博客)
pip install paddleocr
然后进行pdf2img的时候pip了
pip install PyMuPDF
报了个错,具体记不得了,反正让我继续pip
pip install premailer
img2txt时候报错 ,继续pip(这里是pycharm指导的,不是百度)
pip install openpyxl
运行出现上面错误,有所猜测,但是懒得继续尝试了,今天就到这里
代码如下
pdf2img
import datetime
import os
import fitz # fitz就是pip install PyMuPDF
def pdf2png(pdfPath, baseImagePath):
imagePath=os.path.join(baseImagePath,os.path.basename(pdfPath).split('.')[0])
startTime_pdf2img = datetime.datetime.now() # 开始时间
print("imagePath=" + imagePath)
if not os.path.exists(imagePath):
os.makedirs(imagePath)
pdfDoc = fitz.open(pdfPath)
totalPage=pdfDoc.page_count
for pg in range(totalPage):
page = pdfDoc[pg]
rotate = int(0)
zoom_x = 2
zoom_y = 2
mat = fitz.Matrix(zoom_x, zoom_y).prerotate(rotate)
pix = page.get_pixmap(matrix=mat, alpha=False)
print(f'正在保存{pdfPath}的第{pg+1}页,共{totalPage}页')
pix.save(imagePath + '/' + f'images_{pg+1}.png')
endTime_pdf2img = datetime.datetime.now()
print(f'{pdfDoc}-pdf2img-花费时间={(endTime_pdf2img - startTime_pdf2img).seconds}秒')
if __name__ == "__main__":
pdfPath = r'reform.pdf'
baseImagePath = './imgs'
pdf2png(pdfPath, baseImagePath)
img2txt:
import os
import cv2
from paddleocr import PPStructure, save_structure_res
from paddleocr.ppstructure.recovery.recovery_to_doc import sorted_layout_boxes, convert_info_docx
from copy import deepcopy
# 中文测试图
table_engine = PPStructure(recovery=True, lang='ch')
image_path = './imgs/reform'
save_folder = './txt'
def img2docx(img_path):
text = []
imgs = os.listdir(img_path)
for img_name in imgs:
print(os.path.join(img_path, img_name))
img = cv2.imread(os.path.join(img_path, img_name))
result = table_engine(img)
save_structure_res(result, save_folder, os.path.basename(img_path).split('.')[0])
h, w, _ = img.shape
res = sorted_layout_boxes(result, w)
convert_info_docx(img, res, save_folder, os.path.basename(img_path).split('.')[0])
for line in res:
line.pop('img')
print(line)
for pra in line['res']:
text.append(pra['text'])
text.append('\n')
with open('txt/res.txt', 'w', encoding='utf-8') as f:
f.write('\n'.join(text))
img2docx(image_path)