问题替换doc python

问题描述 投票:0回答:1

我正在尝试使用以下 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)

打印功能将标签更改显示为数据字典中存储的所需文本。新文档已保存,但没有明显的更改(标签尚未替换)。没有显示错误。

如何解锁?

python replace python-docx
1个回答
0
投票

您为函数提供一个

new_doc
参数,但您不对其执行任何操作,然后使用
document.save(new_doc)
保存它。您不需要对
new_doc
进行一些更改吗?

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.