2015-12-07 53 views
0

我試圖找到一個簡單的python代碼,將數百個pdf文件轉換爲jpg文件到pdf文件所在的同一個文件夾。我用這個代碼Python Wand converts from PDF to JPG background is incorrect使用python轉換pdf到jpg 2.7-錯誤

from wand.image import Image 
from wand.color import Color 
import os, os.path, sys 

def pdf2jpg(source_file, target_file, dest_width, dest_height): 
RESOLUTION = 300 
ret = True 
try: 
    with Image(filename=source_file, resolution=(RESOLUTION,RESOLUTION)) as img: 
     img.background_color = Color('white') 
     img_width = img.width 
     ratio  = dest_width/img_width 
     img.resize(dest_width, int(ratio * img.height)) 
     img.format = 'jpeg' 
     img.save(filename = target_file) 
except Exception as e: 
    ret = False 

return ret 

if __name__ == "__main__": 
source_file = r"C:\Users\yaron.KAYAMOT\Desktop\aaa.pdf" 
target_file = r"C:\Users\yaron.KAYAMOT\Desktop\aaa.jpg" 

ret = pdf2jpg(source_file, target_file, 1895, 1080) 

,但我得到一個錯誤:

ImportError: MagickWand shared library not found. 
You probably had not installed ImageMagick library. 
Try to install: 
    http://docs.wand-py.org/en/latest/guide/install.html#install-imagemagick-on-windows 
>>> 

,但我有在硬盤模塊MagickWand如在cmd: enter image description here

更新: 當我嘗試點擊安裝在cmd「魔杖」模塊我得到: enter image description here

所以,我確實有這個模塊。當我嘗試點擊安裝imagemagick \ ImageMagick我得到: enter image description here

回答

0

您從wand模塊進口。 您可能還沒有安裝Python的綁定。

pip install Wand 

看到細節在這裏:http://docs.wand-py.org/en/0.4.2/

也儘量做到以下幾點:

pip search pythonmagick 

或類似的東西。嘗試安裝所有必需的軟件包。 This可能會幫助你。