Python SVG 解析器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15857818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:11:32  来源:igfitidea点击:

Python SVG parser

pythonxmlsvgxml-parsingcnc

提问by Sareena Avadhany

I want to parse an SVG file using python to extract coordinates/paths (I believe this is listed under the "path" ID, specifically the d="..."/>). This data will eventually be used to drive a 2 axis CNC.

我想使用 python 解析一个 SVG 文件来提取坐标/路径(我相信这列在“路径”ID 下,特别是 d="..."/>)。该数据最终将用于驱动 2 轴 CNC。

I've searched on SO and Google for libraries that can return the string of such paths so I can further parse it, but to no avail. Does such a library exist?

我在 SO 和 Google 上搜索了可以返回此类路径字符串的库,以便我可以进一步解析它,但无济于事。有这样的图书馆吗?

采纳答案by icktoofay

Ignoring transforms, you can extract the path strings from an SVG like so:

忽略转换,您可以从 SVG 中提取路径字符串,如下所示:

from xml.dom import minidom

doc = minidom.parse(svg_file)  # parseString also exists
path_strings = [path.getAttribute('d') for path
                in doc.getElementsByTagName('path')]
doc.unlink()

回答by AndyP

Getting the d-string can be done in a line or two using svgpathtools.

可以使用svgpathtools在一两行中获取 d-string 。

from svgpathtools import svg2paths
paths, attributes = svg2paths('some_svg_file.svg')

pathsis a list of svgpathtools Path objects (containing just the curve info, no colors, styles, etc.). attributesis a list of corresponding dictionary objects storing the attributes of each path.

路径是 svgpathtools 路径对象的列表(仅包含曲线信息,不包含颜色、样式等)。 attributes是存储每个路径的属性的相应字典对象的列表。

To, say, print out the d-strings then...

比如说,打印出 d-strings 然后......

for k, v in enumerate(attributes):
    print v['d']  # print d-string of k-th path in SVG

回答by Frank Buss

The question was about extracting the path strings, but in the end the line drawing commands were wanted. Based on the answer with minidom, I added the path parsing with svg.path to generate the line drawing coordinates:

问题是关于提取路径字符串,但最终需要画线命令。根据minidom的回答,我添加了svg.path的路径解析,生成画线坐标:

#!/usr/bin/python3
# requires svg.path, install it like this: pip3 install svg.path

# converts a list of path elements of a SVG file to simple line drawing commands
from svg.path import parse_path
from svg.path.path import Line
from xml.dom import minidom

# read the SVG file
doc = minidom.parse('test.svg')
path_strings = [path.getAttribute('d') for path
                in doc.getElementsByTagName('path')]
doc.unlink()

# print the line draw commands
for path_string in path_strings:
    path = parse_path(path_string)
    for e in path:
        if isinstance(e, Line):
            x0 = e.start.real
            y0 = e.start.imag
            x1 = e.end.real
            y1 = e.end.imag
            print("(%.2f, %.2f) - (%.2f, %.2f)" % (x0, y0, x1, y1))