Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot get landscape orientation to work for OpenDocumentText #106

Closed
jbh4x82 opened this issue Jul 29, 2021 · 1 comment
Closed

Cannot get landscape orientation to work for OpenDocumentText #106

jbh4x82 opened this issue Jul 29, 2021 · 1 comment

Comments

@jbh4x82
Copy link

jbh4x82 commented Jul 29, 2021

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
Copy link
Author

jbh4x82 commented Jul 30, 2021

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

@jbh4x82 jbh4x82 closed this as completed Jul 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant