我使用pythondocx模块对大量文档进行编辑。它们都包含一个标题,我需要在其中替换一个数字,但是每次我这样做文档都不会打开,错误是内容不可读。有没有人知道为什么会发生这种情况,或是示例工作代码片段?谢谢。在
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")
编辑:所以它最终可以打开文档,但它说有些内容无法显示,标题也消失了。。。思想?在
我不知道这个模块,但一般来说,你不应该在适当的地方编辑文件。打开文件“A”,写入文件“/tmp/A”。关闭两个文件并确保没有错误,然后将“/tmp/A”移到“A”。否则,如果在写入过程中出现错误,则可能会损坏文件。在
相关问题 更多 >
编程相关推荐