我尝试使用 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>.
我尝试了几件事,但不知道为什么它不起作用。
非常感谢!