I am trying to insert an horizontal line in a LibreOffice Writer (odt) document using odfpy. (In Writer: Menu->Insert->Horizontal line)
This is my attempt (adapting this example):
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")
But I get the next error:
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>
I'm learning odfpy but there are few documentation. Also I have try to create the document in Writer, and after that read the style.xml
and content.xml
but I'm unable to see the related parts.
Finally I solved. Reading the style.xml file of a *.odt file generated by LO Writer with only a horizontal line, I have been able to do it. Probably there'll be a better way to figure out, but I can't fount it.