国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

windows下Python實現將pdf文件轉化為png格式圖片的方法

2019-11-25 16:00:17
字體:
來源:轉載
供稿:網友

本文實例講述了windows下Python實現將pdf文件轉化為png格式圖片的方法。分享給大家供大家參考,具體如下:

最近工作中需要把pdf文件轉化為圖片,想用Python來實現,于是在網上找啊找啊找啊找,找了半天,倒是找到一些代碼。

1、第一個找到的代碼,我試了一下好像是反了,只能實現把圖片轉為pdf,而不能把pdf轉為圖片。。。

參考鏈接:https://zhidao.baidu.com/question/745221795058982452.html

代碼如下:

#!/usr/bin/env pythonimport osimport sysfrom reportlab.lib.pagesizes import A4, landscapefrom reportlab.pdfgen import canvasf = sys.argv[1]filename = ''.join(f.split('/')[-1:])[:-4]f_jpg = filename+'.jpg'print f_jpgdef 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可以實現這個功能,需要下載一個庫 PythonMagick-0.9.10-cp27-none-win_amd64.whl 這個是64位的。

這里不得不說自己又犯了一個錯誤,因為自己從python官網上下載了一個python 2.7,以為是64位的版本,實際上是32位的版本,所以導致python的版本(32位)和下載的PythonMagick的版本(64位)不一致,弄到晚上12點多,總算了發現了這個問題。。。

4、然后,接下來繼續用搜索引擎搜,找到很多stackoverflow的問題帖子,發現了2個代碼,不過要先下載PyPDF2以及ghostscript模塊。

先通過pip來安裝 PyPDF2、PythonMagick、ghostscript 模塊。

C:/Users/Administrator>pip install PyPDF2Collecting PyPDF2 Using cached PyPDF2-1.25.1.tar.gzInstalling collected packages: PyPDF2 Running setup.py install for PyPDF2Successfully installed PyPDF2-1.25.1You 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.whlProcessing c:/pythonmagick-0.9.10-cp27-none-win_amd64.whlInstalling collected packages: PythonMagickSuccessfully installed PythonMagick-0.9.10You 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 ghostscriptCollecting ghostscript Downloading ghostscript-0.4.1.tar.bz2Requirement already satisfied (use --upgrade to upgrade): setuptools in c:/python27/lib/site-packages (from ghostscript)Installing collected packages: ghostscript Running setup.py install for ghostscriptSuccessfully installed ghostscript-0.4.1You 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 osimport ghostscriptfrom PyPDF2 import PdfFileReader, PdfFileWriterfrom tempfile import NamedTemporaryFilefrom PythonMagick import Imagereader = 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 sysimport PyPDF2import PythonMagickimport ghostscriptpdffilename = "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'

然后執行時都報錯了,這個是 代碼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) +']') 這一行報錯。

于是,根據報錯的信息在網上查,但是沒查到什么有用的信息,但是感覺應該和GhostScript有關,于是在網上去查安裝包,找到一個在github上的下載連接,但是點進去的時候顯示無法下載。

最后,在csdn的下載中找到了 這個文件:GhostScript_Windows_9.15_win32_win64,安裝了64位版本,之后,再次運行上面的代碼,都能用了。

不過代碼2需要做如下修改,不然還是會報 No such file or directory @ error/pdf.c/ReadPDFImage/713 錯誤:

#代碼2import sysimport PyPDF2import PythonMagickimport ghostscriptpdffilename = "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'

這次有個很深刻的體會,就是解決這個問題過程中,大部分時間都是用在查資料、驗證資格資料是否有用上了,搜索資料的能力很重要。

而在實際搜索資料的過程中,國內關于PythonMagick的文章太少了,搜索出來的大部分有幫助的文章都是國外的,但是這些國外的帖子文章,也沒有解決我的問題或者是給出有用的線索,最后還是通過自己的思考,解決了問題。

更多關于Python相關內容感興趣的讀者可查看本站專題:《Python圖片操作技巧總結》、《Python數據結構與算法教程》、《Python Socket編程技巧總結》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 四会市| 饶平县| 全椒县| 白沙| 阳曲县| 乐山市| 汉中市| 长汀县| 金乡县| 务川| 神池县| 枣强县| 中超| 平山县| 陕西省| 壶关县| 西安市| 泸西县| 美姑县| 乐山市| 昌都县| 常州市| 东乌珠穆沁旗| 茶陵县| 麦盖提县| 含山县| 封开县| 仪征市| 彭山县| 台东市| 太康县| 霍林郭勒市| 玉屏| 通城县| 乌兰察布市| 赤壁市| 奎屯市| 日土县| 普安县| 西乡县| 余江县|