Skip to content

Commit 5f11ec7

Browse files
author
Ascari
committed
Added removeElement(<tagName>) and removeComments() methods
1 parent 77b3bf5 commit 5f11ec7

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

cleanSVG.py

+20
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,26 @@ def removeAttribute(self, attribute, exception_list=None):
258258
if self._verbose: print ' - Removed attribute: %s="%s"' % (attribute, element.attrib[attribute])
259259
del element.attrib[attribute]
260260

261+
def removeElement(self, tagName):
262+
""" Remove all instances of an element. """
263+
264+
if self._verbose: print '\nRemoving element: %s' % tagName
265+
266+
for element in self.tree.iter():
267+
if (isinstance(element.tag, basestring)):
268+
tag = element.tag.split('}')[1]
269+
if tag == tagName:
270+
element.getparent().remove(element)
271+
272+
def removeComments(self):
273+
""" Remove all comments. """
274+
275+
if self._verbose: print '\nRemoving comments'
276+
277+
for element in self.tree.iter():
278+
if element.tag is etree.Comment:
279+
element.getparent().remove(element)
280+
261281
def removeNonDefIDAttributes(self):
262282
""" Go through def elements and find IDs referred to, then remove all IDs except those. """
263283

example.py

+4
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@
88
svg.removeAttribute('id')
99
svg.setDecimalPlaces(1)
1010
svg.extractStyles()
11+
svg.removeElement('title')
12+
svg.removeElement('desc')
13+
svg.removeElement('defs')
14+
svg.removeComments()
1115
svg.applyTransforms()
1216
svg.write(output_file)

readme.md

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ Python program to clean up SVG files, particularly those created by Inkscape or
77
## Remove attributes
88
Remove attributes with a given name, e.g. remove 'id' attributes, which often aren't used.
99

10+
## Remove comments
11+
Removes all comments.
12+
13+
## Remove elements
14+
Remove elements by their tag name.
15+
1016
## Remove namespaces
1117
Remove all attributes associated with a given namespace, e.g. remove 'sodipodi' attributes created by Inkscape.
1218

0 commit comments

Comments
 (0)