Sunday 15 February 2015

python - Search and replace text odfpy -



python - Search and replace text odfpy -

i'm trying create reports programme using odfpy. thought search each keywords [[[e_mail_address]]] , replace word database. found function text in odfpy api, converted string looses formating. there document in odfpy installation files: api-for-odfpy.odt. in point 6.2 teletype module, there written how texts document , set them list:

from odf import text, teletype odf.opendocument import load textdoc = load("my document.odt") allparas = textdoc.getelementsbytype(text.p) print teletype.extracttext(allparas[0])

and i'm looking method replace current text another. maybe:

text.change()

but there error while using it. if have experience in using odfpy, please help.

i found answer:

textdoc = load("myfile.odt") texts = textdoc.getelementsbytype(text.p) s = len(texts) in range(s): old_text = teletype.extracttext(texts[i]) new_text = old_text.replace('something','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')

python search replace odt

No comments:

Post a Comment