我正在使用 python 库 docx:http: //github.com/mikemaccana/python-docx
我的目标是打开一个文件,替换某些单词,然后用替换写入文件。
我当前的代码:
#! /usr/bin/python
from docx import *
openDoc = "test.docx"
writeDoc = "test2.docx"
replace = {"Test":"TEST"}
document = opendocx(openDoc)
docbody = document.xpath('/w:document/w:body', namespaces=nsprefixes)[0]
print getdocumenttext(document)
for key in replace:
if search(docbody, key):
print "Found" , key , "replacing with" , replace[key]
docbody = replace(docbody,key,replace[key])
print getdocumenttext(document)
# ideally just want to mirror current document details here..
relationships = relationshiplist()
coreprops = coreproperties(title='',subject='',creator='',keywords=[])
savedocx(document,coreprops,appproperties(),contenttypes(),websettings(),wordrelationships(relationships),'a.docx')
` 但是我收到以下错误:
Traceback (most recent call last):
File "process.py", line 17, in <module>
docbody = replace(docbody,key,replace[key])
TypeError: 'dict' object is not callable
我不知道为什么会这样,但我认为这与 docx 模块有关。谁能解释为什么会这样?
谢谢