How to save element.text in document.xml

121 Views Asked by At

I did a search and replace function in .docx document. I use module python-docx:

# -*- coding: utf-8 -*-
from docx import *

document = opendocx('test.docx')

TAG_LIST=[u'TAG1',u'TAG2',u'TAG3']
VALUE_LIST=[u'TEST1',u'TEST2',u'TEST3']

def search(document):
    result = False
    for element in document.iter():
        if element.text == '{':
            result=True
        if result and element.text != '}':
            for i in range(0,len(TAG_LIST)):
                if element.text == TAG_LIST[i]:
                     element.text=re.sub(TAG_LIST[i], VALUE_LIST[i], element.text)
                     print(element.text)

search(document)

This function finds the document tags in {}, verifies them with a TAG_LIST and makes replace to VALUE_LIST. I want to save element.text after re.sub function but don't know how to do it. I know that it is possible to read the entire text of document.xml, then do a replace, but loading the text into a buffer spends a lot of memory. Any ideas how to do it?

0

There are 0 best solutions below