2017-09-17 83 views
0

努力試圖讓這個代碼的工作:得到cairosvg在Windows

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。我現在有開羅頭文件:

C:\msys64\mingw64\include\cairo 

我也裝pycairo的另一個來源建議:

pip install pycairo-1.15.2-cp36-cp36m-win_amd64.whl 

我仍然得到上述錯誤。有任何想法嗎?

回答

0

我只是沒有得到cairosvg的工作。我找到了一種使用svglib包將svg轉換爲png的替代方法。

from svglib.svglib import svg2rlg 
from reportlab.graphics import renderPDF, renderPM 
import os 

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

drawing = svg2rlg("Pic.svg") 
renderPM.drawToFile(drawing, "Pic.png")