windows下Python實現(xiàn)將pdf文件轉化為png格式圖片的方法
本文實例講述了windows下Python實現(xiàn)將pdf文件轉化為png格式圖片的方法。分享給大家供大家參考,具體如下:
最近工作中需要把pdf文件轉化為圖片,想用Python來實現(xiàn),于是在網(wǎng)上找啊找啊找啊找,找了半天,倒是找到一些代碼。
1、第一個找到的代碼,我試了一下好像是反了,只能實現(xiàn)把圖片轉為pdf,而不能把pdf轉為圖片。。。
參考鏈接: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、第二個是文章寫的比較詳細,可惜的是linux下的代碼,所以仍然沒用。
3、第三個文章指出有一個庫PythonMagick可以實現(xiàn)這個功能,需要下載一個庫 PythonMagick-0.9.10-cp27-none-win_amd64.whl 這個是64位的。
這里不得不說自己又犯了一個錯誤,因為自己從python官網(wǎng)上下載了一個python 2.7,以為是64位的版本,實際上是32位的版本,所以導致python的版本(32位)和下載的PythonMagick的版本(64位)不一致,弄到晚上12點多,總算了發(fā)現(xiàn)了這個問題。。。
4、然后,接下來繼續(xù)用搜索引擎搜,找到很多stackoverflow的問題帖子,發(fā)現(xiàn)了2個代碼,不過要先下載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í)行時都報錯了,這個是 代碼2 的報錯信息:
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) +']') 這一行報錯。
于是,根據(jù)報錯的信息在網(wǎng)上查,但是沒查到什么有用的信息,但是感覺應該和GhostScript有關,于是在網(wǎng)上去查安裝包,找到一個在github上的下載連接,但是點進去的時候顯示無法下載。
最后,在csdn的下載中找到了 這個文件:GhostScript_Windows_9.15_win32_win64,安裝了64位版本,之后,再次運行上面的代碼,都能用了。
不過代碼2需要做如下修改,不然還是會報 No such file or directory @ error/pdf.c/ReadPDFImage/713 錯誤:
#代碼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'
這次有個很深刻的體會,就是解決這個問題過程中,大部分時間都是用在查資料、驗證資格資料是否有用上了,搜索資料的能力很重要。
而在實際搜索資料的過程中,國內(nèi)關于PythonMagick的文章太少了,搜索出來的大部分有幫助的文章都是國外的,但是這些國外的帖子文章,也沒有解決我的問題或者是給出有用的線索,最后還是通過自己的思考,解決了問題。
更多關于Python相關內(nèi)容感興趣的讀者可查看本站專題:《Python圖片操作技巧總結》、《Python數(shù)據(jù)結構與算法教程》、《Python Socket編程技巧總結》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
相關文章
Windows系統(tǒng)下cython_bbox庫的正確安裝步驟
Cython-bbox一般無法直接通過pip直接安裝,那么如何安裝呢?下面這篇文章主要給大家介紹了關于Windows系統(tǒng)下cython_bbox庫的正確安裝步驟,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2023-05-05
Pandas中`ValueError: cannot reindex from
在Pandas中,ValueError: cannot reindex from a duplicate axis錯誤通常發(fā)生在嘗試對包含重復索引的DataFrame或Series進行重新索引(reindex)時,所以本文介紹了Pandas中`ValueError: cannot reindex from a duplicate axis`錯誤分析及解決辦法,需要的朋友可以參考下2024-07-07
python實現(xiàn)監(jiān)控指定進程的cpu和內(nèi)存使用率
這篇文章主要為大家詳細介紹了python實現(xiàn)監(jiān)控指定進程的cpu和內(nèi)存使用率,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01
Python類方法__init__和__del__構造、析構過程分析
這篇文章主要介紹了Python類方法__init__和__del__構造、析構過程分析,本文分析了什么時候構造、什么時候析構、成員變量如何處理、Python中的共享成員函數(shù)如何訪問等問題,需要的朋友可以參考下2015-03-03
從Pytorch模型pth文件中讀取參數(shù)成numpy矩陣的操作
這篇文章主要介紹了從Pytorch模型pth文件中讀取參數(shù)成numpy矩陣的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03

