svgwrite - How to enable write/add other objects a

2019-04-14 14:19发布

I have an svg file and I want to insert other svg object inside this existent.

import svgwrite

dwg = svgwrite.Drawing('model.svg')
square = dwg.add(dwg.rect(20,20),(80,80), fill='blue'))
dwg.save()

it returns a new file with this shape ignoring my previous file. How could I write this?

Thank's

标签: python svg
2条回答
趁早两清
2楼-- · 2019-04-14 15:16

The svgwrite library doesn't support this -- its purpose is to create new SVG files, not work with existing ones. Looking at the source for the Drawing class, you can see that when you save your drawing, it opens the file for writing and truncates; anything that was previously in that file is lost as a result:

def save(self):
    """ Write the XML string to **filename**. """
    fileobj = io.open(self.filename, mode='w', encoding='utf-8')
    self.write(fileobj)
    fileobj.close()
查看更多
劳资没心,怎么记你
3楼-- · 2019-04-14 15:23

I found a module to do that

import svgutils.transform as st

template = st.fromfile('template.svg')
second_svg = st.fromfile('second.svg')
template.append(second_svg)
template.save('merged.svg')

I hope that works for you too.

查看更多
登录 后发表回答