Trying to get this code working:
import cairosvg
import os
path = "D:/PyProjects/Bla/Temp"
os.chdir(path)
cairosvg.svg2pdf(url='Pic.svg', write_to='image.pdf')
but get errors along similar to this 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
The post mentions:
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.
So I followed step 1 - 5 described here. I now have cairo header files in:
C:\msys64\mingw64\include\cairo
I also installed pycairo recommended by another source:
pip install pycairo-1.15.2-cp36-cp36m-win_amd64.whl
I still get the above errors. Any ideas?
Please check the path of libcairo-2.dll
with the ctypes.util
.
In my case, It was a directory of old software called Graphviz
.
python
>>> import ctypes.util
>>> path = ctypes.util.find_library('libcairo-2')
>>> print(path)
C:\Program Files (x86)\Graphviz 2.28\bin\libcairo-2.dll
After uninstalling Graphviz
.
python
>>> import ctypes.util
>>> path = ctypes.util.find_library('libcairo-2')
>>> print(path)
C:\msys64\mingw64\bin\libcairo-2.dll
The following workaround works for me:
python -m pip install cairosvg
)import cairosvg
in a script.OSError: no library called "cairo" was found
):uniconvertor-2.0rc4-win64_headless.msi
https://sk1project.net/uc2/download/
libcairo-2.dll
was installed to.C:\path\cairo\dlls\libcairo-2.dll
import os
os.environ['path'] += r';C:\path\cairo\dlls'
import cairosvg
should now succeed and work.(Assumes you are running 64bit version of Python, otherwise use win32_headless.msi
)
I just do not get cairosvg to work. I found an alternative way to transform an svg into a png using the svglib package.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With