title: How to replace multiple words in .docx file and save the docx file using python-docx
body: <p>I'm trying to change the content of the docx using python-docx library. My changes are about replacing the words. So, I have list of words <code>Original word list: ['ABC','XYZ']</code> which I need to replace with <code>revised word list: ['PQR', 'DEF']</code>. I also need to preserve the format of these words. Right now, I can save only one change. Here is my code for the reference.</p>
  
  <pre><code>def replace_string(filename='test.docx'):
  doc = Document(filename)
  list= ['ABC','XYZ']
  list2 = ['PQR','DEF']
  for p in doc.paragraphs:
          print(p.text)
          for i in range(0, len(list)):
              if list[i] in p.text:
                  print('----!!SEARCH FOUND!!------')
                  print(list[i])
                  print(list2[i])
                  print('\n')
                  inline = p.runs
                  # Loop added to work with runs (strings with same style)
                  for i in range(len(inline)):
                      #print(inline[i].text)
                      if list[i] in inline[i].text:
                          print('----SEARCH FOUND!!------')
                          text = inline[i].text.replace(list[i], list2[i])
                          inline[i].text = text
                          print(inline[i].text)
          doc.save('dest1.docx')
  return 1
  
  replace_string()
  </code></pre>
  
  <p>Original content of test.docx file: </p>
  
  <p><code>ABC
  XYZ
  </code></p>
  
  <p>Revised content or saved content of dest1.docx file:</p>
  
  <p><code>PQR
  XYZ
  </code></p>
  
  <p>How can I save all the replacements? The list of word may increase and its size is not fix.</p>