基于Pyqt5和PaddleOCR实现PDF转DOC
创始人
2024-04-23 14:37:34
0

在上一篇文章《基于Pyqt5实现笔记本摄像头拍照及PaddleOCR测试》的基础上,继续做了个简单的扩展:
将PDF文档转换为DOC文档。

一、界面增加一个按钮,如下图:
在这里插入图片描述

二、源码修改
1、paddleocr.py文件直接拷贝 Github下载的源码PaddleOCR-release-2.6中的文件,但要注释掉def main():那段代码。

2、修改ui后保存文件,回到PyCharm工程,ocr_camera.ui文件,然后右键,找到"External Tools",选择PyUIC,更新ocr_camera.py文件。

3、main函数修改:
3.1、新增PDF转DOC按钮点击槽链接:
在这里插入图片描述
3.2、槽函数定义如下,实现打开文件夹选择pdf文件及转换功能。
在这里插入图片描述

   def pdfRecognition(self, image_dir):fname, _ = QFileDialog.getOpenFileName(self, '选择PDF文件', './', 'Image files(*.PDF *.pdf)')self.showtext.append("loadPDF {}".format(fname))self.PDFmain(fname)def PDFmain(self,image_dir):args = paddleocr.parse_args(mMain=True)args.use_pdf2docx_api=Trueargs.recovery=Trueargs.output='pdf'print("{}".format(image_dir))if paddleocr.is_link(image_dir):paddleocr.download_with_progressbar(image_dir, 'tmp.jpg')image_file_list = ['tmp.jpg']else:image_file_list = get_image_file_list(image_dir)if len(image_file_list) == 0:logger.error('no images find in {}'.format(image_dir))self.showtext.append('no images find in {}'.format(image_dir))returnengine = paddleocr.PPStructure()for img_path in image_file_list:img_name = os.path.basename(img_path).split('.')[0]logger.info('{}{}{}'.format('*' * 10, img_path, '*' * 10))img, flag_gif, flag_pdf = paddleocr.check_and_read(img_path)if not flag_gif and not flag_pdf:img = cv2.imread(img_path)if args.recovery and args.use_pdf2docx_api and flag_pdf:from pdf2docx.converter import Converterdocx_file = os.path.join(args.output,'{}.docx'.format(img_name))cv = Converter(img_path)cv.convert(docx_file)cv.close()logger.info('docx save to {}'.format(docx_file))self.showtext.append('docx save to {}'.format(docx_file))continueif not flag_pdf:if img is None:logger.error("error in loading image:{}".format(img_path))continueimg_paths = [[img_path, img]]else:img_paths = []for index, pdf_img in enumerate(img):os.makedirs(os.path.join(args.output, img_name), exist_ok=True)pdf_img_path = os.path.join(args.output, img_name,img_name + '_' + str(index) + '.jpg')cv2.imwrite(pdf_img_path, pdf_img)img_paths.append([pdf_img_path, pdf_img])all_res = []for index, (new_img_path, img) in enumerate(img_paths):logger.info('processing {}/{} page:'.format(index + 1,len(img_paths)))self.showtext.append('processing {}/{} page:'.format(index + 1,len(img_paths)))new_img_name = os.path.basename(new_img_path).split('.')[0]result = engine(new_img_path, img_idx=index)paddleocr.save_structure_res(result, args.output, img_name, index)if args.recovery and result != []:from copy import deepcopyfrom ppstructure.recovery.recovery_to_doc import sorted_layout_boxesh, w, _ = img.shaperesult_cp = deepcopy(result)result_sorted = sorted_layout_boxes(result_cp, w)all_res += result_sortedif args.recovery and all_res != []:try:from ppstructure.recovery.recovery_to_doc import convert_info_docxconvert_info_docx(img, all_res, args.output, img_name)except Exception as ex:logger.error("error in layout recovery image:{}, err msg: {}".format(img_name, ex))continuefor item in all_res:item.pop('img')item.pop('res')logger.info(item)logger.info('result save to {}'.format(args.output))self.showtext.append('processing {}/{} page:'.format(index + 1,len(img_paths)))

三、编译

编译时报错:AttributeError: ‘Document’ object has no attribute ‘pageCount’
网上查说是PyMuPDF版本不对,安装1.18.14版本

pip install PyMuPDF==1.18.14

在这里插入图片描述
安装完成后,提示pdf2docx需要PyMuPDF>=1.19.0。先编译,果然报错

在这里插入图片描述
重新安装1.19.0版本后编译通过。

pip install PyMuPDF==1.19.0

四、测试

1、第一次测试时,会下载相关模型。

2、打开的pdf文件转换为同名的docx文件
在这里插入图片描述
转换前后对比如下:

在这里插入图片描述

相关内容

热门资讯

保存时出现了1个错误,导致这篇... 当保存文章时出现错误时,可以通过以下步骤解决问题:查看错误信息:查看错误提示信息可以帮助我们了解具体...
汇川伺服电机位置控制模式参数配... 1. 基本控制参数设置 1)设置位置控制模式   2)绝对值位置线性模...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
表格中数据未显示 当表格中的数据未显示时,可能是由于以下几个原因导致的:HTML代码问题:检查表格的HTML代码是否正...
本地主机上的图像未显示 问题描述:在本地主机上显示图像时,图像未能正常显示。解决方法:以下是一些可能的解决方法,具体取决于问...
表格列调整大小出现问题 问题描述:表格列调整大小出现问题,无法正常调整列宽。解决方法:检查表格的布局方式是否正确。确保表格使...
不一致的条件格式 要解决不一致的条件格式问题,可以按照以下步骤进行:确定条件格式的规则:首先,需要明确条件格式的规则是...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...