分割表

Contents
[ ]

Aspose.Words Document Object Model 中表示的表格由独立的行和单元格组成,因此可以轻松拆分表格。

要操作一个表以将其拆分为两个表,我们只需将原始表中的一些行移动到新表即可。为此,我们需要选择要分割表的行。

我们可以通过以下简单步骤从原始表创建两个表:

  1. 创建表的克隆而不克隆子表以保留移动的行并将它们插入到原始表之后
  2. 从指定行开始,将所有后续行移至第二个表

以下代码示例演示如何将一个表拆分为特定行上的两个表:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
doc = aw.Document(MY_DIR + "Tables.docx")
first_table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table()
# We will split the table at the third row (inclusive).
row = first_table.rows[2]
# Create a new container for the split table.
table = first_table.clone(False).as_table()
# Insert the container after the original.
first_table.parent_node.insert_after(table, first_table)
# Add a buffer paragraph to ensure the tables stay apart.
first_table.parent_node.insert_after(aw.Paragraph(doc), first_table)
while True:
current_row = first_table.last_row
table.prepend_child(current_row)
if current_row == row:
break
doc.save(ARTIFACTS_DIR + "WorkingWithTables.split_table.docx")
view raw split-table.py hosted with ❤ by GitHub