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 モジュールと関係があると思います。なぜこれが起こっているのか誰でも説明できますか?
ありがとう