eea / odfpy

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

Setting paragraph style properties #44

Open dreme opened 7 years ago

dreme commented 7 years ago

I'd like to create an amended Text Body style which has large font and some extra spacing below each paragraph.

I've worked out how to set the font size as shown below:

s = newdoc.styles
textbody = Style(name="Text Body", family="paragraph")
textbody.addElement(TextProperties(attributes={'fontsize':"18pt"}))
s.addElement(textbody)

However, I can't see how to set the spacing below the paragraph. I'm assuming it would involve using the ParagraphProperties class, but I can't find any documentation for the parameters of this class, particularly how to set the attribute for paragraph spacing.

cityy commented 5 years ago

I know this is super old but I'm answering so it could be closed maybe. I tried simulating what you want to achieve by hand in a simple .odt made in word. I haven't tested it but you should be able to use the lineheightattribute. This is how it looks like in the .xml produced by word:

<style:paragraph-properties fo:line-height="300%" fo:break-before="page"/>

So, applied to your example:

s = newdoc.styles
textbody = Style(name="Text Body", family="paragraph")
textbody.addElement(TextProperties(attributes={'fontsize':"18pt"}))
textbody.addElement(ParagraphProperties(lineheight="300%"))
s.addElement(textbody)
dreme commented 5 years ago

Hey @cityy, thanks for having a go at answering my question. Better late than never!

Unfortunately, the edit you suggested textbody.addElement(ParagraphProperties(lineheight="300%")) didn't work.

I've pasted below a fuller depiction of my code which may help with testing and commentary:


from odf import text
from odf.opendocument import OpenDocumentText, load
from odf.style import Style, TextProperties, ParagraphProperties
from odf.text import P

infile = 'infile.odt'
outfile = 'outfile.odt'
doc = load(infile)
newdoc = OpenDocumentText()

# Add style
s = newdoc.styles
textbody = Style(name="Text Body", family="paragraph")
textbody.addElement(TextProperties(attributes={'fontsize':"12pt"}))
textbody.addElement(ParagraphProperties(lineheight="300%"))
s.addElement(textbody)

# Read in paragraphs from infile.odt
paras = doc.getElementsByType(text.P)

# Add paragraphs to newdoc
blankline = P(stylename=textbody,text='***')
for para in paras:
    if str(para) != '':  # don't copy blank lines
        p = P(stylename=textbody,text=str(para).strip())
        newdoc.text.addElement(p)
        newdoc.text.addElement(blankline)

# Save newdoc to outfile.odt    
newdoc.save(outfile) 
cityy commented 5 years ago

Hey @dreme, I made it work in this example:

from odf.opendocument import OpenDocumentText
from odf.style import Style, TextProperties, ParagraphProperties
from odf.text import P

textFile = OpenDocumentText()

bigText = Style(name="bigText", family="paragraph")
bigText.addElement(ParagraphProperties(lineheight="300%"))
bigText.addElement(TextProperties(fontsize="20pt"))

textFile.styles.addElement(bigText)

bigParagraph = P(text="Big Text", stylename=bigText)
smallParagraph = P(text="small Text")
anotherSmallParagraph = P(text="another small Text")

textFile.text.addElement(bigParagraph)
textFile.text.addElement(smallParagraph)
textFile.text.addElement(anotherSmallParagraph)

textFile.save("lineheight.odt")

unbenannt

dreme commented 5 years ago

Thx @cityy, your code does indeed work on my machine. Thx for following up!

Strangely though, it doesn't work on the code I had developed for reading in .odt files, processing them with the new style, and then exporting them as a new .odt files. There's something weird in there which I'll have to ponder over a bit more.