最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年07月21日 09:02:54   作者:不想長大啊  
這篇文章主要介紹了windows下Python實現(xiàn)將pdf文件轉化為png格式圖片的方法,結合實例形式較為詳細的分析了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程序設計有所幫助。

相關文章

  • Python實現(xiàn)pdf轉word詳細代碼

    Python實現(xiàn)pdf轉word詳細代碼

    在日常工作中,我們經(jīng)常會遇到需要將PDF文件轉換成Word文件的需求。雖然市面上有許多PDF轉Word的工具,但是它們通常需要付費或者有轉換后的格式問題,這篇文章主要給大家介紹了關于Python實現(xiàn)pdf轉word的相關資料,需要的朋友可以參考下
    2023-09-09
  • Python 中閉包與裝飾器案例詳解

    Python 中閉包與裝飾器案例詳解

    這篇文章主要介紹了Python 中閉包與裝飾器案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Python基于OpenCV實現(xiàn)人臉檢測并保存

    Python基于OpenCV實現(xiàn)人臉檢測并保存

    這篇文章主要介紹了Python基于OpenCV實現(xiàn)人臉檢測并保存,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • 基于Python的OCR實現(xiàn)示例

    基于Python的OCR實現(xiàn)示例

    這篇文章主要介紹了基于Python的OCR實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-04-04
  • 如何在Python中將字符串轉換為數(shù)組詳解

    如何在Python中將字符串轉換為數(shù)組詳解

    最近在用Python,做一個小腳本,有個操作就是要把內(nèi)容換成數(shù)組對象再進行相關操作,下面這篇文章主要給大家介紹了關于如何在Python中將字符串轉換為數(shù)組的相關資料,需要的朋友可以參考下
    2022-12-12
  • Windows系統(tǒng)下cython_bbox庫的正確安裝步驟

    Windows系統(tǒng)下cython_bbox庫的正確安裝步驟

    Cython-bbox一般無法直接通過pip直接安裝,那么如何安裝呢?下面這篇文章主要給大家介紹了關于Windows系統(tǒng)下cython_bbox庫的正確安裝步驟,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2023-05-05
  • Pandas中`ValueError: cannot reindex from a duplicate axis`錯誤分析及解決辦法

    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)存使用率

    這篇文章主要為大家詳細介紹了python實現(xiàn)監(jiān)控指定進程的cpu和內(nèi)存使用率,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Python類方法__init__和__del__構造、析構過程分析

    Python類方法__init__和__del__構造、析構過程分析

    這篇文章主要介紹了Python類方法__init__和__del__構造、析構過程分析,本文分析了什么時候構造、什么時候析構、成員變量如何處理、Python中的共享成員函數(shù)如何訪問等問題,需要的朋友可以參考下
    2015-03-03
  • 從Pytorch模型pth文件中讀取參數(shù)成numpy矩陣的操作

    從Pytorch模型pth文件中讀取參數(shù)成numpy矩陣的操作

    這篇文章主要介紹了從Pytorch模型pth文件中讀取參數(shù)成numpy矩陣的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03

最新評論

宁海县| 横山县| 德阳市| 平陆县| 益阳市| 广元市| 白银市| 巴彦县| 乌恰县| 利川市| 富蕴县| 永德县| 邵武市| 玛沁县| 佛山市| 肃北| 甘谷县| 五河县| 鱼台县| 炉霍县| 筠连县| 马鞍山市| 米林县| 阜宁县| 邢台县| 大埔县| 北票市| 三台县| 营口市| 大安市| 山东省| 吉木乃县| 奉节县| 玉山县| 华亭县| 延寿县| 台北县| 长武县| 永嘉县| 桦南县| 车险|