如何有效地用python替换word文档中的句子

How to effectively replace sentences in word document with python

这是我目前所做的:

from docx import Document

document = Document('filename.docx')

dic = {
    'Stack':'Stack Overflow',
'October 18 2021' : 'Actual Date'}
for p in document.paragraphs:
    inline = p.runs
    for i in range(len(inline)):
        text = inline[i].text
        for key in dic.keys():
            if key in text:
                 text=text.replace(key,dic[key])
                 inline[i].text = text


document.save('new.docx')

但是好像这个功能在她需要替换一个词的时候可以正常使用,但是当她需要替换句子的时候,它就不行了(这里是October 18 2021)/

知道为什么句子不起作用吗?

问题在于您正在阅读的部分句子实际上处于不同的运行状态。

正如 scanny 在 post 中所述:

So runs can effectively break up the text of a paragraph at arbitrary locations, even one run per character. In short, Word doesn't try to keep track of sentences; if you see a run that is a sentence that is pure coincidence.

解决此问题的一个简单方法是使用 paragraph.text 而不是 inline.text

进行搜索和替换
from docx import Document

document = Document('test.docx')

dic = {
    'Stack':'Stack Overflow',
    'October 18 2021' : 'Actual Date'
}
for p in document.paragraphs:
    for key in dic.keys():
        if key in p.text:
            p.text = p.text.replace(key,dic[key])

document.save('new.docx')