利用python将图片版PDF转文字版PDF
时间:2022-04-02 10:33 作者:admin
图片版PDF无法复制,转化成文字版的PDF后使用更方便.
我们需要用到python/' target='_blank'>python3.6,pypdf2,ghostscript,PythonMagick,百度文字识别服务和pdfkit.
安装
安装python3.6 略
安装ghostscript
https://ghostscript.com/download/gsdnld.html
安装wkhtmltopdf
https://wkhtmltopdf.org/downloads.html
pip安装PyPDF2,ghostscript,baidu-aip,pdfkit
pip install PyPDF2pip install ghostscriptpip install baidu-aippip install pdfkit
pip安装PythonMagick
https://www.lfd.uci.edu/~gohlke/pythonlibs/
cd 下载目录pip install PythonMagick‑0.9.13‑cp36‑cp36m‑win_amd64.whl
pypdf2用于拆分和合并PDF
示例代码如下:
#导入PdfFileReader和PdfFileWriterfrom PyPDF2 import PdfFileReader, PdfFileWriter#获取一个pdf对象pdf_input = PdfFileReader(open(r'pdf路径', 'rb'))#获取pdf页数page_count = pdf_input.getNumPages()#获取pdf第四页的内容page = pdf_input.getPage(3)page['/Contents']#获取一个pdfWriter对象pdf_output = PdfFileWriter()# 将一个 PageObject 加入到 PdfFileWriter 中pdf_output.addPage(page)#把新pdf保存pdf_output.write(open(r'新pdf路径','wb'))
PythonMagick用于将单页PDF转化为jpg
百度云-文字识别-python SDK
每天有500次免费的识别
示例代码如下:
#导入baidu-aipfrom aip import AipOcr#https://console.bce.baidu.com/#/index/overview#产品服务->人工智能->文字识别->创建应用#获取以下三个值APP_ID = '??'API_KEY = '??'SECRET_KEY = '?? '#新建一个AipOcrclient = AipOcr(APP_ID, API_KEY, SECRET_KEY)#读取本地图片的函数def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read()#读取本地图片image = get_file_content('p1.jpg')#可选参数options = {}options["language_type"] = "CHN_ENG"options["detect_direction"] = "true"options["detect_language"] = "true"options["probability"] = "true"#通用文字识别client.basicGeneral(image, options)#读取网络图片url = "https://note.youdao.com/yws/public/resource/1577071c1ffa2b6bf4e238ef6dbcfbf5/xmlnote/E5A19BEDFEBA4879B217C5BBF53B0245/22138"#可选参数options = {}options["language_type"] = "CHN_ENG"options["detect_direction"] = "true"options["detect_language"] = "true"options["probability"] = "true"#通用文字识别client.basicGeneralUrl(url, options)#读取本地表格图片的函数def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read()#读取本地表格图片image = get_file_content('p2.jpg')#可选参数options = {}options["language_type"] = "CHN_ENG"options["detect_direction"] = "true"options["detect_language"] = "true"options["probability"] = "true"#通用文字识别client.basicGeneral(image, options)#读取表格分割效果较差!
pdfkit用于利用字符串生成pdf
示例代码如下:
#pdfkit安装位置设置path_wk = r'pdfkit安装位置设置'pdfkit_config = pdfkit.configuration(wkhtmltopdf = path_wk)#pdfkit参数pdfkit_options = {'encoding': 'UTF-8',}#制作PDFpdfkit.from_string(('string'),'D:\test.pdf',configuration=pdfkit_config,options=pdfkit_options)
完整代码如下
#导入所需包#os,操作文件和路径import os#ghostscript,代码简化import ghostscript#pypdf2,拆分pdffrom PyPDF2 import PdfFileReader, PdfFileWriter#PythonMagick,单页PDF转图片from PythonMagick import Image#baidu-aip,百度文字识别from aip import AipOcr#pdfkit,字符串制作PDFimport pdfkit#参数path='??'pdfname='??'DPI='85'#https://console.bce.baidu.com/#/index/overview#产品服务->人工智能->文字识别->创建应用#获取以下三个值APP_ID = '??'API_KEY = '??'SECRET_KEY = '?? '#pdfkit安装位置设置path_wk = r'pdfkit安装位置设置'pdfkit_config = pdfkit.configuration(wkhtmltopdf = path_wk)#pdfkit参数pdfkit_options = {'encoding': 'UTF-8',}#PDF转化为图片os.chdir(path)pdf_input = PdfFileReader(open(pdfname, 'rb'))#自动获取PDF页数page_count = pdf_input.getNumPages()page_range=range(page_count)#也可以手工指定PDF需要转换的页数#page_range=range(0,100)#使用PyPDF和ghostscript#==超级好用,超级直观,超级短==for page_num in page_range: im = Image() im.density(DPI) im.read(pdfname + '[' + str(page_num) +']') im.write(str(page_num)+ '.jpg')#图片转化为字符串#新建一个AipOcrclient = AipOcr(APP_ID, API_KEY, SECRET_KEY)#读取本地图片的函数def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read()#可选参数options = {}options["language_type"] = "CHN_ENG"options["detect_direction"] = "false"options["detect_language"] = "false"options["probability"] = "false"allteststr=[]for page_num in page_range: #读取本地图片 image = get_file_content(r'%s\%s.jpg' % (path,page_num)) #通用文字识别,得到的是一个dict testjson=client.basicGeneral(image, options) teststr='' for x in testjson['words_result']: teststr=teststr+x['words']+'</br>' allteststr.append(teststr)#字符串写入PDFfor page_num in page_range: pdfkit.from_string((allteststr[page_num]),'%s.pdf' % (str(page_num)),configuration=pdfkit_config,options=pdfkit_options)#合并单页PDFpdf_output = PdfFileWriter()for page_num in page_range: os.chdir(path) pdf_input = PdfFileReader(open('%s.pdf' % (str(page_num)), 'rb')) page = pdf_input.getPage(0) pdf_output.addPage(page)pdf_output.write(open('newpdf.pdf','wb'))
以上就是为大家介绍的如何使用python3.6,pypdf2,ghostscript,PythonMagick,百度文字识别服务和pdfkit
(责任编辑:admin)