cairosvg - svg 转换为 png/pdf
关于 cairosvg
Convert your SVG files to PDF and PNG.
- a SVG 1.1 to PNG, PDF, PS and SVG converter;
- a command-line interface;
- a Python 3.6+ library;
- known to work at least on Linux, OS X, and Windows;
- based on the Cairo 2D graphics library;
- tested using the W3C test suite;
- LGPLv3-licensed free software.
安装
(base) $ sudo pip install cairosvg
终端中使用
$ cairosvg image.svg -o image.png
简单实用示例
import cairosvg
svg_path = 'a.svg'
png_path = 'a.png'
cairosvg.svg2png(url=svg_path, write_to=png_path)
批量转换
import os
path = "/Users/xx/Documents/001"
for file in os.listdir(path):
path = ''
path = os.path.join(path, file)
# print(path)
if file.endswith('.svg'):
png_path = path.replace('.svg', '.png')
print(path, png_path)
cairosvg.svg2png(url=path, write_to=png_path)
else:
print(path)
转换为 pdf
>>> import cairosvg
>>> cairosvg.svg2pdf(url='image.svg', write_to='image.pdf')