0

我正在尝试使用 odfpy 在 LibreOffice Writer (odt) 文档中插入一条水平线。(在Writer中:菜单->插入->水平线)

这是我的尝试(改编这个例子):

from odf import opendocument, chart, text, draw

drawdoc = opendocument.OpenDocumentDrawing() # Create the subdocument
maindoc = opendocument.OpenDocumentText() # Create the main document
dl = draw.Line(x1="1pt", x2="1pt", y1="100pt", y2="100pt",  anchortype="paragraph")
maindoc.text.addElement(dl)
objectloc = maindoc.addObject(drawdoc)
do = draw.Object(href=objectloc)
dl.addElement(do)
maindoc.save("horizontalline.odt")

但我得到下一个错误:

Traceback (most recent call last):
  File "pagebreak.py", line 9, in <module>
    dl.addElement(do)
  File "/usr/local/lib/python3.6/dist-packages/odf/element.py", line 427, in addElement
    raise IllegalChild( "<%s> is not allowed in <%s>" % ( element.tagName, self.tagName))
odf.element.IllegalChild: <draw:object> is not allowed in <draw:line>

我正在学习 odfpy,但文档很少。我也尝试在 Writer 中创建文档,然后阅读style.xmlcontent.xml但我无法看到相关部分。

4

2 回答 2

0

最后我解决了。读取 LO Writer 生成的 *.odt 文件的 style.xml 文件,只有一条水平线,我已经能够做到了。可能会有更好的方法来弄清楚,但我找不到它。

textFile = OpenDocumentText()
horizontal_line = Style(name="Horizontal_20_Line", displayname="Horizontal Line", family="paragraph", parentstylename="Standard", nextstylename="Text_20_body")
horizontal_line.addElement(ParagraphProperties(margintop="0cm", marginbottom="0.499cm", contextualspacing="false", borderlinewidthbottom="0cm 0.010cm 0.08cm", padding="0cm", borderleft="none", borderright="none", bordertop="none", borderbottom="0.06pt double #574644", numberlines="false", linenumber="0", joinborder="false"))
horizontal_line.addElement(TextProperties(fontfamily="arial", fontsize="12pt", fontsizeasian="6pt", fontsizecomplex="6pt"))
textFile.styles.addElement(horizontal_line)
parrafo = P(text="THIS IS AN HORIZONTAL LINE", stylename=horizontal_line)
textFile.text.addElement(parrafo)
于 2020-04-01T17:30:46.700 回答
0

Line您可以通过简单地添加一个来绘制一条水平线textdoc.text

from odf.opendocument import OpenDocumentText
from odf.draw import Line

textdoc = OpenDocumentText()
line = Line(x1="1pt", x2="100pt", y1="100pt", y2="100pt")
textdoc.text.addElement(line)
于 2021-09-03T07:23:29.117 回答