使用odfpy在文本下方加下划线



我想用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") 

以下是我最终获得它的方式:

我使用libreoffice创建了一个带下划线的示例文档,并对其进行了解压缩

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

有趣的样式属性命名为:文本下划线样式,文本下划线宽度和文本下划线颜色。

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

最新更新