Comments (1)
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")
from odfpy.
Related Issues (20)
- Table of contents HOT 1
- TypeError while height and width while defining the frame
- document on write formula with fraction/integral/matrix/... ?
- Automaticstyle not added into content xml HOT 1
- Linebreaks not picked up HOT 5
- Wrong AttributeError in setAttribute due to removing hyphens from attribute names HOT 1
- Cloning Subtrees
- clonespannedcolumns should not be in examples
- Iterate on All P forget Header paragraph
- Improve README.md with project status HOT 13
- How to create a table in a Text document? HOT 7
- Lost the numbers
- Would you release 1.4.2 version to pypi ? HOT 1
- ODF2XHTML throws an error if the document has a footnote or endnote in it
- Replace text
- Is there a way to generate the page breaks in an odt without having to open it manually first?
- Reverse Engineering Question HOT 1
- Open encrypted files HOT 1
- Python Wheel support
- Write functions
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from odfpy.