Skip to content

ReplaceOneTextToAnother

Pedro Alan edited this page Oct 29, 2020 · 6 revisions

Replace one text to another

!!! This example not work properly in some cases. Please give correct way for this task.

from odf import opendocument, text, teletype

doc = opendocument.load('test.odt')
for item in doc.getElementsByType(text.Span):
    s = teletype.extractText(item)
    if s.find('Replace this') != -1:
        s = s.replace('Replace this', 'to this')
        new_item = text.Span()
        new_item.setAttribute('stylename', item.getAttribute('stylename'))
        new_item.addText(s)
        item.parentNode.insertBefore(new_item, item)
        item.parentNode.removeChild(item)
doc.save('result.odt')
Clone this wiki locally