Python将pdf转换为图像?

时间:2016-05-18 12:28:15

标签: python pdf wand

所以我正在使用Wand尝试将pdf转换为图像

from wand.image import Image

with Image(filename="test.pdf") as img:
     img.save(filename="/temp.jpg")

with Image(filename="test.jpg") as img:
     img.resize(200, 150)
     img.save(filename="t.jpg")

但出于某种原因,我得到了:

Traceback (most recent call last):
  File "C:\Users\Rafael\Desktop\k\pdf to image.py", line 3, in <module>
    with Image(filename="test.pdf") as img:
  File "C:\Python27\lib\site-packages\wand\image.py", line 2534, in __init__
    self.read(filename=filename, resolution=resolution)
  File "C:\Python27\lib\site-packages\wand\image.py", line 2601, in read
    self.raise_exception()
  File "C:\Python27\lib\site-packages\wand\resource.py", line 222, in raise_exception
    raise e
DelegateError: PDFDelegateFailed `The system cannot find the file specified.

' @ error/pdf.c/ReadPDFImage/798

我可以做某事或有其他方法将pdf转换为图像吗?

3 个答案:

答案 0 :(得分:2)

安装Ghostscript并添加

C:\Program Files (x86)\gs\gs9.06\bin

您的systempath应该有助于解决此问题,因为它帮助我克服了该错误。...

答案 1 :(得分:1)

原因可能是缺少ghostscript安装。这是一个类似的question on SO

答案 2 :(得分:0)

还有一种方法。看起来您正在使用 Windows,因此您需要下载“https://github.com/oschwartz10612/poppler-windows/releases/” 因为您使用的是 Windows,所以您需要说明 poppler/bin 文件夹的路径: 代码:

from pdf2image import convert_from_path

poppler_path = "C:\path\to\poppler\bin"

images = convert_from_path('example.pdf')

for i in range(len(images)):

  
    images[i].save('page'+ str(i) +'.jpg', 'JPEG')

参考:https://www.geeksforgeeks.org/convert-pdf-to-image-using-python/