How can i fix this cairosvg error in result to a python code?

184 views Asked by At

Hello I tried running this code:

import os
import cairosvg

def convert_svg_to_png(input_folder, output_folder):
    # Create the output folder if it doesn't exist
    if not os.path.exists(output_folder):
    os.makedirs(output_folder)

    # Loop through all files in the input folder
    for filename in os.listdir(input_folder):
        # Check if the file is an SVG file
        if filename.endswith(".svg"):
            # Create the input and output file paths
            input_path = os.path.join(input_folder, filename)
            output_path = os.path.join(output_folder, os.path.splitext(filename)[0] + 
    ".png")

        # Convert the SVG to PNG
        cairosvg.svg2png(url=input_path, write_to=output_path)
        print(f"Converted {filename} to {os.path.basename(output_path)}")

if __name__ == "__main__":
    # Specify your input and output folders
    input_folder = r'C:\Users\Utente\Desktop\Underwear - Copia'
    output_folder = r'C:\Users\Utente\Desktop\Grande'

    # Call the function to convert SVG to PNG
    convert_svg_to_png(input_folder, output_folder)



print("FINITO")

Having installed:

C:\Users\Utente>pip install cairosvg
Collecting cairosvg
  Using cached CairoSVG-2.7.1-py3-none-any.whl (43 kB)
Requirement already satisfied: cairocffi in c:\users\utente\appdata\local\programs\python\python311\lib\site-packages (from cairosvg) (1.6.1)
Requirement already satisfied: cssselect2 in c:\users\utente\appdata\local\programs\python\python311\lib\site-packages (from cairosvg) (0.7.0)
Requirement already satisfied: defusedxml in c:\users\utente\appdata\local\programs\python\python311\lib\site-packages (from cairosvg) (0.7.1)
Requirement already satisfied: pillow in c:\users\utente\appdata\local\programs\python\python311\lib\site-packages (from cairosvg) (9.4.0)
Requirement already satisfied: tinycss2 in c:\users\utente\appdata\local\programs\python\python311\lib\site-packages (from cairosvg) (1.2.1)
Requirement already satisfied: cffi>=1.1.0 in c:\users\utente\appdata\local\programs\python\python311\lib\site-packages (from cairocffi->cairosvg) (1.15.1)
Requirement already satisfied: webencodings in c:\users\utente\appdata\local\programs\python\python311\lib\site-packages (from cssselect2->cairosvg) (0.5.1)
Requirement already satisfied: pycparser in c:\users\utente\appdata\local\programs\python\python311\lib\site-packages (from cffi>=1.1.0->cairocffi->cairosvg) (2.21)
Installing collected packages: cairosvg
Successfully installed cairosvg-2.7.1

But it returned this error:

Traceback (most recent call last):
  File "C:/Users/Utente/AppData/Local/Programs/Python/Python311/dawdwadwadwadadwadwadwadwad.py", line 2, in <module>
    import cairosvg
  File "C:\Users\Utente\AppData\Local\Programs\Python\Python311\Lib\site-packages\cairosvg\__init__.py", line 26, in <module>
    from . import surface  # noqa isort:skip
  File "C:\Users\Utente\AppData\Local\Programs\Python\Python311\Lib\site-packages\cairosvg\surface.py", line 9, in <module>
    import cairocffi as cairo
  File "C:\Users\Utente\AppData\Local\Programs\Python\Python311\Lib\site-packages\cairocffi\__init__.py", line 47, in <module>
    cairo = dlopen(
  File "C:\Users\Utente\AppData\Local\Programs\Python\Python311\Lib\site-packages\cairocffi\__init__.py", line 44, in dlopen
    raise OSError(error_message)  # pragma: no cover
OSError: no library called "cairo-2" was found
no library called "cairo" was found
no library called "libcairo-2" was found
cannot load library 'libcairo.so.2': error 0x7e.  Additionally, ctypes.util.find_library() did not manage to locate a library called 'libcairo.so.2'
cannot load library 'libcairo.2.dylib': error 0x7e.  Additionally, ctypes.util.find_library() did not manage to locate a library called 'libcairo.2.dylib'
cannot load library 'libcairo-2.dll': error 0x7e.  Additionally, ctypes.util.find_library() did not manage to locate a library called 'libcairo-2.dll'

I udnerstand there is a problem locating cairosvg maybe, but I don't know how to fix it.

0

There are 0 answers