Python Svgwrite and font styles/ sizes
Asked Answered
P

3

8

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()
Periclean answered 15/6, 2013 at 19:20 Comment(0)
P
12

Manfred Moitzi the maker of SvgWrite mailed me an more eleborated answer;

This has to be done by CSS, use the 'class_' or 'style' keyword args to set text properties:

dwg = svgwrite.Drawing()

with 'style' keyword arg:

g = dwg.g(style="font-size:30;font-family:Comic Sans MS, Arial;font-weight:bold;font-
style:oblique;stroke:black;stroke-width:1;fill:none")

g.add(dwg.text("your text", insert=(10,30))) # settings are valid for all text added to 'g'
dwg.add(g)

with 'class_' keyword arg:

Create a CSS file with content:

.myclass {
font-size:30;
font-family:Comic Sans MS, Arial;
font-weight:bold;
font-style:oblique;
stroke:black;
stroke-width:1;
fill:none;
}

see CSS reference: http://www.w3schools.com/cssref/default.asp

dwg.add_stylesheet(filename, title="sometext") # same rules as for html files

g = dwg.g(class_="myclass")
g.add(dwg.text("your text", insert=(10,30))) # settings are valid for all text added to 'g'
dwg.add(g)

With 'class_' and 'style' keyword args you can style every graphic object and they can be used at container objects.

Periclean answered 18/6, 2013 at 15:1 Comment(1)
is any css style allowed? I'm failing to obtain overflow:wrap-around behavior and am having to consider docs.python.org/3/library/textwrap.html ...Jacklin
P
7

The answer was quite simple;

svg_document.add(svg_document.text("Reported Crimes in Sweden",
                                   insert = (410, 50),
                                   fill = "rgb(255,255,0)",
                                   style = "font-size:10px; font-family:Arial"))
Periclean answered 17/6, 2013 at 21:5 Comment(1)
This only seems to work with the "full" profile (not "tiny"). Still a valuable answer. Thanks.Birdiebirdlike
J
3

Set font-size like this:

dwg = svgwrite.Drawing('test.svg', profile='tiny')
dwg.add(dwg.text('Test',insert = (30, 55),font_size="10px",fill='black'))
Jahdai answered 30/8, 2015 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.