Row class
Row class
Represents a table row. To learn more, visit the Working with Tables documentation article.
Remarks
Row can only be a child of a Table.
Row can contain one or more Cell nodes.
A minimal valid row needs to have at least one Cell.
Inheritance: Row → CompositeNode → Node
Constructors
Name | Description |
---|---|
Row(doc) | Initializes a new instance of the Row class. |
Properties
Name | Description |
---|---|
cells | Provides typed access to the Cell child nodes of the row. |
count | Gets the number of immediate children of this node. (Inherited from CompositeNode) |
custom_node_id | Specifies custom node identifier. (Inherited from Node) |
document | Gets the document to which this node belongs. (Inherited from Node) |
first_cell | Returns the first Cell in the row. |
first_child | Gets the first child of the node. (Inherited from CompositeNode) |
has_child_nodes | Returns True if this node has any child nodes.(Inherited from CompositeNode) |
is_composite | Returns True if this node can contain other nodes.(Inherited from Node) |
is_first_row | True if this is the first row in a table; false otherwise. |
is_last_row | True if this is the last row in a table; false otherwise. |
last_cell | Returns the last Cell in the row. |
last_child | Gets the last child of the node. (Inherited from CompositeNode) |
next_row | Gets the next Row node. |
next_sibling | Gets the node immediately following this node. (Inherited from Node) |
node_type | Returns NodeType.ROW. |
parent_node | Gets the immediate parent of this node. (Inherited from Node) |
parent_table | Returns the immediate parent table of the row. |
previous_row | Gets the previous Row node. |
previous_sibling | Gets the node immediately preceding this node. (Inherited from Node) |
range | Returns a Range object that represents the portion of a document that is contained in this node. (Inherited from Node) |
row_format | Provides access to the formatting properties of the row. |
Methods
Name | Description |
---|---|
accept(visitor) | Accepts a visitor. |
accept_end(visitor) | Accepts a visitor for visiting the end of the row. |
accept_start(visitor) | Accepts a visitor for visiting the start of the row. |
append_child(new_child) | Adds the specified node to the end of the list of child nodes for this node. (Inherited from CompositeNode) |
clone(is_clone_children) | Creates a duplicate of the node. (Inherited from Node) |
ensure_minimum() | If the Row has no cells, creates and appends one Cell. |
get_ancestor(ancestor_type) | Gets the first ancestor of the specified object type. (Inherited from Node) |
get_ancestor(ancestor_type) | Gets the first ancestor of the specified NodeType. (Inherited from Node) |
get_child(node_type, index, is_deep) | Returns an Nth child node that matches the specified type. (Inherited from CompositeNode) |
get_child_nodes(node_type, is_deep) | Returns a live collection of child nodes that match the specified type. (Inherited from CompositeNode) |
get_text() | Gets the text of all cells in this row including the end of row character. |
index_of(child) | Returns the index of the specified child node in the child node array. (Inherited from CompositeNode) |
insert_after(new_child, ref_child) | Inserts the specified node immediately after the specified reference node. (Inherited from CompositeNode) |
insert_before(new_child, ref_child) | Inserts the specified node immediately before the specified reference node. (Inherited from CompositeNode) |
next_pre_order(root_node) | Gets next node according to the pre-order tree traversal algorithm. (Inherited from Node) |
node_type_to_string(node_type) | A utility method that converts a node type enum value into a user friendly string. (Inherited from Node) |
prepend_child(new_child) | Adds the specified node to the beginning of the list of child nodes for this node. (Inherited from CompositeNode) |
previous_pre_order(root_node) | Gets the previous node according to the pre-order tree traversal algorithm. (Inherited from Node) |
remove() | Removes itself from the parent. (Inherited from Node) |
remove_all_children() | Removes all the child nodes of the current node. (Inherited from CompositeNode) |
remove_child(old_child) | Removes the specified child node. (Inherited from CompositeNode) |
remove_smart_tags() | Removes all SmartTag descendant nodes of the current node. (Inherited from CompositeNode) |
select_nodes(xpath) | Selects a list of nodes matching the XPath expression. (Inherited from CompositeNode) |
select_single_node(xpath) | Selects the first Node that matches the XPath expression. (Inherited from CompositeNode) |
to_string(save_format) | Exports the content of the node into a string in the specified format. (Inherited from Node) |
to_string(save_options) | Exports the content of the node into a string using the specified save options. (Inherited from Node) |
Examples
Shows how to create a table.
doc = aw.Document()
table = aw.tables.Table(doc)
doc.first_section.body.append_child(table)
# Tables contain rows, which contain cells, which may have paragraphs
# with typical elements such as runs, shapes, and even other tables.
# Calling the "EnsureMinimum" method on a table will ensure that
# the table has at least one row, cell, and paragraph.
first_row = aw.tables.Row(doc)
table.append_child(first_row)
first_cell = aw.tables.Cell(doc)
first_row.append_child(first_cell)
paragraph = aw.Paragraph(doc)
first_cell.append_child(paragraph)
# Add text to the first cell in the first row of the table.
run = aw.Run(doc=doc, text='Hello world!')
paragraph.append_child(run)
doc.save(file_name=ARTIFACTS_DIR + 'Table.CreateTable.docx')
Shows how to iterate through all tables in the document and print the contents of each cell.
doc = aw.Document(file_name=MY_DIR + 'Tables.docx')
tables = doc.first_section.body.tables
self.assertEqual(2, len(list(tables)))
i = 0
while i < tables.count:
print(f'Start of Table {i}')
rows = tables[i].rows
# We can use the "ToArray" method on a row collection to clone it into an array.
self.assertSequenceEqual(list(rows), list(rows))
self.assertNotEqual(rows, list(rows))
j = 0
while j < rows.count:
print(f'\tStart of Row {j}')
cells = rows[j].cells
# We can use the "ToArray" method on a cell collection to clone it into an array.
self.assertSequenceEqual(list(cells), list(cells))
self.assertNotEqual(cells, list(cells))
k = 0
while k < cells.count:
cell_text = cells[k].to_string(save_format=aw.SaveFormat.TEXT).strip()
print(f'\t\tContents of Cell:{k} = "{cell_text}"')
k += 1
print(f'\tEnd of Row {j}')
j += 1
print(f'End of Table {i}\n')
i += 1
Shows how to build a nested table without using a document builder.
def create_nested_table():
doc = aw.Document()
# Create the outer table with three rows and four columns, and then add it to the document.
outer_table = create_table(doc, 3, 4, 'Outer Table')
doc.first_section.body.append_child(outer_table)
# Create another table with two rows and two columns and then insert it into the first table's first cell.
inner_table = create_table(doc, 2, 2, 'Inner Table')
outer_table.first_row.first_cell.append_child(inner_table)
doc.save(ARTIFACTS_DIR + 'Table.create_nested_table.docx')
def create_table(doc: aw.Document, row_count: int, cell_count: int, cell_text: str) -> aw.tables.Table:
"""Creates a new table in the document with the given dimensions and text in each cell."""
table = aw.tables.Table(doc)
for row_id in range(1, row_count + 1):
row = aw.tables.Row(doc)
table.append_child(row)
for cell_id in range(1, cell_count + 1):
cell = aw.tables.Cell(doc)
cell.append_child(aw.Paragraph(doc))
cell.first_paragraph.append_child(aw.Run(doc, cell_text))
row.append_child(cell)
# You can use the "title" and "description" properties to add a title and description respectively to your table.
# The table must have at least one row before we can use these properties.
# These properties are meaningful for ISO / IEC 29500 compliant .docx documents (see the OoxmlCompliance class).
# If we save the document to pre-ISO/IEC 29500 formats, Microsoft Word ignores these properties.
table.title = 'Aspose table title'
table.description = 'Aspose table description'
return table
See Also
- module aspose.words.tables
- class CompositeNode