Hey guys,
I need to read a file and replace an old string by new string in some position which I already know.
I use python 2.7 and install python-docx.
I search on StackOverFlow.com and found that we could read a ms-word by convert to xml. I use re
to find the string but I don’t know how to replace this string and save document after that.
I found that tag <w:t>
contains text value.
Does anybody use python-docx and know how to search/replace in docx file ?
from docx import *
import re
with open ("text.docx", "rb") as f:
document = Document(f)
body_element = document._body._body
print body_element.xml
print re.findall('<w:t>', body_element.xml)