Weasyprint在调用write_pdf时出现未定义的属性:"AttributeError: 'PosixPath' object has no attribute 'read_text'"。

8

我将在Ubuntu 18.04上运行Weasyprint项目,并尝试创建PDF文件。

当我尝试设置页脚图像时,问题出现了。我的Python版本是3.6.7。

这是我调用Weasyprint的代码:

import sys
import os
from weasyprint import HTML, CSS

htmlFile = sys.argv[1]
pdfFile = sys.argv[2]

html = HTML(filename=htmlFile)

css = CSS(string='@page { @bottom-center { width: 125%; margin-top: 10px; content: " "; background-image: url(file://' + os.getcwd() + '/pdf/footer.png); background-repeat: no-repeat; background-position: right;  background-size: 100%; } }')

html.write_pdf(pdfFile, stylesheets=[css])

但是我遇到了以下错误:

Error: Command failed: python3 /node_modules/my-project/python/run.py /node_modules/my-project/pdf/catalog_01e299a3-1fca-482b-a512-e1bea832559f.html /node_modules/my-project/pdf/catalog_01e299a3-1fca-482b-a512-e1bea832559f.pdf
Traceback (most recent call last):
File "/node_modules/my-project/python/run.py", line 3, in <module>
    from weasyprint import HTML, CSS
File "/node_modules/my-project/python/weasyprint/__init__.py", line 394, in <module>
    from .css import preprocess_stylesheet  # noqa
File "/node_modules/my-project/python/weasyprint/css/__init__.py", line 25, in <module>
    from . import computed_values
File "/node_modules/my-project/python/weasyprint/css/computed_values.py", line 17, in <module>
    from .. import text
File "/node_modules/my-project/python/weasyprint/text.py", line 14, in <module>
    import cairocffi as cairo
File "/node_modules/my-project/python/cairocffi/__init__.py", line 19, in <module>
    VERSION = __version__ = (Path(__file__).parent / 'VERSION').read_text().strip()
AttributeError: 'PosixPath' object has no attribute 'read_text'
2个回答

11
我在这里得到了一些帮助后解决了问题: https://github.com/Kozea/WeasyPrint/issues/807 以下是问题概述:
在我的 Python 项目中,我有一个 requirements.txt 文件,并将 pathlib 作为依赖项。不知何故,尽管 python 版本高于等于 3.5 的当前库已安装,但 pathlib 覆盖了当前库。 当我删除此依赖项时,问题得以解决。

-2

我在尝试通过pip install安装软件包时遇到了同样的错误,但是我的项目确实需要pathlib,所以我不能简单地删除依赖项。

我在这里找到了解决方案。

基本上,你必须运行pip3而不是pip


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接