Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

Python svgwrite Drawing Error "not a valid value for attribute 'd' at svg-element <path>"

I try to extract one path from a SVG in Python using minidom and then want to create a new SVG with just the first path using svgwrite.

This is my code for extracting the first path with its color:

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])

Output:

'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'

This is my code for creating a new svg (I replaced the spaces from linebreaks with commas, this should not cause the error):

# 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()

The second code gives me the following error:

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>.

I tried several things but don't know why it is not working.

Thank you very much!

question from:https://stackoverflow.com/questions/65643424/python-svgwrite-drawing-error-not-a-valid-value-for-attribute-d-at-svg-elemen

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

According to the SVG specification commas are only permitted between numbers so

-193.39,C410.655

is invalid.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...