python如何替换word文档里面的特定的字符

pip install python-docx #第一步先安装python-docx模块
import docx

def replace_text_in_word_document(file_path, old_text, new_text):
    doc = docx.Document(file_path)
    for para in doc.paragraphs:
        para.text = para.text.replace(old_text, new_text)
    doc.save(file_path)

replace_text_in_word_document("document.docx", "old_text", "new_text")
#以上为代码

 

posted @ 2023-02-04 11:04  无恙大势  阅读(791)  评论(0)    收藏  举报