1

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!

Hackerman
  • 35
  • 7

1 Answers1

3

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

-193.39,C410.655

is invalid.

Robert Longson
  • 118,664
  • 26
  • 252
  • 242
  • Thank you very much for your quick answer! I removed the commas next to letters but unfortunately I still get the same error. However, when I open an SVG with exact this path in Chrome it gets displayed correctly. Do you have any other idea what might cause this problem? – Hackerman Jan 09 '21 at 14:19
  • That's a known bug in Chrome that its parser doesn't quite follow the SVG specification. – Robert Longson Jan 09 '21 at 14:38