【发布时间】:2015-11-19 21:37:48
【问题描述】:
我正在尝试在 docx 文件上执行查找和替换,同时仍保持格式设置。
通过阅读本文,最好的方法似乎是在文档的 xml 文件上执行查找/替换。
我可以加载 xml 文件并在其上查找/替换,但不确定如何将其写回。
文档:
Hello {text}!
蟒蛇:
import zipfile
zip = zipfile.ZipFile(open('test.docx', 'rb'))
xmlString = zip.read('word/document.xml').decode('utf-8')
xmlString.replace('{text}', 'world')
【问题讨论】: