【问题标题】:Python Svgwrite and font styles/ sizesPython Svgwrite 和字体样式/大小
【发布时间】:2013-06-12 04:48:51
【问题描述】:

我正在尝试将 SVG 文件连接到网络爬虫。

如何使用 svgwrite 更改字体和文本大小?我知道我必须定义一个 CSS 样式并以某种方式将其连接到文本对象。但是这是怎么做的呢?

这是我目前的代码

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()

【问题讨论】:

    标签: python css svg svgwrite


    【解决方案1】:

    SvgWrite 的制造者 Manfred Moitzi 给我邮寄了一个更详细的答案;

    这必须由 CSS 完成,使用 'class_' 或 'style' 关键字参数来设置文本属性:

    dwg = svgwrite.Drawing()
    

    带有'style'关键字参数:

    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_”关键字参数:

    创建一个包含内容的 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 参考: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)
    

    使用“class_”和“style”关键字参数,您可以设置每个图形对象的样式,并且可以在容器对象中使用它们。

    【讨论】:

      【解决方案2】:

      答案很简单;

      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"))
      

      【讨论】:

      • 这似乎只适用于“完整”配置文件(而不是“小”配置文件)。仍然是一个有价值的答案。谢谢。
      【解决方案3】:

      像这样设置字体大小:

      dwg = svgwrite.Drawing('test.svg', profile='tiny')
      dwg.add(dwg.text('Test',insert = (30, 55),font_size="10px",fill='black'))
      

      【讨论】:

        猜你喜欢
        • 2016-07-20
        • 1970-01-01
        • 2013-10-10
        • 1970-01-01
        • 2013-02-06
        • 2020-02-29
        • 1970-01-01
        • 2016-06-07
        • 1970-01-01
        相关资源
        最近更新 更多