CellCollection class

CellCollection class

Provides typed access to a collection of Cell nodes. To learn more, visit the Working with Tables documentation article.

Inheritance: CellCollectionNodeCollection

Indexers

NameDescription
__getitem__(index)Retrieves a Cell at the given index.

Properties

NameDescription
countGets the number of nodes in the collection.
(Inherited from NodeCollection)

Methods

NameDescription
add(node)Adds a node to the end of the collection.
(Inherited from NodeCollection)
clear()Removes all nodes from this collection and from the document.
(Inherited from NodeCollection)
contains(node)Determines whether a node is in the collection.
(Inherited from NodeCollection)
index_of(node)Returns the zero-based index of the specified node.
(Inherited from NodeCollection)
insert(index, node)Inserts a node into the collection at the specified index.
(Inherited from NodeCollection)
remove(node)Removes the node from the collection and from the document.
(Inherited from NodeCollection)
remove_at(index)Removes the node at the specified index from the collection and from the document.
(Inherited from NodeCollection)
to_array()Copies all cells from the collection to a new array of cells.

Examples

Shows how to iterate through all tables in the document and print the contents of each cell.

doc = aw.Document(MY_DIR + "Tables.docx")
tables = doc.first_section.body.tables

self.assertEqual(2, len(tables.to_array()))

for i in range(tables.count):
    print("Start of Table", i)

    rows = tables[i].rows

    # We can use the "to_array" method on a row collection to clone it into an array.
    self.assertSequenceEqual(list(rows), rows.to_array())
    #Assert.are_not_same(rows, rows.to_array())

    for j in range(rows.count):
        print("\tStart of Row", j)

        cells = rows[j].cells

        # We can use the "to_array" method on a cell collection to clone it into an array.
        self.assertSequenceEqual(list(cells), cells.to_array())
        #Assert.are_not_same(cells, cells.to_array())

        for k in range(cells.count):
            cell_text = cells[k].to_string(aw.SaveFormat.TEXT).strip()
            print(f"\t\tContents of Cell:{k} = \"{cell_text}\"")

        print(f"\tEnd of Row {j}")

    print(f"End of Table {i}\n")

See Also