Find and replace text in .docx file - Python

后端 未结 2 1260
栀梦
栀梦 2020-12-28 19:41

I\'ve been doing a lot of searching for a method to find and replace text in a docx file with little luck. I\'ve tried the docx module and could not get that to work. Eventu

相关标签:
2条回答
  • 2020-12-28 19:59

    Sometimes, Word does strange things. You should try to remove the text and rewrite it in one stroke, eg without editing the text in the middle

    Your document is saved in a xml file (usually in word/document.xml for docx, afer unzipping). Sometimes it is possible that your text won't be in one stroke: it is possible that somewhere in the document, they is XXXCLIENT and somewhere else they is NAMEXXX.

    Something like this:

    <w:t> XXXCLIENT </w:t> ... <w:t> NAMEXXX </w:t>

    This happens quite often because of language support : word splits words when he thinks that one word is of one specific language, and may do so between words, that will split the words into multiple tags.

    Only problem with your solution is that you have to write everything in one stroke, which isn't the most user-friendly.

    I have created a JS Library that uses mustache like tags: {clientName} https://github.com/edi9999/docxgenjs

    It works globally the same as your algorithm but won't crash if the content is not in one stroke (when you write {clientName} in Word, the text will usually be splitted: {, clientName, } in the document.

    0 讨论(0)
  • 2020-12-28 20:02

    You can try a workaround. Use Word's search/replace to get the text in one stroke.

    For example search for "XXXCLIENTNAMEXXX" and replace it again with "XXXCLIENTNAMEXXX".

    0 讨论(0)
提交回复
热议问题