2

有没有办法在字符串替换期间保存段落样式?

from docx import Document


doc = Document('source.docx')
for p in doc.paragraphs:
    if 'some text' in p.text:
        text = p.text.replace('some text', 'new text')
        style = p.style
        p.text = text
        p.style = style

doc.save('dest.docx')

此代码替换字符串但更改字体样式和背景颜色。

4

0 回答 0