someone
someone

Reputation: 1

Search and replace text ODFDO

I'm trying to searching and replacing some text with odfdo in python. But I can't figure out how to do it.

I'm trided with this code bellow, and it give some text.

doc = Document("test_01.odt")
k = doc.get_formatted_text()
print(k)

But i can't select the text when i use "if"

k = doc.get_formatted_text()
print(k)

if k == "replace":
    print(k)
else:
    pass

Upvotes: 0

Views: 402

Answers (2)

Jerome Dumonteil
Jerome Dumonteil

Reputation: 16

In simple situations, use document.body.replace() method (or the odfdo-replace script). However, as mentioned before, if the text to change is mangled in several <span> tags, a deeper analysis is required.

Upvotes: 0

Hlib
Hlib

Reputation: 1

The issue is that get_formatted_text() is an output function that is not supposed to be used for edits. Odf documents are actually xml element tree, so in the end you need to traverse the tree and find things to replace.

If you want to replace some text with odfdo you need to use functions like:

doc.body.get_paragraphs(content="identifier")
doc.body.get_table(content="identifier")
doc.body.get_headers(content="identifier")

Then replace with something like

element.replace("text_to_replace", "")

One issue you may encounter is that searched text will be split between different elements, it happens when the text is in different fonts, styles, with spaces in between, e.t.c.

To overcome this issue you can go through all elements of certain type, for example paragraphs, use get_formatted_text or such to figure out if your searched text is inside and then generate new element to replace in document

Upvotes: 0

Related Questions