jdum / odfdo

python library for OpenDocument format (ODF)
Apache License 2.0
48 stars 11 forks source link

how_to_apply_a_style_to_a_paragraph.py simply duplicates paragraphs with new style. #21

Open Sazu-bit opened 1 year ago

Sazu-bit commented 1 year ago
for selectedParagraph in body.get_paragraphs():

    # Work on the element itself
    selectedParagraph.set_span("Text Body")

so I tried the append route (as described in the recipes) but that just adds an extra element. I hunted down what seems to be the correct way to do it, which is 'set_span' since I'm iterating through the paragraphs but... no joy.

Still stays as "Default Paragraph Style".

What gives?

Additional

For applying a style to a paragraph the recipes offer:

body.append(Paragraph("some text", style="Text Body"))

However, append does exactly what it is designed to do and basically copies the paragraph.

jdum commented 1 year ago

Hi, About set_span() : The syntax is either Paragraph.set_span("syle_name", regex="some regex to find relevant text target") or Paragraph.set_span("syle_name", offset=x, length=y) if the exact position of the target is known.

And there is some bug: if neither regex or offset are provided, nothing happens (it should be at least raise an error)

Sazu-bit commented 1 year ago

I don't need to apply a style to specific words, I need to apply the style to the paragraph. It's just simple clean up work. So the regex is pretty useless to me.

Instead I worked around this by adding:

Paragraph.delete(paragraph) just before a body.append, basically I save the content earlier (as I'm already checking if it's an empty line or not).

Since you're reading this thread though...

Thank you so much for creating this!

jdum commented 1 year ago

Maybe Paragraph.set_span("syle_name", offset=0), that should apply to whole content of the paragraph.

Sazu-bit commented 1 year ago

No joy.

I've tried "Text_Body", "text_body", "Text Body", "text body" with offset=0 but once the document is generated, there's no change the the paragraphs.

for selectedParagraph in body.get_paragraphs():

    # Work on the content inside the paragraph
    paragraphContent = selectedParagraph.text_recursive
    selectedParagraph.set_span("Text_Body", offset=0)

I can't use Paragraph unless I can specify it somewhere. (I love how python doesn't care about key words...).