title: Editing a DOCX file
body: <p>I am working on a little project that should be quite simple.  I know its been done before but for the life of me, I cannot get it to work. Alright so I made a docx template using Microsoft word that contains a Header and just some text in the body of the paper.  My goal is have a program that can change this text.  Using python-docx I have successfully been able to write a program that modifies the body text easily.  That being said I am trying to learn how to do the same thing using XML parsing, which will allow the header to be changed.  Long story short, XML parsing (I think thats what it is) will give me much more freedom down the road.</p>
  
  <p>I know after the docx is unzipped, the word/document.xml contains the body text.
  Here is my code so far.</p>
  
  <pre><code>from lxml import etree as ET
  
  tree = ET.parse('document.xml')
  root = tree.getroot()
  
  for i in root.iter('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}t'):
      if i.text == 'Title':
          i.text = 'How to cook'
  
  tree.write('document_output.xml', xml_declaration = True, encoding = "UTF-8", method = "xml" \
  , standalone = "yes")
  </code></pre>
  
  <p>This program successfully changes the wanted text to the updated text.  </p>
  
  <p>Here is the original document.xml</p>
  
  <p><a href="https://www.dropbox.com/s/ghe1m176rdqtng7/document.xml?dl=0" rel="nofollow">https://www.dropbox.com/s/ghe1m176rdqtng7/document.xml?dl=0</a></p>
  
  <p>Here is the output. </p>
  
  <p><a href="https://www.dropbox.com/s/8n9llagozbvb2mz/document_output.xml?dl=0" rel="nofollow">https://www.dropbox.com/s/8n9llagozbvb2mz/document_output.xml?dl=0</a></p>
  
  <p>P.S. viewing the code from dropbox, it makes everything start at line 4 instead of line 1.  </p>
  
  <p>If you view them in an XML viewer you can see they are identical.  Also, if you use a text difference tool, the only difference is the changed word.  And I wouldn't think this would matter but the top line uses single quotes instead of double. </p>
  
  <p>Hope someone can shed some light on why this is still not opening properly in Word.</p>
  
  <p>Thanks for all the help!!</p>