Table split at end of page

I have a document with few tables. The first table is split at end of its page (see input.docx (12.2 KB)). I applied this code to put the table in the following page.

import aspose.words as aw

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

table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table()

for cell in table.get_child_nodes(aw.NodeType.CELL, True):
    cell = cell.as_cell()
    cell.ensure_minimum()

    for para in cell.paragraphs:
        para = para.as_paragraph()
        if not (cell.parent_row.is_last_row and para.is_end_of_cell):
            para.paragraph_format.keep_with_next = True

doc.save("keep_table.docx")

As a result, the first table was placed in the beginning of the following page (hence not split anymore), but the second table became split.
How can I have all tables in the document not splited?
Any help is appreciated.

@ansar2024 Please try using the following code:

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

for t in doc.get_child_nodes(aw.NodeType.TABLE, True) :
    table = t.as_table()
    for r in table.rows :
        row = r.as_row()
        row.row_format.allow_break_across_pages = False
        if not row.is_last_row :
            for p in row.get_child_nodes(aw.NodeType.PARAGRAPH, True) :
                para = p.as_paragraph()
                para.paragraph_format.keep_with_next = True

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

Thank you very much. It works fine now.

1 Like