gpt4 book ai didi

python - TypeError : 'dict' object is not callable

转载 作者:行者123 更新时间:2023-11-28 19:57:57 29 4
gpt4 key购买 nike

我正在使用 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 模块有关。谁能解释为什么会这样?

谢谢

最佳答案

您将 replace 分配给了最顶部的字典:

replace = {"Test":"TEST"}

因此您不能使用 replace() 方法,因为单词 replace 现在指向字典 - 而不是我怀疑是您库中的某个方法。

重命名您的字典,它应该可以工作。

关于 python - TypeError : 'dict' object is not callable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12034296/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com