AttributeError:“Ghostscript”对象没有属性“_instance”

Posted

技术标签:

【中文标题】AttributeError:“Ghostscript”对象没有属性“_instance”【英文标题】:AttributeError: 'Ghostscript' object has no attribute '_instance' 【发布时间】:2021-11-23 09:14:58 【问题描述】:

在我的大学学期项目中,我们尝试在一些 PDF 文件上使用 ghostscript,但是当我们尝试运行我们的代码时,我们得到了错误:

AttributeError: 'Ghostscript' object has no attribute '_instance'

我们已尝试各种尝试来解决此问题,但尚未找到解决方案。我们使用 ghostscript 的唯一部分是以下代码:

ar = ["-sDEVICE=pdfwrite", "-dPDFSETTINGS=/prepress", "-dQUIET", "-dBATCH", "-dNOPAUSE", "-dPDFSETTINGS=/printer", "-sOutputFile=" + os.path.join(filepath, file), os.path.join(filepath, file)]
gs = ghostscript.Ghostscript(*ar)
del gs

我们正在使用 Python-3.8 和 PyPi Ghostscript 0.7。

有没有其他人遇到过这个错误或者有谁知道如何解决它?

【问题讨论】:

没有 Ghostscript 0.7 版。当前版本是 9.55.0,1.0 版本是在 1980 年代发布的,在 Python 之前,所以我非常怀疑您使用的是“Ghostscript 版本 0.7”。我怀疑您正在为 Ghostscript 'python-ghostscript' 使用 PyPI Python 绑定(在 0.7 版中 is)。你真的安装了 Ghostscript 吗?如果库不存在,绑定对你没有任何好处。 是的,这是正确的,我使用的是 PyPi ghostscript 0.7,抱歉。我已经安装了 Ghostscript 是的。我相信我刚刚找到了解决方案。显然,给出参数的顺序很重要,因此通过将“-sDEVICE”和“-dPDFSETTINGS”移到“-dNOPAUSE”后面,它可以工作。 对于完全没有区别的普通 Ghostscript,显然我不知道 Python 胶水代码在做什么。 【参考方案1】:

显然,传递参数的顺序很重要。所以不要有:

ar = ["-sDEVICE=pdfwrite", "-dPDFSETTINGS=/prepress", "-dQUIET", "-dBATCH", "-dNOPAUSE", "-dPDFSETTINGS=/printer", "-sOutputFile=" + os.path.join(filepath, file), os.path.join(filepath, file)]

我们现在有:

ar = ["-dQUIET", "-dBATCH", "-dNOPAUSE", "-sDEVICE=pdfwrite", "-dPDFSETTINGS=/prepress", "-dPDFSETTINGS=/printer", "-sOutputFile=" + os.path.join(filepath, file), os.path.join(filepath, file)]

这为我们解决了问题。

【讨论】:

您不能拥有两次 PDFSETTINGS(就像您拥有的那样)。后者覆盖前者。除此之外,订单的重要性微乎其微,但您所做的更改应该没有什么区别。当然,我不能说 Python 接口代码可能在做什么。

以上是关于AttributeError:“Ghostscript”对象没有属性“_instance”的主要内容,如果未能解决你的问题,请参考以下文章

AttributeError:“字节”对象没有属性“告诉”

AttributeError: 'RDD' 对象没有属性 'show'

AttributeError:“NumpyArrayIterator”对象没有属性“类”

AttributeError:模块 'dbus' 没有属性 'lowlevel'

AttributeError:模块'keras'没有属性'initializers'

AttributeError:“会话”对象没有属性“会话”