我尝试使用minidom从Python中的SVG中提取一个路径,然后希望使用svgwrite创建一个只包含第一个路径的新SVG。
这是我的代码,用于提取第一个路径及其颜色:
from xml.dom import minidom
# Read svg as xml document
doc = minidom.parse('Target_logo.svg')
# extract path coordinates
path_strings = [path.getAttribute('d') for path
in doc.getElementsByTagName('path')]
# extract path colors
path_colors = [path.getAttribute('fill') for path
in doc.getElementsByTagName('path')]
display(path_strings[0])
display(path_colors[0])
输出:
'M217.074,360.93c-71.239,0-129.052-57.721-129.052-128.972c0-71.38,57.813-129.059,129.052-129.059 c71.301,0,129.042,57.679,129.042,129.059C346.116,303.209,288.375,360.93,217.074,360.93 M217.074,38.459 c-106.796,0-193.399,86.625-193.399,193.499c0,106.792,86.603,193.39,193.399,193.39c106.842,0,193.581-86.598,193.581-193.39 C410.655,125.084,323.916,38.459,217.074,38.459z'
'#CC0000'
这是我创建新svg的代码(我用逗号替换了换行符中的空格,这不会导致错误(:
# Create new svg with only one shape
import svgwrite
d1 = path_strings[0].replace(" ", ",")
fill1 = path_colors[0]
svg1 = svgwrite.Drawing('test.svg')
svg1.add(svg1.path(d=d1, fill=fill1))
svg1.save()
第二个代码给了我以下错误:
TypeError: 'M217.074,360.93c-71.239,0-129.052-57.721-129.052-128.972c0-71.38,57.813-129.059,129.052-129.059,c71.301,0,129.042,57.679,129.042,129.059C346.116,303.209,288.375,360.93,217.074,360.93 M217.074,38.459,c-106.796,0-193.399,86.625-193.399,193.499c0,106.792,86.603,193.39,193.399,193.39c106.842,0,193.581-86.598,193.581-193.39,C410.655,125.084,323.916,38.459,217.074,38.459z' is not a valid value for attribute 'd' at svg-element <path>.
我试了好几种方法,但不知道为什么不起作用。
非常感谢!
根据SVG规范,数字之间只允许使用逗号,因此
-193.39,C410.655
无效。