利用python将图片版PDF转文字版PDF
图片版PDF无法复制,转化成文字版的PDF后使用更方便.
我们需要用到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
pipinstallPyPDF2 pipinstallghostscript pipinstallbaidu-aip pipinstallpdfkit
pip安装PythonMagick
https://www.lfd.uci.edu/~gohlke/pythonlibs/
cd下载目录 pipinstallPythonMagick‑0.9.13‑cp36‑cp36m‑win_amd64.whl
pypdf2用于拆分和合并PDF
示例代码如下:
#导入PdfFileReader和PdfFileWriter fromPyPDF2importPdfFileReader,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
百度云-文字识别-pythonSDK
每天有500次免费的识别
示例代码如下:
#导入baidu-aip fromaipimportAipOcr #https://console.bce.baidu.com/#/index/overview #产品服务->人工智能->文字识别->创建应用 #获取以下三个值 APP_ID='??' API_KEY='??' SECRET_KEY='??' #新建一个AipOcr client=AipOcr(APP_ID,API_KEY,SECRET_KEY) #读取本地图片的函数 defget_file_content(filePath): withopen(filePath,'rb')asfp: returnfp.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) #读取本地表格图片的函数 defget_file_content(filePath): withopen(filePath,'rb')asfp: returnfp.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',} #制作PDF pdfkit.from_string(('string'),'D:\test.pdf',configuration=pdfkit_config,options=pdfkit_options)
完整代码如下
#导入所需包 #os,操作文件和路径 importos #ghostscript,代码简化 importghostscript #pypdf2,拆分pdf fromPyPDF2importPdfFileReader,PdfFileWriter #PythonMagick,单页PDF转图片 fromPythonMagickimportImage #baidu-aip,百度文字识别 fromaipimportAipOcr #pdfkit,字符串制作PDF importpdfkit #参数 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 #==超级好用,超级直观,超级短== forpage_numinpage_range: im=Image() im.density(DPI) im.read(pdfname+'['+str(page_num)+']') im.write(str(page_num)+'.jpg') #图片转化为字符串 #新建一个AipOcr client=AipOcr(APP_ID,API_KEY,SECRET_KEY) #读取本地图片的函数 defget_file_content(filePath): withopen(filePath,'rb')asfp: returnfp.read() #可选参数 options={} options["language_type"]="CHN_ENG" options["detect_direction"]="false" options["detect_language"]="false" options["probability"]="false" allteststr=[] forpage_numinpage_range: #读取本地图片 image=get_file_content(r'%s\%s.jpg'%(path,page_num)) #通用文字识别,得到的是一个dict testjson=client.basicGeneral(image,options) teststr='' forxintestjson['words_result']: teststr=teststr+x['words']+'' allteststr.append(teststr) #字符串写入PDF forpage_numinpage_range: pdfkit.from_string((allteststr[page_num]),'%s.pdf'%(str(page_num)),configuration=pdfkit_config,options=pdfkit_options) #合并单页PDF pdf_output=PdfFileWriter() forpage_numinpage_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