title: Preserving styles while replacing words in python-docx
body: <p>I've written a small function to replace certain words with <code>python-docx</code>, which works great except that it changes some style element but weirdly not all of them.</p>
  
  <p>My function (simplified):</p>
  
  <pre><code>def template2doc(replace_dict, source, destination):
      """
      Creates a new docx file from a template, replacing keywords withing the template and saving with a new name
      :param replace_dict: dict for replacing each key with its value
      :param source: path to source template file
      :param destination: path to save the result document
      :return: None
      """
      doc = docx.Document(source)
  
      for key, replacement in replace_dict.items():
          for paragraph in doc.paragraphs:
              if key in par.text:
                  paragraph.text = paragraph.text.replace(key, replacement)
  
      # Save the result
      doc.save(destination)
  </code></pre>
  
  <p>This works for replacing words or sentences, but fails to preserve some style elements for the paragraph (alignment, font size, bold) but preserves others (double underline for example). So my question:</p>
  
  <p><strong>Is there a better way to replace phrases in docx files, while still keeping all of the former styling?</strong></p>
  
  <p>Note: i'm not "married" to <code>python-docx</code>, any solution or package that works will do</p>