windows下Python實(shí)現(xiàn)將pdf文件轉(zhuǎn)化為png格式圖片的方法
本文實(shí)例講述了windows下Python實(shí)現(xiàn)將pdf文件轉(zhuǎn)化為png格式圖片的方法。分享給大家供大家參考,具體如下:
最近工作中需要把pdf文件轉(zhuǎn)化為圖片,想用Python來(lái)實(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下的代碼,所以仍然沒(méi)用。
3、第三個(gè)文章指出有一個(gè)庫(kù)PythonMagick可以實(shí)現(xiàn)這個(gè)功能,需要下載一個(gè)庫(kù) PythonMagick-0.9.10-cp27-none-win_amd64.whl 這個(gè)是64位的。
這里不得不說(shuō)自己又犯了一個(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è)問(wèn)題。。。
4、然后,接下來(lái)繼續(xù)用搜索引擎搜,找到很多stackoverflow的問(wèn)題帖子,發(fā)現(xiàn)了2個(gè)代碼,不過(guò)要先下載PyPDF2以及ghostscript模塊。
先通過(guò)pip來(lái)安裝 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)上查,但是沒(méi)查到什么有用的信息,但是感覺(jué)應(yīng)該和GhostScript有關(guān),于是在網(wǎng)上去查安裝包,找到一個(gè)在github上的下載連接,但是點(diǎn)進(jìn)去的時(shí)候顯示無(wú)法下載。
最后,在csdn的下載中找到了 這個(gè)文件:GhostScript_Windows_9.15_win32_win64,安裝了64位版本,之后,再次運(yùn)行上面的代碼,都能用了。
不過(guò)代碼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è)問(wèn)題過(guò)程中,大部分時(shí)間都是用在查資料、驗(yàn)證資格資料是否有用上了,搜索資料的能力很重要。
而在實(shí)際搜索資料的過(guò)程中,國(guó)內(nèi)關(guān)于PythonMagick的文章太少了,搜索出來(lái)的大部分有幫助的文章都是國(guó)外的,但是這些國(guó)外的帖子文章,也沒(méi)有解決我的問(wèn)題或者是給出有用的線索,最后還是通過(guò)自己的思考,解決了問(wèn)題。
更多關(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庫(kù)將pdf轉(zhuǎn)為圖片的實(shí)現(xiàn)
相關(guān)文章
Python實(shí)現(xiàn)pdf轉(zhuǎn)word詳細(xì)代碼
在日常工作中,我們經(jīng)常會(huì)遇到需要將PDF文件轉(zhuǎn)換成Word文件的需求。雖然市面上有許多PDF轉(zhuǎn)Word的工具,但是它們通常需要付費(fèi)或者有轉(zhuǎn)換后的格式問(wèn)題,這篇文章主要給大家介紹了關(guān)于Python實(shí)現(xiàn)pdf轉(zhuǎn)word的相關(guān)資料,需要的朋友可以參考下2023-09-09
Python基于OpenCV實(shí)現(xiàn)人臉檢測(cè)并保存
這篇文章主要介紹了Python基于OpenCV實(shí)現(xiàn)人臉檢測(cè)并保存,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
如何在Python中將字符串轉(zhuǎn)換為數(shù)組詳解
最近在用Python,做一個(gè)小腳本,有個(gè)操作就是要把內(nèi)容換成數(shù)組對(duì)象再進(jìn)行相關(guān)操作,下面這篇文章主要給大家介紹了關(guān)于如何在Python中將字符串轉(zhuǎn)換為數(shù)組的相關(guān)資料,需要的朋友可以參考下2022-12-12
Windows系統(tǒng)下cython_bbox庫(kù)的正確安裝步驟
Cython-bbox一般無(wú)法直接通過(guò)pip直接安裝,那么如何安裝呢?下面這篇文章主要給大家介紹了關(guān)于Windows系統(tǒng)下cython_bbox庫(kù)的正確安裝步驟,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
Pandas中`ValueError: cannot reindex from
在Pandas中,ValueError: cannot reindex from a duplicate axis錯(cuò)誤通常發(fā)生在嘗試對(duì)包含重復(fù)索引的DataFrame或Series進(jìn)行重新索引(reindex)時(shí),所以本文介紹了Pandas中`ValueError: cannot reindex from a duplicate axis`錯(cuò)誤分析及解決辦法,需要的朋友可以參考下2024-07-07
python實(shí)現(xiàn)監(jiān)控指定進(jìn)程的cpu和內(nèi)存使用率
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)監(jiān)控指定進(jìn)程的cpu和內(nèi)存使用率,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
Python類方法__init__和__del__構(gòu)造、析構(gòu)過(guò)程分析
這篇文章主要介紹了Python類方法__init__和__del__構(gòu)造、析構(gòu)過(guò)程分析,本文分析了什么時(shí)候構(gòu)造、什么時(shí)候析構(gòu)、成員變量如何處理、Python中的共享成員函數(shù)如何訪問(wèn)等問(wèn)題,需要的朋友可以參考下2015-03-03
從Pytorch模型pth文件中讀取參數(shù)成numpy矩陣的操作
這篇文章主要介紹了從Pytorch模型pth文件中讀取參數(shù)成numpy矩陣的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03

