Python SVG parser

前端 未结 3 2038
北恋
北恋 2020-12-14 08:32

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 eventua

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-14 08:50

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

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

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

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

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

提交回复
热议问题