eea / odfpy

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

Losting images when search and replace text #61

Open ppKrauss opened 6 years ago

ppKrauss commented 6 years ago

I am using this solution to search/replace text.

from odf import text, teletype
from odf.opendocument import load

textdoc = load("original.odt")

texts = textdoc.getElementsByType(text.P)
s = len(texts)
for i in range(s):
    old_text = teletype.extractText(texts[i])
    new_text = old_text.replace('my-complexNoRisk-text-to-replace','something else')
    new_S = text.P()
    new_S.setAttribute("stylename",texts[i].getAttribute("stylename"))
    new_S.addText(new_text)
    texts[i].parentNode.insertBefore(new_S,texts[i])
    texts[i].parentNode.removeChild(texts[i])
textdoc.save('myfile.odt')

but myfile.odt is not the expected, losted the image.

DavidNemeskey commented 5 years ago

I am not sure what is valid and what isn't in an ODT, but isn't it possible that the lost image was a child of a P you deleted? BTW I did search and replace on the Text node level (you can modify its data), maybe that would work for you as well?

SMAntony commented 2 years ago

I get IndexError when I run this piece of code