Inkscape.org
Creating New Extensions Saving the SVG file after changes...
  1. #1
    snehith snehith @snehith

    Hi

    I have written an extension for adding an object to an existing SVG file, but I'm unable to save the file after the changes has been done. (using the same extension script)

    Can anyone help me with this? How can i save the SVG file using inkex?

    Thanks

  2. #2
    inklinea inklinea @inklinea⛰️
    *
    # Save the current svg contained in self
    with open('/home/name/Pictures/output_test0.svg', 'wb') as output_file:
        self.save(output_file)
    
    # Generic python method for saving any svg element - svg, path, rect etc
    # This useful if you want to output mutiple svgs - each different
    # For example with different stroke widths / colours etc.
    
    svg_copy = self.svg.copy()
    with open('/home/name/Pictures/output_test1.svg', 'w') as svg_file:
        svg_text = svg_copy.tostring().decode('utf-8')
        svg_file.write(svg_text)
    
    # Here we just find the first ellipse in the document and export as text
    
    with open('/home/name/Pictures/output_test2.svg', 'w') as svg_file:
        ellipse = self.svg.xpath('//svg:ellipse')[0]
        ellipse_text = ellipse.tostring().decode('utf-8')
        svg_file.write(ellipse_text)
    
    # This is a generic method for exporting and svg ( or svg copy )
    # pretty_print tries to make it look nicer
    
    from lxml import etree
    svg_filepath = '/home/name/Pictures/output_test3.svg'
    
    with open(svg_filepath, 'w') as svg_file:
        svg_text = etree.tostring(self.svg, pretty_print=True).decode('utf-8')
        svg_file.write(svg_text)

    You can also use:

    sys.exit()

    At the end of def effect - that will return to inkscape without updating the svg in the gui ignoring any changes you have made in the extensions system. So you can export without changing the current svg you are working on.

  3. #3
    snehith snehith @snehith

    @inklinea Really grateful for this, works perfectly!

    Thank you!!!

Inkscape Inkscape.org Inkscape Forum Creating New Extensions Saving the SVG file after changes...