title: Get docx element from doc.element.iter()
body: <p><strong>QUESTION: How do I use the <code>child</code> object (below) to actually get the paragraph or table object?</strong></p>
  
  <p>This is based on the answer found <a href="https://stackoverflow.com/questions/25130957/how-to-iterate-over-everything-in-a-python-docx-document">here</a>, which referenced docx <a href="https://github.com/python-openxml/python-docx/issues/40" rel="nofollow noreferrer">Issue 40</a>.</p>
  
  <p>Unfortunately, none of the code posted there appears to work with commit <a href="https://github.com/python-openxml/python-docx" rel="nofollow noreferrer">e784a73  </a> but I was able to get close by an examination of the code (and trial and error)</p>
  
  <p>I have the following ... </p>
  
  <pre><code>def iter_block_items(parent):
      """
      Yield each paragraph and table child within *parent*, in document order.
      Each returned value is an instance of either Table or Paragraph.
      """
      print(type(parent))
      if isinstance(parent, docx.document.Document):
          parent_elm = doc.element.body
      elif isinstance(parent, _Cell):
          parent_elm = parent._tc
      else:
          raise ValueError("something's not right")
  
      for child in parent_elm.iter():
          if isinstance(child, docx.oxml.text.paragraph.CT_P):
              yield ("(paragraph)", child)
          elif isinstance(child, docx.oxml.table.CT_Tbl):
              yield ("(table)", child)
  
  for i in iter_block_items(doc): 
      print(i)
  </code></pre>
  
  <p>This successfully iterates through the elements, and gives me the following output ...</p>
  
  <pre><code>doc= &lt;class 'docx.document.Document'&gt;
  &lt;class 'docx.document.Document'&gt;
  ('(table)', &lt;CT_Tbl '&lt;w:tbl&gt;' at 0x10c9ce0e8&gt;)
  ('(paragraph)', &lt;CT_P '&lt;w:p&gt;' at 0x10c9ceef8&gt;)
  ('(paragraph)', &lt;CT_P '&lt;w:p&gt;' at 0x10c9ce0e8&gt;)
  ('(paragraph)', &lt;CT_P '&lt;w:p&gt;' at 0x10c9cef98&gt;)
  ('(paragraph)', &lt;CT_P '&lt;w:p&gt;' at 0x10c9ce0e8&gt;)
  ('(table)', &lt;CT_Tbl '&lt;w:tbl&gt;' at 0x10c9ceef8&gt;)
  ('(paragraph)', &lt;CT_P '&lt;w:p&gt;' at 0x10c9cef48&gt;)
  ('(paragraph)', &lt;CT_P '&lt;w:p&gt;' at 0x10c9cef48&gt;)
  ('(paragraph)', &lt;CT_P '&lt;w:p&gt;' at 0x10c9cef98&gt;)
  </code></pre>
  
  <p><strong>All I need at this point is: the text from each paragraph, and the table object for the table - so I can iterate through its cells.</strong> </p>
  
  <p>But <code>child.text</code> (for a paragraph) does not return the paragraph text (as it would in the example below) because the <code>child</code> object does not actually the paragraph object, but an element object that should be able to 'get' it.</p>
  
  <pre><code>for para in doc.paragraphs:
      print(para.text)
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I've tried:</p>
  
  <pre><code>yield child.text
  (yields "None")
  </code></pre>
  
  <p>and</p>
  
  <pre><code>from docx.text import paragraph
  yield paragraph(child)
  (Errors with TypeError: 'module' object is not callable)
  </code></pre>
  
  <p>and</p>
  
  <pre><code>from docx.oxml.text import paragraph
  yield paragraph(child)
  (Errors with TypeError: 'module' object is not callable)
  </code></pre>