Setting default units in svg (Python svgwrite)
Asked Answered
O

2

6

I'm generating SVG drawings using python's svgwrite. Every time I want to draw something, I find myself doing this ugly awkward thing:

line = drawing.line(start = "%dmm" % start, end = "%dmm" % end)

I wish I could just do:

line = drawing.line(start = start, end = end)

Is there a way to set the default units to 'mm' for the entire svg drawing?

Onward answered 22/10, 2012 at 7:15 Comment(0)
W
15

A possible way is to set the viewBox attribute along with the document sizing,

dwg = svgwrite.Drawing('myDrawing.svg', size=('170mm', '130mm'), viewBox=('0 0 170 130'))

dwg.add(dwg.line(start=(30, 30), end=(50,50)))

dwg.save()

produces for me,

<?xml version="1.0" encoding="utf-8" ?>

<svg baseProfile="full" height="130mm" version="1.1" viewBox="0 0 170 130" width="170mm"
xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xlink="http://www.w3.org/1999/xlink"><defs /><line x1="30" x2="50" y1="30" y2="50" />     </svg>
Warwickshire answered 22/10, 2012 at 9:42 Comment(0)
S
4

I just found that you can do this:

from svgwrite import cm, mm
dwg = svgwrite.Drawing('my_drawing.svg', height='10cm', width='10cm')
dwg.add(dwg.line((0*cm, 0*cm), (10*cm, 10*cm))
dwg.save()
Sesquicarbonate answered 17/5, 2016 at 12:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.