尝试使此代码正常工作:

import cairosvg
import os

path = "D:/PyProjects/Bla/Temp"
os.chdir(path)

cairosvg.svg2pdf(url='Pic.svg', write_to='image.pdf')

但是会收到类似于post的错误:
Traceback (most recent call last):
  File "D:/work/lean_python/pdf/other.py", line 2, in <module>
    import cairosvg
  File "D:\env_python352\lib\site-packages\cairosvg\__init__.py", line 29, in <module>
    from . import surface
  File "D:\env_python352\lib\site-packages\cairosvg\surface.py", line 24, in <module>
    import cairocffi as cairo
  File "D:\env_python352\lib\site-packages\cairocffi\__init__.py", line 46, in <module>
    cairo = dlopen(ffi, 'cairo', 'cairo-2')
  File "D:\env_python352\lib\site-packages\cairocffi\__init__.py", line 43, in dlopen
    raise OSError("dlopen() failed to load a library: %s" % ' / '.join(names))
OSError: dlopen() failed to load a library: cairo / cairo-2

该帖子提到:
CairoSVG (the python library and bindings) needs Cairo (The C library, part of GTK+) to run. It appears you don't have it an it's headers installed on your system.

因此,我按照步骤1-5描述了here。我现在在以下位置有cairo头文件:
C:\msys64\mingw64\include\cairo

我还安装了另一个来源推荐的pycairo:
pip install pycairo-1.15.2-cp36-cp36m-win_amd64.whl

我仍然收到上述错误。有任何想法吗?

最佳答案

以下变通办法适用于我:

  • 安装cairosvg(python -m pip install cairosvg)
  • 在脚本中运行import cairosvg
  • (如果有效),您已设置好。否则(OSError: no library called "cairo" was found):
  • 获得libcairo-2.dll的副本
  • 我通过从uniconvertor-2.0rc4-win64_headless.msi安装https://sk1project.net/uc2/download/
  • 做到了这一点
  • 然后查找libcairo-2.dll的安装位置。
  • 说路径是C:\path\cairo\dlls\libcairo-2.dll
  • 将脚本中的
  • 添加到顶部(在导入cairosvg之前)
  • import osos.environ['path'] += r';C:\path\cairo\dlls'
  • import cairosvg现在应该成功并且可以工作。

  • (假设您正在运行64位版本的Python,否则请使用win32_headless.msi)

    关于python - 使cairosvg在Windows中工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46265677/

    10-11 02:41