【问题标题】:underline text with odfpy用 odfpy 给文本加下划线
【发布时间】:2021-09-27 21:16:30
【问题描述】:

我想用 odfpy 生成一个 odf 文件,但卡在下划线文本上。 这是一个受官方文档启发的最小示例,在其中我找不到有关可以使用哪些属性以及在何处使用的任何信息。 有什么建议吗?

from odf.opendocument import OpenDocumentText
from odf.style import Style, TextProperties
from odf.text import H, P, Span

textdoc = OpenDocumentText()

ustyle = Style(name="Underline", family="text")
#uprop = TextProperties(fontweight="bold")                  #uncommented, this works well
#uprop = TextProperties(attributes={"fontsize":"26pt"})     #this either
uprop = TextProperties(attributes={"underline":"solid"})    # bad guess, wont work  !!
ustyle.addElement(uprop)
textdoc.automaticstyles.addElement(ustyle)
p = P(text="Hello world. ")
underlinedpart = Span(stylename=ustyle, text="This part would like to be underlined. ")
p.addElement(underlinedpart)
p.addText("This is after the style test.")
textdoc.text.addElement(p)
textdoc.save("myfirstdocument.odt") 

【问题讨论】:

    标签: python underline odf odfpy


    【解决方案1】:

    这是我最终得到它的方法:

    我使用 libreoffice 创建了一个带下划线的示例文档,并将其解压缩。查看提取文件的styles.xml部分,我得到了在文档中加下划线的部分:

    <style:style style:name="Internet_20_link" style:display-name="Internet link" style:family="text">
    <style:text-properties fo:color="#000080" fo:language="zxx" fo:country="none" style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color" style:language-asian="zxx" style:country-asian="none" style:language-complex="zxx" style:country-complex="none"/>
    </style:style>
    

    有趣的样式属性命名为:text-underline-style, 文字下划线宽度和文字下划线颜色。

    要在 odfpy 中使用它们,必须删除“-”字符,并且属性键必须用作 str(带引号),如以下代码所示。必须在 Style 构造函数调用中指定正确的样式系列(在我们的例子中为文本)。

    from odf.opendocument import OpenDocumentText
    from odf.style import Style, TextProperties
    from odf.text import H, P, Span
    
    textdoc = OpenDocumentText()
    
    #underline style
    ustyle = Style(name="Underline", family="text")  #here  style family
    
    uprop = TextProperties(attributes={
        "textunderlinestyle":"solid",
        "textunderlinewidth":"auto",
        "textunderlinecolor":"font-color"
        })
    
    ustyle.addElement(uprop)
    textdoc.automaticstyles.addElement(ustyle)
    p = P(text="Hello world. ")
    underlinedpart = Span(stylename=ustyle, text="This part would like to be underlined. ")
    p.addElement(underlinedpart)
    p.addText("This is after the style test.")
    textdoc.text.addElement(p)
    
    textdoc.save("myfirstdocument.odt")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-22
      • 1970-01-01
      • 1970-01-01
      • 2014-03-22
      • 2011-03-19
      • 2018-02-11
      相关资源
      最近更新 更多