我正在尝试使用以下 python 函数替换文档中的一些标签:
def fill_in_doc(template, data, new_doc):
document = Document(template)
for para in document.paragraphs:
for cle, valeur in data.items():
if cle in para.text:
para.text = para.text.replace(cle, data[cle])
for table in document.tables:
for row in table.rows:
for cell in row.cells:
for cle, valeur in data.items():
if cle in cell.text:
print(cell.text)
cell.text = cell.text.replace(cle, data[cle])
print(cell.text)
document.save(new_doc)
打印功能将标签更改显示为数据字典中存储的所需文本。新文档已保存,但没有明显的更改(标签尚未替换)。没有显示错误。
如何解锁?
您为函数提供一个
new_doc
参数,但您不对其执行任何操作,然后使用 document.save(new_doc)
保存它。您不需要对new_doc
进行一些更改吗?