windows下Python實(shí)現(xiàn)將pdf文件轉(zhuǎn)化為png格式圖片的方法
本文實(shí)例講述了windows下Python實(shí)現(xiàn)將pdf文件轉(zhuǎn)化為png格式圖片的方法。分享給大家供大家參考,具體如下:
最近工作中需要把pdf文件轉(zhuǎn)化為圖片,想用Python來實(shí)現(xiàn),于是在網(wǎng)上找啊找啊找啊找,找了半天,倒是找到一些代碼。
1、第一個(gè)找到的代碼,我試了一下好像是反了,只能實(shí)現(xiàn)把圖片轉(zhuǎn)為pdf,而不能把pdf轉(zhuǎn)為圖片。。。
參考鏈接:https://zhidao.baidu.com/question/745221795058982452.html
代碼如下:
#!/usr/bin/env python import os import sys from reportlab.lib.pagesizes import A4, landscape from reportlab.pdfgen import canvas f = sys.argv[1] filename = ''.join(f.split('/')[-1:])[:-4] f_jpg = filename+'.jpg' print f_jpg def conpdf(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、第二個(gè)是文章寫的比較詳細(xì),可惜的是linux下的代碼,所以仍然沒用。
3、第三個(gè)文章指出有一個(gè)庫PythonMagick可以實(shí)現(xiàn)這個(gè)功能,需要下載一個(gè)庫 PythonMagick-0.9.10-cp27-none-win_amd64.whl 這個(gè)是64位的。
這里不得不說自己又犯了一個(gè)錯(cuò)誤,因?yàn)樽约簭膒ython官網(wǎng)上下載了一個(gè)python 2.7,以為是64位的版本,實(shí)際上是32位的版本,所以導(dǎo)致python的版本(32位)和下載的PythonMagick的版本(64位)不一致,弄到晚上12點(diǎn)多,總算了發(fā)現(xiàn)了這個(gè)問題。。。
4、然后,接下來繼續(xù)用搜索引擎搜,找到很多stackoverflow的問題帖子,發(fā)現(xiàn)了2個(gè)代碼,不過要先下載PyPDF2以及ghostscript模塊。
先通過pip來安裝 PyPDF2、PythonMagick、ghostscript 模塊。
C:\Users\Administrator>pip install PyPDF2 Collecting PyPDF2 Using cached PyPDF2-1.25.1.tar.gz Installing collected packages: PyPDF2 Running setup.py install for PyPDF2 Successfully installed PyPDF2-1.25.1 You are using pip version 7.1.2, however version 8.1.2 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command. C:\Users\Administrator>pip install C:\PythonMagick-0.9.10-cp27-none-win_amd64.whl Processing c:\pythonmagick-0.9.10-cp27-none-win_amd64.whl Installing collected packages: PythonMagick Successfully installed PythonMagick-0.9.10 You are using pip version 7.1.2, however version 8.1.2 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command. C:\Users\Administrator>pip install ghostscript Collecting ghostscript Downloading ghostscript-0.4.1.tar.bz2 Requirement already satisfied (use --upgrade to upgrade): setuptools in c:\python27\lib\site-packages (from ghostscript) Installing collected packages: ghostscript Running setup.py install for ghostscript Successfully installed ghostscript-0.4.1 You are using pip version 7.1.2, however version 8.1.2 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.
下面是代碼
代碼1:
import os import ghostscript from PyPDF2 import PdfFileReader, PdfFileWriter from tempfile import NamedTemporaryFile from PythonMagick import Image reader = PdfFileReader(open("C:/deep.pdf", "rb")) for page_num in xrange(reader.getNumPages()): writer = PdfFileWriter() writer.addPage(reader.getPage(page_num)) temp = NamedTemporaryFile(prefix=str(page_num), suffix=".pdf", delete=False) writer.write(temp) print temp.name tempname = temp.name temp.close() im = Image(tempname) #im.density("3000") # DPI, for better quality #im.read(tempname) im.write("some_%d.png" % (page_num)) os.remove(tempname)
代碼2:
import sys import PyPDF2 import PythonMagick import ghostscript pdffilename = "C:\deep.pdf" pdf_im = PyPDF2.PdfFileReader(file(pdffilename, "rb")) print '1' npage = pdf_im.getNumPages() print('Converting %d pages.' % npage) for p in range(npage): im = PythonMagick.Image() im.density('300') im.read(pdffilename + '[' + str(p) +']') im.write('file_out-' + str(p)+ '.png') #print pdffilename + '[' + str(p) +']','file_out-' + str(p)+ '.png'
然后執(zhí)行時(shí)都報(bào)錯(cuò)了,這個(gè)是 代碼2 的報(bào)錯(cuò)信息:
Traceback (most recent call last): File "C:\c.py", line 15, in <module> im.read(pdffilename + '[' + str(p) +']') RuntimeError: pythonw.exe: PostscriptDelegateFailed `C:\DEEP.pdf': No such file or directory @ error/pdf.c/ReadPDFImage/713
總是在上面的 im.read(pdffilename + '[' + str(p) +']') 這一行報(bào)錯(cuò)。
于是,根據(jù)報(bào)錯(cuò)的信息在網(wǎng)上查,但是沒查到什么有用的信息,但是感覺應(yīng)該和GhostScript有關(guān),于是在網(wǎng)上去查安裝包,找到一個(gè)在github上的下載連接,但是點(diǎn)進(jìn)去的時(shí)候顯示無法下載。
最后,在csdn的下載中找到了 這個(gè)文件:GhostScript_Windows_9.15_win32_win64,安裝了64位版本,之后,再次運(yùn)行上面的代碼,都能用了。
不過代碼2需要做如下修改,不然還是會(huì)報(bào) No such file or directory @ error/pdf.c/ReadPDFImage/713 錯(cuò)誤:
#代碼2 import sys import PyPDF2 import PythonMagick import ghostscript pdffilename = "C:\deep.pdf" pdf_im = PyPDF2.PdfFileReader(file(pdffilename, "rb")) print '1' npage = pdf_im.getNumPages() print('Converting %d pages.' % npage) for p in range(npage): im = PythonMagick.Image(pdffilename + '[' + str(p) +']') im.density('300') #im.read(pdffilename + '[' + str(p) +']') im.write('file_out-' + str(p)+ '.png') #print pdffilename + '[' + str(p) +']','file_out-' + str(p)+ '.png'
這次有個(gè)很深刻的體會(huì),就是解決這個(gè)問題過程中,大部分時(shí)間都是用在查資料、驗(yàn)證資格資料是否有用上了,搜索資料的能力很重要。
而在實(shí)際搜索資料的過程中,國(guó)內(nèi)關(guān)于PythonMagick的文章太少了,搜索出來的大部分有幫助的文章都是國(guó)外的,但是這些國(guó)外的帖子文章,也沒有解決我的問題或者是給出有用的線索,最后還是通過自己的思考,解決了問題。
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python圖片操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- Python自制一個(gè)PDF轉(zhuǎn)PNG圖片小工具
- 詳解python實(shí)現(xiàn)多張多格式圖片轉(zhuǎn)PDF并打包成exe
- Python自動(dòng)化辦公之圖片轉(zhuǎn)PDF的實(shí)現(xiàn)
- Python實(shí)現(xiàn)文字pdf轉(zhuǎn)換圖片pdf效果
- Python把圖片轉(zhuǎn)化為pdf代碼實(shí)例
- 利用python將圖片版PDF轉(zhuǎn)文字版PDF
- Python 將pdf轉(zhuǎn)成圖片的方法
- 用python 制作圖片轉(zhuǎn)pdf工具
- Python的pdfplumber庫將pdf轉(zhuǎn)為圖片的實(shí)現(xiàn)
相關(guān)文章
Python?常用模塊threading和Thread模塊之線程池
這篇文章主要介紹了Python?threading和Thread模塊之線程池,線程池如消費(fèi)者,負(fù)責(zé)接收任務(wù),并將任務(wù)分配到一個(gè)空閑的線程中去執(zhí)行。并不關(guān)心是哪一個(gè)線程執(zhí)行的這個(gè)任務(wù),具體介紹需要的小伙伴可以參考下面文章詳細(xì)內(nèi)容2022-06-06python實(shí)現(xiàn)ROA算子邊緣檢測(cè)算法
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)ROA算子邊緣檢測(cè)算法,以光學(xué)圖像為例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04Pytorch深度學(xué)習(xí)gather一些使用問題解決方案
這篇文章主要為大家介紹了Pytorch深度學(xué)習(xí),在使用gather過程中遇到的一下問題,下面給出解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09Python實(shí)戰(zhàn)之實(shí)現(xiàn)簡(jiǎn)單的名片管理系統(tǒng)
這篇文章主要介紹了Python實(shí)戰(zhàn)之實(shí)現(xiàn)簡(jiǎn)單的名片管理系統(tǒng),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04Python中字典(dict)和列表(list)的排序方法實(shí)例
這篇文章主要介紹了Python中字典(dict)和列表(list)的排序方法實(shí)例,總結(jié)來說優(yōu)先使用內(nèi)置的sort()方法進(jìn)行排序,需要的朋友可以參考下2014-06-06python matplotlib實(shí)現(xiàn)坐標(biāo)投影的示例代碼
這篇文章主要為大家詳細(xì)介紹了python matplotlib實(shí)現(xiàn)坐標(biāo)投影,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02