title: How to use python-docx to replace text in a Word document and save
body: <p>The oodocx module mentioned in the same page refers the user to an /examples folder that does not seem to be there.<br>
  I have read the documentation of python-docx 0.7.2, plus everything I could find in Stackoverflow on the subject, so please believe that I have done my “homework”.  </p>
  
  <p>Python is the only language I know (beginner+, maybe intermediate), so please do not assume any knowledge of C, Unix, xml, etc.</p>
  
  <p>Task : Open a ms-word 2007+ document with a single line of text in it (to keep things simple) and replace any “key” word in Dictionary that occurs in that line of text with its dictionary value.  Then close the document keeping everything else the same.</p>
  
  <p>Line of text (for example) “We shall linger in the chambers of the sea.”</p>
  
  <pre><code>from docx import Document
  
  document = Document('/Users/umityalcin/Desktop/Test.docx')
  
  Dictionary = {‘sea’: “ocean”}
  
  sections = document.sections
  for section in sections:
      print(section.start_type)
  
  #Now, I would like to navigate, focus on, get to, whatever to the section that has my
  #single line of text and execute a find/replace using the dictionary above.
  #then save the document in the usual way.
  
  document.save('/Users/umityalcin/Desktop/Test.docx')
  </code></pre>
  
  <p>I am not seeing anything in the documentation that allows me to do this—maybe it is there but I don’t get it because everything is not spelled-out at my level.</p>
  
  <p>I have followed other suggestions on this site and have tried to use earlier versions of the module (<a href="https://github.com/mikemaccana/python-docx" rel="noreferrer">https://github.com/mikemaccana/python-docx</a>) that is supposed to have "methods like replace, advReplace" as follows: I open the source-code in the python interpreter, and add the following at the end (this is to avoid clashes with the already installed version 0.7.2):</p>
  
  <pre><code>document = opendocx('/Users/umityalcin/Desktop/Test.docx')
  words = document.xpath('//w:r', namespaces=document.nsmap)
  for word in words:
      if word in Dictionary.keys():
          print "found it", Dictionary[word]
          document = replace(document, word, Dictionary[word])
  savedocx(document, coreprops, appprops, contenttypes, websettings,
      wordrelationships, output, imagefiledict=None) 
  </code></pre>
  
  <p>Running this produces the following error message:</p>
  
  <p>NameError: name 'coreprops' is not defined </p>
  
  <p>Maybe I am trying to do something that cannot be done—but I would appreciate your help if I am missing something simple.</p>
  
  <p>If this matters, I am using the 64 bit version of Enthought's Canopy on OSX 10.9.3</p>