Skip to content
-Smooth-E- edited this page Feb 15, 2023 · 4 revisions

Landscape layout with headers and footers:

from odf.opendocument import OpenDocumentText
from odf.style import PageLayout
from odf.style import MasterPage 
from odf.style import Header
from odf.style import Footer
from odf.style import PageLayoutProperties
from odf.style import Style
from odf.style import TextProperties
from odf.style import ParagraphProperties
from odf.text import P
from odf.text import PageNumber
from odf.text import A
from odf.text import Span

document = OpenDocumentText()

 # Dimensions are needed or it will not work in Word
page_layout_properties = PageLayoutProperties(
    pagewidth='11in', 
    pageheight='8.5in', 
    printorientation="landscape",
    margintop='1in', 
    marginleft='1in', 
    marginbottom='0.5in',
    marginright='1in', 
    numformat='1', 
    writingmode='lr-tb'
)

page_layout = PageLayout(name="pagelayout")
page_layout.addElement(page_layout_properties)
document.automaticstyles.addElement(page_layout)

master_page = MasterPage(name="Standard", pagelayoutname=page_layout)
document.masterstyles.addElement(master_page)

text_properties_attributes = {
    "color": "#0000FF", 
    "textunderlinetype": "single", 
    "textunderlinestyle": "solid",
    "textunderlinewidth": "auto"
}

text_properties = TextProperties(attributes=text_properties_attributes)
hyperlink_style = Style(name="Hyperlink", parentstylename="Standard", family="text")
hyperlink_style.addElement(text_properties)

document.styles.addElement(hyperlink_style)

plain_style = Style(name="plainStyle", parentstylename="Standard", family="paragraph")
plain_style.addElement(ParagraphProperties(lineheight="130%"))
document.styles.addElement(plain_style)

center_style = Style(name="centerStyle", parentstylename="Standard", family="paragraph")
center_style.addElement(ParagraphProperties(textalign="center"))
document.styles.addElement(center_style)

footer = Footer()
footer_paragraph = P()
footer_paragraph.addElement(PageNumber(text="1"))
footer.addElement(footer_paragraph)
master_page.addElement(footer)

header = Header()
header_paragraph = P(text="Training name", stylename=center_style)
header.addElement(header_paragraph)
master_page.addElement(header)


document.text.addElement(P(text="Test text goes here", stylename=plain_style))

span = Span(text="hyperlink", stylename=hyperlink_style)
hyperlink = A(href="https://www.cnn.com")
hyperlink.addElement(span)
paragraph = P()
paragraph.addElement(hyperlink)
document.text.addElement(paragraph)

document.save("headers.odt")
Clone this wiki locally