python-3.x - 将 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']
还有什么我需要的吗?
解决方案
这是因为我将脚本命名为 ghostscript.py。我只需要重命名它。
推荐阅读
- javascript - 条件滚动条不适用于 chrome
- java - Open GL中体素模型的纹理映射
- javascript - 在反应 useState 挂钩中将对象推送到数组
- html - 在 Angular HTML 表中获取重复数据
- javascript - Webpack 导入的模块仍未定义
- google-apps-script - Google Scripts API 某些事件具有不同类型的事件 ID,它们将无法正确提取
- r - 按组和条件 data.table 编辑
- sql - Postgres 零长度分隔标识符在“”处或附近
- node.js - 从 Azure Blob 存储下载多个文件和文件夹 - node.js
- sql-server - 从字符串中删除前导字符