I am using the python library docx: http://github.com/mikemaccana/python-docx
My goal is to open a file, replace certain words, then write the file with replacement.
My current code:
#! /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')
` However I get the following error:
Traceback (most recent call last):
File "process.py", line 17, in <module>
docbody = replace(docbody,key,replace[key])
TypeError: 'dict' object is not callable
I have no idea why this is happening, but I think it has something to do with the docx module. Can anyone explain why this is happening?
Thanks
You assigned replace
to a dictionary at the very top:
replace = {"Test":"TEST"}
So you cannot use the replace()
method, because the word replace
is now pointing to a dictionary - instead of what I suspect is some method from your library.
Rename your dictionary and it should work.