Home:ALL Converter>Python Svgwrite and font styles/ sizes

Python Svgwrite and font styles/ sizes

Ask Time:2013-06-16T03:20:11         Author:Peter Muungano

Json Formatter

I'm trying to make a SVG file connected to a web scraper.

How do I change font and text size with svgwrite? I understand that I have to define a CSS style and somehow connect that to the text object. But how is this made?

Here's the code I have so far

import svgwrite

svg_document = svgwrite.Drawing(filename = "test-svgwrite3.svg",
                            size = ("1200px", "800px"))
#This is the line I'm stuck at
#svg_document.add(svg_document.style('style="font-family: Arial; font-size  : 34;'))

svg_document.add(svg_document.rect(insert = (900, 800),
                                   size = ("200px", "100px"),
                                   stroke_width = "1",
                                   stroke = "black",
                                   fill = "rgb(255,255,0)"))

svg_document.add(svg_document.text("Reported Crimes in Sweden",
                                   insert = (410, 50),
                                   fill = "rgb(255,255,0)",

                                  #This is the connection to the first line that I'm stuck at
                                  #style = 'style="font-family: Arial; font-size  : 104;'))

print(svg_document.tostring())

svg_document.save()

Author:Peter Muungano,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/17127083/python-svgwrite-and-font-styles-sizes
Peter Muungano :

Manfred Moitzi the maker of SvgWrite mailed me an more eleborated answer;\n\nThis has to be done by CSS, use the 'class_' or 'style' keyword args to set text properties:\n\ndwg = svgwrite.Drawing()\n\n\nwith 'style' keyword arg:\n\ng = dwg.g(style=\"font-size:30;font-family:Comic Sans MS, Arial;font-weight:bold;font-\nstyle:oblique;stroke:black;stroke-width:1;fill:none\")\n\ng.add(dwg.text(\"your text\", insert=(10,30))) # settings are valid for all text added to 'g'\ndwg.add(g)\n\n\nwith 'class_' keyword arg:\n\nCreate a CSS file with content:\n\n.myclass {\nfont-size:30;\nfont-family:Comic Sans MS, Arial;\nfont-weight:bold;\nfont-style:oblique;\nstroke:black;\nstroke-width:1;\nfill:none;\n}\n\n\nsee CSS reference: http://www.w3schools.com/cssref/default.asp\n\ndwg.add_stylesheet(filename, title=\"sometext\") # same rules as for html files\n\ng = dwg.g(class_=\"myclass\")\ng.add(dwg.text(\"your text\", insert=(10,30))) # settings are valid for all text added to 'g'\ndwg.add(g)\n\n\nWith 'class_' and 'style' keyword args you can style every graphic object and they can be used at container objects.",
2013-06-18T15:01:24
yy