title: How can I copy equations from docx to a specific location in another docx?
body: <p>Hello I am currently trying to write a code that combines docx files. These files may have text, images, tables, or equations. The code aims to copy these objects and append them to a base docx. I am able to copy and merge text, images, and tables by using the docx module's 'add_picture' and 'add_paragraph' methods but I cannot do this for word equations. I decided to try to dig into the xml of the docx and copy the equation section from there. I am able to append equations to my base document but when I continue to append pictures, texts, and tables, these equations show up at the <strong>end</strong> of the docx. My questions are: <strong>why</strong> does this occur if I loop through the appended objects in the order I want them to appear and is <strong>there is a way</strong> to keep the code from putting the equations at the end of the docx. </p>
  
  <p>Here is some overview of the code:</p>
  
  <ol>
  <li><p>create base document:</p>
  
  <p>document=Document('basedoc.docx')</p></li>
  <li>get list of sub-documents to append</li>
  <li>start loop through sub-document list</li>
  <li>for each sub-doc I iterate through and find the different parent and child objects. I found this function on this website called 'iter_block_items'(<a href="https://github.com/python-openxml/python-docx/issues/276" rel="nofollow noreferrer">https://github.com/python-openxml/python-docx/issues/276</a>)
  The document items are referred to as blocks.  </li>
  <li><p>For each block item of the sub-doc I categorize the type, style, and whether an equation is present or not:</p>
  
  <p>if isinstance(block, Paragraph):</p>
  
  <pre><code>if "r:embed" in block._element.xml:
  
      append content,style, and equation arrays, content being a drawing/image
  
  elif "m:oMathPara" in block._element.xml:
  
      append content,style, and equation arrays, content being an equation
      equationXml.append(block._element.xml)
  
  elif 'w:br w:type="page"' in block._element.xml:
  
      append content,style, and equation arrays, content being a page break
  
  else:
  
      append content,style, and equation arrays), content being text
  </code></pre>
  
  <p>else:</p>
  
  <pre><code>append content,style, and equation arrays, content being a table
  </code></pre></li>
  <li><p>Once I have my arrays of content and style I loop through the content array and append table, drawings, pagebreaks, and texts. </p>
  
  <pre><code>    if equationXml[i]=='0': #the content is either an image, table, text, or page break
          if "Table" in str(contentStyle[i]):
                  insert table and caption
          else:
              if "drawing" in content[i]:
                  insert image and caption
  
              elif "pageBreak" in content[i]:
                  document.add_page_break()
              else:
                  insert text
      else:                        #there is an equation present
        document=EquationInsert.(document,equationXml[i])
  </code></pre></li>
  </ol>
  
  <p>My EquationInsert file has function called 'AddEquation' where I basically rewrite my document object (where UpdateableZipFile is a code I found online that quickly updates a file in a zip file):</p>
  
  <pre><code>def AddEquation(self,document,equationContent):
      document.save('temp.docx')
      z = zipfile.ZipFile('temp.docx')
      tree=etree.parse(z.open('word/document.xml'))
      nmspcDict = tree.getroot().iter().next().nsmap
  
      for key in nmspcDict:
          ET.register_namespace(key, nmspcDict[key])
      tree2=etree.ElementTree(etree.fromstring(equationContent))
      xmlRoot2=tree2.getroot()
      xmlRoot=tree.getroot()
      xmlRoot[1].append(xmlRoot2) #note that [1] had to be used bc [0] was a comment. need to see if general case or not
  
  
      tree.write("document.xml",encoding="utf-8", xml_declaration=True, standalone="yes", pretty_print=True)
  
      with UpdateableZipFile.UpdateableZipFile("temp.docx","a") as o:
          o.write("document.xml","word/document.xml")
  
      document = Document('temp.docx') 
      os.remove('document.xml')
      z.close()
      os.remove('temp.docx')
      return document
  </code></pre>
  
  <p>This code adds the equation but as the main code continues to loop through sub-doc items, the equations are just pushed to the end of the base document somehow. I've tried returning a docx from the Insert equation function and creating a new document from it but that didn't do anything. If anyone has any advice on how to make the equation not go to the end of the file that would be very appreciated. Otherwise I'll have to venture into seeing how to convert these equations into images =/ or something that docx can handle. I'm open to solutions/suggestions/comments. Thanks!</p>