Closed jbh4x82 closed 3 years ago
If anyone else struggles like me, I suggest you use XML Notepad to figure out how tags exactly have to look. Here is a working example that spits out a Landscape page with headers / footers (page number), hyperlinks and custom formatting. Hope it helps others who use this library!
from odf.opendocument import OpenDocumentText
from odf.style import PageLayout, MasterPage, Header, Footer, PageLayoutProperties, Style, TextProperties, ParagraphProperties
from odf.text import P, PageNumber, A, Span
textdoc = OpenDocumentText()
pl = PageLayout(name="pagelayout")
pl.addElement(PageLayoutProperties(pagewidth='11in', pageheight='8.5in', printorientation="landscape",
margintop='1in', marginleft='1in', marginbottom='0.5in',
marginright='1in', numformat='1', writingmode='lr-tb'))
textdoc.automaticstyles.addElement(pl)
mp = MasterPage(name="Standard", pagelayoutname=pl)
textdoc.masterstyles.addElement(mp)
hyperlinkStyle = Style(name="Hyperlink", parentstylename="Standard", family="text")
hyperlinkStyle.addElement(TextProperties(
attributes={"color": "#0000FF", "textunderlinetype": "single", "textunderlinestyle": "solid",
"textunderlinewidth": "auto"}))
textdoc.styles.addElement(hyperlinkStyle)
plainStyle = Style(name="plainStyle", parentstylename="Standard", family="paragraph")
plainStyle.addElement(ParagraphProperties(lineheight="130%"))
textdoc.styles.addElement(plainStyle)
centerStyle = Style(name="centerStyle", parentstylename="Standard", family="paragraph")
centerStyle.addElement(ParagraphProperties(textalign="center"))
textdoc.styles.addElement(centerStyle)
f = Footer()
fp = P()
fp.addElement(PageNumber(text="1"))
f.addElement(fp)
mp.addElement(f)
h = Header()
hp = P(text="Training name", stylename=centerStyle)
h.addElement(hp)
mp.addElement(h)
textdoc.text.addElement(P(text="Test text goes here", stylename=plainStyle))
sp = Span(text="hyperlink", stylename=hyperlinkStyle)
a = A(href="https://www.cnn.com")
a.addElement(sp)
newP = P()
newP.addElement(a)
textdoc.text.addElement(newP)
textdoc.save("headers.odt")
Hi! Would be very grateful if anyone could let me know why the below is not generating a landscape orientation document. Cannot find any working examples with odfpy. Thanks a lot!