windows下Python实现将pdf文件转化为png格式图片的方法
本文实例讲述了windows下Python实现将pdf文件转化为png格式图片的方法。分享给大家供大家参考,具体如下:
最近工作中需要把pdf文件转化为图片,想用Python来实现,于是在网上找啊找啊找啊找,找了半天,倒是找到一些代码。
1、第一个找到的代码,我试了一下好像是反了,只能实现把图片转为pdf,而不能把pdf转为图片。。。
参考链接:https://zhidao.baidu.com/question/745221795058982452.html
代码如下:
#!/usr/bin/envpython importos importsys fromreportlab.lib.pagesizesimportA4,landscape fromreportlab.pdfgenimportcanvas f=sys.argv[1] filename=''.join(f.split('/')[-1:])[:-4] f_jpg=filename+'.jpg' printf_jpg defconpdf(f_jpg): f_pdf=filename+'.pdf' (w,h)=landscape(A4) c=canvas.Canvas(f_pdf,pagesize=landscape(A4)) c.drawImage(f,0,0,w,h) c.save() print"okkkkkkkk." conpdf(f_jpg)
2、第二个是文章写的比较详细,可惜的是linux下的代码,所以仍然没用。
3、第三个文章指出有一个库PythonMagick可以实现这个功能,需要下载一个库PythonMagick-0.9.10-cp27-none-win_amd64.whl这个是64位的。
这里不得不说自己又犯了一个错误,因为自己从python官网上下载了一个python2.7,以为是64位的版本,实际上是32位的版本,所以导致python的版本(32位)和下载的PythonMagick的版本(64位)不一致,弄到晚上12点多,总算了发现了这个问题。。。
4、然后,接下来继续用搜索引擎搜,找到很多stackoverflow的问题帖子,发现了2个代码,不过要先下载PyPDF2以及ghostscript模块。
先通过pip来安装PyPDF2、PythonMagick、ghostscript模块。
C:\Users\Administrator>pipinstallPyPDF2 CollectingPyPDF2 UsingcachedPyPDF2-1.25.1.tar.gz Installingcollectedpackages:PyPDF2 Runningsetup.pyinstallforPyPDF2 SuccessfullyinstalledPyPDF2-1.25.1 Youareusingpipversion7.1.2,howeverversion8.1.2isavailable. Youshouldconsiderupgradingviathe'python-mpipinstall--upgradepip'command. C:\Users\Administrator>pipinstallC:\PythonMagick-0.9.10-cp27-none-win_amd64.whl Processingc:\pythonmagick-0.9.10-cp27-none-win_amd64.whl Installingcollectedpackages:PythonMagick SuccessfullyinstalledPythonMagick-0.9.10 Youareusingpipversion7.1.2,howeverversion8.1.2isavailable. Youshouldconsiderupgradingviathe'python-mpipinstall--upgradepip'command. C:\Users\Administrator>pipinstallghostscript Collectingghostscript Downloadingghostscript-0.4.1.tar.bz2 Requirementalreadysatisfied(use--upgradetoupgrade):setuptoolsinc:\python27\lib\site-packages(fromghostscript) Installingcollectedpackages:ghostscript Runningsetup.pyinstallforghostscript Successfullyinstalledghostscript-0.4.1 Youareusingpipversion7.1.2,howeverversion8.1.2isavailable. Youshouldconsiderupgradingviathe'python-mpipinstall--upgradepip'command.
下面是代码
代码1:
importos importghostscript fromPyPDF2importPdfFileReader,PdfFileWriter fromtempfileimportNamedTemporaryFile fromPythonMagickimportImage reader=PdfFileReader(open("C:/deep.pdf","rb")) forpage_numinxrange(reader.getNumPages()): writer=PdfFileWriter() writer.addPage(reader.getPage(page_num)) temp=NamedTemporaryFile(prefix=str(page_num),suffix=".pdf",delete=False) writer.write(temp) printtemp.name tempname=temp.name temp.close() im=Image(tempname) #im.density("3000")#DPI,forbetterquality #im.read(tempname) im.write("some_%d.png"%(page_num)) os.remove(tempname)
代码2:
importsys importPyPDF2 importPythonMagick importghostscript pdffilename="C:\deep.pdf" pdf_im=PyPDF2.PdfFileReader(file(pdffilename,"rb")) print'1' npage=pdf_im.getNumPages() print('Converting%dpages.'%npage) forpinrange(npage): im=PythonMagick.Image() im.density('300') im.read(pdffilename+'['+str(p)+']') im.write('file_out-'+str(p)+'.png') #printpdffilename+'['+str(p)+']','file_out-'+str(p)+'.png'
然后执行时都报错了,这个是代码2的报错信息:
Traceback(mostrecentcalllast): File"C:\c.py",line15,inim.read(pdffilename+'['+str(p)+']') RuntimeError:pythonw.exe:PostscriptDelegateFailed`C:\DEEP.pdf':Nosuchfileordirectory@error/pdf.c/ReadPDFImage/713
总是在上面的 im.read(pdffilename+'['+str(p)+']')这一行报错。
于是,根据报错的信息在网上查,但是没查到什么有用的信息,但是感觉应该和GhostScript有关,于是在网上去查安装包,找到一个在github上的下载连接,但是点进去的时候显示无法下载。
最后,在csdn的下载中找到了这个文件:GhostScript_Windows_9.15_win32_win64,安装了64位版本,之后,再次运行上面的代码,都能用了。
不过代码2需要做如下修改,不然还是会报Nosuchfileordirectory@error/pdf.c/ReadPDFImage/713错误:
#代码2 importsys importPyPDF2 importPythonMagick importghostscript pdffilename="C:\deep.pdf" pdf_im=PyPDF2.PdfFileReader(file(pdffilename,"rb")) print'1' npage=pdf_im.getNumPages() print('Converting%dpages.'%npage) forpinrange(npage): im=PythonMagick.Image(pdffilename+'['+str(p)+']') im.density('300') #im.read(pdffilename+'['+str(p)+']') im.write('file_out-'+str(p)+'.png') #printpdffilename+'['+str(p)+']','file_out-'+str(p)+'.png'
这次有个很深刻的体会,就是解决这个问题过程中,大部分时间都是用在查资料、验证资格资料是否有用上了,搜索资料的能力很重要。
而在实际搜索资料的过程中,国内关于PythonMagick的文章太少了,搜索出来的大部分有帮助的文章都是国外的,但是这些国外的帖子文章,也没有解决我的问题或者是给出有用的线索,最后还是通过自己的思考,解决了问题。
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python图片操作技巧总结》、《Python数据结构与算法教程》、《PythonSocket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。