Highlight and add comment to text

@alexey.noskov I want to identify if there are any tables after headings in a document. How can I do that

@harshitha112000 You can check heading paragraph’s next sibling and check it’s field type. For example see the following code:

doc = aw.Document("C:\\Temp\\in.docx")
# Get paragraph
para = doc.first_section.body.first_paragraph
# Check type of the next node.
print(para.next_sibling.node_type)

ok thankyou.

1 Like

@alexey.noskov I want to check for the empty cell in the table and print the header of the table if it’s cell is empty. Can you please help me?

Document1.docx (3.1 KB)

@harshitha112000 You can use code like the following to check whether there are empty cells in the table:

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

# Get all tables in the document.
tables = doc.get_child_nodes(aw.NodeType.TABLE, True)

# Loop through the tables
for node in tables:
    table = node.as_table()
    # Get the first not empty paragraph before the table (consider it as table title)
    title_para = table.previous_sibling.as_paragraph()
    while title_para!=None and title_para.to_string(aw.SaveFormat.TEXT).strip()=="":
        title_para = title_para.previous_sibling.as_paragraph()
    
    # Check whether there are empty cells in the table.
    cells = table.get_child_nodes(aw.NodeType.CELL, True)
    has_empty_cells = False
    for cell_node in cells:
        cell = cell_node.as_cell()
        if cell.get_child_nodes(aw.NodeType.SHAPE, True).count==0 and cell.to_string(aw.SaveFormat.TEXT).strip()=="":
            has_empty_cells = True
            break

    if has_empty_cells:
        print(title_para.to_string(aw.SaveFormat.TEXT).strip())

Thank you @alexey.noskov

1 Like

How to get a table heading in a word document? @alexey.noskov

@harshitha112000 Could you please attach your input document and expected output? Most likely you simply need to get the paragraph before the table that contains the table title. If so you can simply use Node.previous_sibling property.

Ok got it. Thanks

@alexey.noskov how to extract ‘track changes’ content from word document

@harshitha112000 You can access revisions in the document using Document.revisions collection. But in MS Word revisions usually are grouped. So to access revisions in the same way as they are represented in MS Word, it is more convenient to use RevisionGroup. For example see the following code:

doc = aw.Document("C:\\Temp\\in.docx")
for group in doc.revisions.groups:
    print(f"Revision author: {group.author};\r\nRevision type: {group.revision_type}\r\nRevision text: {group.text}")
    print("=============================")

RevisionGroup.revision_type property you can determine type of revision.

Also, you can use Inline.is_delete_revision , Inline.is_insert_revision, Inline.is_format_revision, Inline.is_move_from_revision and Inline.is_move_to_revision to check whether inline node has revision. Paragraph node has similar properties.