NodeCollection class

NodeCollection class

Represents a collection of nodes of a specific type. To learn more, visit the Aspose.Words Document Object Model (DOM) documentation article.

Remarks

NodeCollection does not own the nodes it contains, rather, is just a selection of nodes of the specified type, but the nodes are stored in the tree under their respective parent nodes.

NodeCollection supports indexed access, iteration and provides add and remove methods.

The NodeCollection collection is “live”, i.e. changes to the children of the node object that it was created from are immediately reflected in the nodes returned by the NodeCollection properties and methods.

NodeCollection is returned by CompositeNode.get_child_nodes() and also serves as a base class for typed node collections such as SectionCollection, ParagraphCollection etc.

NodeCollection can be “flat” and contain only immediate children of the node it was created from, or it can be “deep” and contain all descendant children.

Indexers

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

Properties

NameDescription
countGets the number of nodes in the collection.

Methods

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

Examples

Shows how to replace all textbox shapes with image shapes.

doc = aw.Document(MY_DIR + "Textboxes in drawing canvas.docx")

shapes = [node.as_shape() for node in doc.get_child_nodes(aw.NodeType.SHAPE, True)]

self.assertEqual(3, len([shape for shape in shapes if shape.shape_type == aw.drawing.ShapeType.TEXT_BOX]))
self.assertEqual(1, len([shape for shape in shapes if shape.shape_type == aw.drawing.ShapeType.IMAGE]))

for shape in shapes:
    if shape.shape_type == aw.drawing.ShapeType.TEXT_BOX:
        replacement_shape = aw.drawing.Shape(doc, aw.drawing.ShapeType.IMAGE)
        replacement_shape.image_data.set_image(IMAGE_DIR + "Logo.jpg")
        replacement_shape.left = shape.left
        replacement_shape.top = shape.top
        replacement_shape.width = shape.width
        replacement_shape.height = shape.height
        replacement_shape.relative_horizontal_position = shape.relative_horizontal_position
        replacement_shape.relative_vertical_position = shape.relative_vertical_position
        replacement_shape.horizontal_alignment = shape.horizontal_alignment
        replacement_shape.vertical_alignment = shape.vertical_alignment
        replacement_shape.wrap_type = shape.wrap_type
        replacement_shape.wrap_side = shape.wrap_side

        shape.parent_node.insert_after(replacement_shape, shape)
        shape.remove()

shapes = [node.as_shape() for node in doc.get_child_nodes(aw.NodeType.SHAPE, True)]

self.assertEqual(0, len([shape for shape in shapes if shape.shape_type == aw.drawing.ShapeType.TEXT_BOX]))
self.assertEqual(4, len([shape for shape in shapes if shape.shape_type == aw.drawing.ShapeType.IMAGE]))

doc.save(ARTIFACTS_DIR + "Shape.replace_textboxes_with_images.docx")

See Also