Skip to content

ReplaceOneTextToAnother

thunderamur edited this page Apr 3, 2020 · 6 revisions

Replace one text to another

from odf import opendocument, text, teletype

doc = opendocument.load('test.odt')
for item in doc.getElementsByType(text.Span):
    s = teletype.extractText(item)
    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