How to determine if paragraph is a table to skip it?

import aspose.words as aw

docToRead = aw.Document("...docx")

for paragraph in docToRead.get_child_nodes(aw.NodeType.PARAGRAPH, True) :    
    paragraph = paragraph.as_paragraph()
    #if paragraph.istable != 0: 
       print(paragraph.to_string(aw.SaveFormat.TEXT))

@ArturM A paragraph cannot be a table, it can be inside a table. So you can simply check whether the paragraph is a child of the table. For example see the following code:

doc = aw.Document("C:\\Temp\\in.docx")

for paragraph in doc.get_child_nodes(aw.NodeType.PARAGRAPH, True) :    
    paragraph = paragraph.as_paragraph()
    if(paragraph.get_ancestor(aw.NodeType.TABLE) is None) :
       print(paragraph.to_string(aw.SaveFormat.TEXT))

Please see our documentation to learn more about Aspose.Words Document Object Model:
https://docs.aspose.com/words/python-net/aspose-words-document-object-model/

it works, great! tnx very much!

1 Like

Can you help me please one more time - why replace method works fine with constant string in apostrophes, but doesn’t work with variable string?

doc = aw.Document("...docx")

for paragraph in docToRead.get_child_nodes(aw.NodeType.PARAGRAPH, True) :    
    paragraph = paragraph.as_paragraph()    
    if(paragraph.get_ancestor(aw.NodeType.TABLE) is None) :   

    doc.range.replace(paragraph.to_string(aw.SaveFormat.TEXT), content,    
    aw.replacing.FindReplaceOptions(aw.replacing.FindReplaceDirection.FORWARD)) # doesn't work
    
   doc.range.replace("123", "345",  
   aw.replacing.FindReplaceOptions(aw.replacing.FindReplaceDirection.FORWARD)) # works fine

   doc.save("...docx")

@ArturM This occurs because paragraph.to_string(aw.SaveFormat.TEXT) contains paragraph break as \r\n sequence. To replace text of the paragraph, you shout trim these cahracters:

doc = aw.Document("C:\\Temp\\in.docx")

for paragraph in doc.get_child_nodes(aw.NodeType.PARAGRAPH, True) :    
    paragraph = paragraph.as_paragraph()
    if(paragraph.get_ancestor(aw.NodeType.TABLE) is None) :
        para_text = paragraph.to_string(aw.SaveFormat.TEXT).strip();
        if(para_text != "") :
            doc.range.replace(para_text, "replacemnt text")

doc.save("C:\\Temp\\out.docx")

If you need to replace paragraph break character in MS Word document, you should use special &p metacharacter. Please see our documentation for more information:
https://reference.aspose.com/words/python-net/aspose.words/range/replace/#str_str

yup, it did work, tnx very much!

1 Like

May I bother you one more time - hope the last (at this context at least) - I’m really stuck despite of your documentation. I need to skip not only table’s text, but also headings of all levels - 1, 2, 3 etc
But it doesn’t work for me both ways:

  if(paragraph.get_ancestor(aw.NodeType.TABLE) is None) and not(paragraph.break_is_style_separator):   
  
  if(paragraph.get_ancestor(aw.NodeType.TABLE) is None) and (paragraph.paragraph_format.style_identifier != aw.StyleIdentifier.HEADING1):

@ArturM There is ParagraphFormat.is_heading property, which will allow you to determine whether paragraph is a heading.

Thank you very much once again, it did work out!

1 Like