首页 > 解决方案 > 将 pdf 转换为图像时出现 Ghostscript 错误

问题描述

我正在尝试阅读 PDF 并写入图像。那里没有很多文档,但我尝试了以下示例:

import ghostscript
import locale

def pdf2jpeg(pdf_input_path, jpeg_output_path):
    args = ["pdf2jpeg", # actual value doesn't matter
            "-dNOPAUSE",
            "-sDEVICE=jpeg",
            "-r144",
            "-sOutputFile=" + jpeg_output_path, pdf_input_path]

    encoding = locale.getpreferredencoding()
    args = [a.encode(encoding) for a in args]
    ghostscript.Ghostscript(*args)

pdf2jpeg('input.pdf', 'output.jpeg')

我得到了错误:

partially initialized module 'ghostscript' has no attribute 'Ghostscript' (most likely due to a circular import)

为了安装,我这样做了,它安装了最新的 0.6 版。

pip install ghostscript

根据文档,我有要求(我正在运行 Python 3.8 Anaconda Spyder)

https://pypi.org/project/ghostscript/

要检查可用的方法:

dir(ghostscript)
Out[30]: 
['__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 'ghostscript',
 'locale',
 'pdf2jpeg',
 'sys']

还有什么我需要的吗?

标签: python-3.ximagepdfghostscript

解决方案


这是因为我将脚本命名为 ghostscript.py。我只需要重命名它。


推荐阅读