我需要在不更改格式的情况下替换部分文本。我写了这个函数:
import docx
import os
def getText(from_filename, to_filename, old_value, new_value):
doc = docx.Document(from_filename)
for paragraph in doc.paragraphs:
print(paragraph.text)
new_text = paragraph.text.replace(old_value, new_value)
print(new_text)
paragraph.text = new_text
doc.save(to_filename)
问题是粗体字体和字体苍蝇。我不知道如何做同样的事情,但保留格式。