eea / odfpy

API for OpenDocument in Python
GNU General Public License v2.0
311 stars 64 forks source link

Cannot get landscape orientation to work for OpenDocumentText #106

Closed jbh4x82 closed 3 years ago

jbh4x82 commented 3 years ago

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!

from odf.opendocument import OpenDocumentText
from odf.style import Style, ParagraphProperties, PageLayout, PageLayoutProperties, MasterPage
from odf.text import P, Page

textdoc = OpenDocumentText()

pageStyle = PageLayout(name="Standard")
pageStyle.addElement(PageLayoutProperties(printorientation="landscape"))
textdoc.automaticstyles.addElement(pageStyle)

mp = MasterPage(name="Standard", pagelayoutname=pageStyle)
textdoc.masterstyles.addElement(mp)
s = textdoc.stylesxml()

# Create a style for the paragraph with page-break
withbreak = Style(name="WithBreak", parentstylename="Standard", family="paragraph")
withbreak.addElement(ParagraphProperties(breakbefore="page"))
textdoc.automaticstyles.addElement(withbreak)

page = Page(masterpagename=mp)
p = P(text="Hello World!")
textdoc.text.addElement(p)
jbh4x82 commented 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")