我正在使用 python-docx 模块对大量文档进行一些编辑。它们都包含一个标题,我需要在其中替换一个数字,但每次我这样做时,文档都不会打开,错误是内容不可读。任何人对为什么会发生这种情况有任何想法,或者示例工作代码片段?谢谢。
from docx import *
#document = yourdocument.docx
filename = "NUR-ADM-2001"
relationships = relationshiplist()
document = opendocx("C:/Users/ai/My Documents/Nursing docs/" + filename + ".docx")
docbody = document.xpath('/w:document/w:body',namespaces=nsprefixes)[0]
advReplace(docbody, "NUR-NPM 101", "NUR-NPM 202")
# Create our properties, contenttypes, and other support files
coreprops = coreproperties(title='Nursing Doc',subject='Policies',creator='IA',keywords='Policy'])
appprops = appproperties()
contenttypes = contenttypes()
websettings = websettings()
wordrelationships = wordrelationships(relationships)
# Save our document
savedocx(document,coreprops,appprops,contenttypes,websettings, wordrelationships,"C:/Users/ai/My Documents/Nursing docs/" + filename + ".docx")
编辑:所以它最终可以打开文档,但它说某些内容无法显示并且标题已经消失......想法?