Python Svgwrite和字体样式/大小
我正在尝试将SVG文件连接到web刮板 如何使用svgwrite更改字体和文本大小?我知道我必须定义CSS样式并以某种方式将其连接到文本对象。但这是如何制作的 这是我目前掌握的代码Python Svgwrite和字体样式/大小,python,css,svg,svgwrite,Python,Css,Svg,Svgwrite,我正在尝试将SVG文件连接到web刮板 如何使用svgwrite更改字体和文本大小?我知道我必须定义CSS样式并以某种方式将其连接到文本对象。但这是如何制作的 这是我目前掌握的代码 import svgwrite svg_document = svgwrite.Drawing(filename = "test-svgwrite3.svg", size = ("1200px", "800px")) #This is the line I'm
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()
答案很简单,
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"))
SvgWrite的制造商曼弗雷德·莫伊茨(Manfred Moitzi)给我寄来了一封更加详细的回信 这必须通过CSS来完成,使用“class_uz”或“style”关键字args来设置文本属性:
dwg = svgwrite.Drawing()
使用“style”关键字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)
使用“class_u2;”关键字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)
创建包含以下内容的CSS文件:
.myclass {
font-size:30;
font-family:Comic Sans MS, Arial;
font-weight:bold;
font-style:oblique;
stroke:black;
stroke-width:1;
fill:none;
}
参见CSS参考:
使用“class_uu”和“style”关键字参数,您可以设置每个图形对象的样式,并且它们可以用于容器对象。如下设置字体大小:
dwg = svgwrite.Drawing('test.svg', profile='tiny')
dwg.add(dwg.text('Test',insert = (30, 55),font_size="10px",fill='black'))
这似乎只适用于“完整”配置文件(而不是“微小”)。这仍然是一个有价值的答案。谢谢