本文實例講述了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.
新聞熱點
疑難解答