Text-Replace in docx and save the changed file with python-docx

前端 未结 8 743
小鲜肉
小鲜肉 2021-01-02 07:04

I\'m trying to use the python-docx module to replace a word in a file and save the new file with the caveat that the new file must have exactly the same formatting as the ol

8条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-02 07:37

    In addition to @ramil, you have to escape some characters before placing them as string values into the XML, so this worked for me:

    def escape(escapee):
      escapee = escapee.replace("&", "&")
      escapee = escapee.replace("<", "<")
      escapee = escapee.replace(">", ">")
      escapee = escapee.replace("\"", """)
      escapee = escapee.replace("'", "'")
    return escapee
    

提交回复
热议问题