get cairosvg working in windows
Asked Answered
D

6

10

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?

Dorking answered 17/9, 2017 at 15:18 Comment(0)
D
5

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")
Dorking answered 17/9, 2017 at 17:48 Comment(2)
this doesn't work if svg has transparent backgroundDupont
I couldn't get this snippet to work for me on Windows. Kept getting strange missing font errors. Many, many missing fonts.Disentomb
I
11

The following workaround works for me:

  • install cairosvg (python -m pip install cairosvg)
  • run import cairosvg in a script.
  • if it works, you're set. otherwise (OSError: no library called "cairo" was found):
  • get a copy of libcairo-2.dll
  • say the path is C:\path\cairo\dlls\libcairo-2.dll
  • in your script add to the top (before import cairosvg)

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)

Incidental answered 14/2, 2020 at 6:3 Comment(2)
GIMP also uses libcairo-2.dll, so the path was C:\Program Files\GIMP 2\32\bin for me. Thanks!Marieann
This worked for me! thanks =) Inkscape also uses libcairo-2.dll. os.environ['path'] += r';C:\Program Files\Inkscape\bin'Konstanze
D
5

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")
Dorking answered 17/9, 2017 at 17:48 Comment(2)
this doesn't work if svg has transparent backgroundDupont
I couldn't get this snippet to work for me on Windows. Kept getting strange missing font errors. Many, many missing fonts.Disentomb
Z
3

I adopted a different way but discovered in the process a fix that allowed cairosvg to work on Windows 11 and Python 3.10. Even with the GTK3 Runtime Win64 installed the dll could not be loaded and generated an error. This resolved the issue. Add this code before import cairosvg. Source of idea was jcupitt, https://github.com/libvips/pyvips:

    import os
    gtkbin = r'C:\Program Files\GTK3-Runtime Win64\bin'
    add_dll_dir = getattr(os, 'add_dll_directory', None)
    if callable(add_dll_dir):
        add_dll_dir(gtkbin)
    else:
        os.environ['PATH'] = os.pathsep.join((gtkbin, os.environ['PATH']))

    import cairosvg
    cairosvg.svg2pdf(url='banana-coloured.svg', write_to='image.pdf')
Zebulon answered 23/3, 2023 at 21:37 Comment(1)
import os os.add_dll_directory(r"C:\Program Files\GTK3-Runtime Win64\bin")Dialyze
M
1

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
Mcminn answered 31/7, 2018 at 5:38 Comment(0)
S
1

You can first install cairocffi package first using a built binary package, then install cairosvg. That will resolve the problem.

https://www.lfd.uci.edu/~gohlke/pythonlibs/

Specialism answered 20/6, 2022 at 8:0 Comment(1)
P
0

The following worked for me: I had GIMP for Windows installed in my Program Files folder. I copied the bin folder in to cairo folder in the root of my folder and then added it to path before importing the cairo package in Python.

import os

folder_path = os.path.abspath("./cairo/bin/")

path_env_var = os.environ["PATH"]

if folder_path not in path_env_var:
    os.environ["PATH"] = folder_path + os.pathsep + path_env_var

import cairosvg
Parathion answered 4/4, 2023 at 8:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.