Manage SmartArt Shape Nodes in Presentations Using Python

Overview

SmartArt graphics in PowerPoint presentations are organized through nodes that contain text and define the structure of the diagram. Aspose.Slides allows you to work with these SmartArt nodes programmatically: add new nodes and child nodes, insert child nodes at a specific position, access existing nodes, and read their text, level, and position.

This article explains how to manage SmartArt shape nodes. It shows how to remove nodes, work with child nodes by index or position, change an assistant node to a normal node, adjust the position, size, and rotation of SmartArt node shapes, set node fill formats, and generate a thumbnail image for a SmartArt child node.

Add a SmartArt Node

Aspose.Slides for Python via Java provides an API to manage SmartArt shapes. The following example adds a node and a child node to a SmartArt shape.

  1. Create an instance of the Presentation class and load the presentation containing a SmartArt shape.
  2. Get the first slide by its index.
  3. Iterate through every shape on the first slide.
  4. Check whether the shape is a SmartArt instance.
  5. Add a new node to the SmartArt shape’s node collection and set its text through TextFrame.
  6. Add a child node to the new node and set its text through TextFrame.
  7. Save the presentation.
import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, SaveFormat, SmartArt

presentation = Presentation("SimpleSmartArt.pptx")
try:
    for shape in presentation.getSlides().get_Item(0).getShapes():
        if isinstance(shape, SmartArt):
            smart_art = shape
            node = smart_art.getAllNodes().addNode()
            node.getTextFrame().setText("Test")
            child_node = node.getChildNodes().addNode()
            child_node.getTextFrame().setText("New Node Added")
    presentation.save("AddSmartArtNode.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

Add a SmartArt Node at a Specific Position

The following example adds a child node at a specific position in a SmartArt node.

  1. Create an instance of the Presentation class.
  2. Get the first slide by its index.
  3. Add a SmartArt shape with the StackedList layout to the slide.
  4. Access the first node in the added SmartArt shape.
  5. Add a child node to the selected node at position 2 using addNodeByPosition and set its text.
  6. Save the presentation.
import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, SaveFormat, SmartArtLayoutType

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)
    smart_art = slide.getShapes().addSmartArt(0, 0, 400, 400, SmartArtLayoutType.StackedList)
    node = smart_art.getAllNodes().get_Item(0)
    child_node = node.getChildNodes().addNodeByPosition(2)
    child_node.getTextFrame().setText("Sample Text Added")
    presentation.save("AddSmartArtNodeByPosition.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

Access a SmartArt Node

The following example accesses nodes in a SmartArt shape. The layout returned by getLayout is read-only and is set when the SmartArt shape is added.

  1. Create an instance of the Presentation class and load the presentation containing a SmartArt shape.
  2. Get the first slide by its index.
  3. Iterate through every shape on the first slide.
  4. Check whether the shape is a SmartArt instance.
  5. Iterate through all nodes in the SmartArt shape.
  6. Read and display each SmartArt node’s position, level, and text.
import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, SmartArt

presentation = Presentation("SmartArtShape.pptx")
try:
    slide = presentation.getSlides().get_Item(0)
    for shape in slide.getShapes():
        if isinstance(shape, SmartArt):
            smart_art = shape
            for i in range(smart_art.getAllNodes().size()):
                node = smart_art.getAllNodes().get_Item(i)
                print(node.getTextFrame().getText(), " ", node.getLevel(), " ", node.getPosition())
finally:
    presentation.dispose()

Access a SmartArt Child Node

The following example accesses the child nodes of each node in a SmartArt shape.

  1. Create an instance of the Presentation class and load the presentation containing a SmartArt shape.
  2. Get the first slide by its index.
  3. Iterate through every shape on the first slide.
  4. Check whether the shape is a SmartArt instance.
  5. Iterate through all nodes in the SmartArt shape.
  6. For each node, iterate through its child nodes.
  7. Read and display the child node position, level, and text.
import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, SmartArt

presentation = Presentation("AccessChildNodes.pptx")
try:
    slide = presentation.getSlides().get_Item(0)
    for shape in slide.getShapes():
        if isinstance(shape, SmartArt):
            smart_art = shape
            for i in range(smart_art.getAllNodes().size()):
                parent_node = smart_art.getAllNodes().get_Item(i)
                for j in range(parent_node.getChildNodes().size()):
                    node = parent_node.getChildNodes().get_Item(j)
                    print("j = ", j, ", Text = ", node.getTextFrame().getText(), ",  Level = ", node.getLevel(), ", Position = ", node.getPosition())
finally:
    presentation.dispose()

Access a SmartArt Child Node at a Specific Position

The following example accesses a child node at a specific index in its parent node’s collection.

  1. Create an instance of the Presentation class.
  2. Get the first slide by its index.
  3. Add a SmartArt shape with the StackedList layout.
  4. Access the added SmartArt shape.
  5. Access the node at index 0 in the SmartArt shape.
  6. Access the child node at index 1 using get_Item.
  7. Read and display the child node position, level, and text.
import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, SmartArtLayoutType

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)
    smart_art = slide.getShapes().addSmartArt(0, 0, 400, 400, SmartArtLayoutType.StackedList)
    node = smart_art.getAllNodes().get_Item(0)
    position = 1
    child_node = node.getChildNodes().get_Item(position)
    print("Text = ", child_node.getTextFrame().getText(), ",  Level = ", child_node.getLevel(), ", Position = ", child_node.getPosition())
finally:
    presentation.dispose()

Remove a SmartArt Node

The following example removes a node from a SmartArt shape.

  1. Create an instance of the Presentation class and load the presentation containing a SmartArt shape.
  2. Get the first slide by its index.
  3. Iterate through every shape on the first slide.
  4. Check whether the shape is a SmartArt instance.
  5. Check that the SmartArt shape contains at least one node.
  6. Select the SmartArt node to be deleted.
  7. Remove the selected node using removeNode.
  8. Save the presentation.
import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, SaveFormat, SmartArt

presentation = Presentation("AddSmartArtNode.pptx")
try:
    for shape in presentation.getSlides().get_Item(0).getShapes():
        if isinstance(shape, SmartArt):
            smart_art = shape
            if smart_art.getAllNodes().size() > 0:
                node = smart_art.getAllNodes().get_Item(0)
                smart_art.getAllNodes().removeNode(node)
    presentation.save("RemoveSmartArtNode.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

Remove a SmartArt Node from a Specific Position

The following example removes a child node at a specific index in a SmartArt node’s collection.

  1. Create an instance of the Presentation class and load the presentation containing a SmartArt shape.
  2. Get the first slide by its index.
  3. Iterate through every shape on the first slide.
  4. Check whether the shape is a SmartArt instance.
  5. Access the SmartArt node at index 0 if it exists.
  6. Check that the selected SmartArt node has at least two child nodes.
  7. Remove the child node at index 1 using removeNode.
  8. Save the presentation.
import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, SaveFormat, SmartArt

presentation = Presentation("AddSmartArtNode.pptx")
try:
    for shape in presentation.getSlides().get_Item(0).getShapes():
        if isinstance(shape, SmartArt):
            smart_art = shape
            if smart_art.getAllNodes().size() > 0:
                node = smart_art.getAllNodes().get_Item(0)
                if node.getChildNodes().size() >= 2:
                    node.getChildNodes().removeNode(1)
    presentation.save("RemoveSmartArtNodeByPosition.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

Set a Custom Position for a Child Node in a SmartArt Object

Aspose.Slides for Python via Java supports setting the position of a SmartArtShape using setX and setY. The following example sets a custom position, size, and rotation for SmartArt node shapes. Adding new nodes recalculates the positions and sizes of all nodes. Custom positioning lets you arrange nodes as required.

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, SaveFormat, SmartArtLayoutType

presentation = Presentation("SimpleSmartArt.pptx")
try:
    smart_art = presentation.getSlides().get_Item(0).getShapes().addSmartArt(20, 20, 600, 500, SmartArtLayoutType.OrganizationChart)
    node = smart_art.getAllNodes().get_Item(1)
    shape = node.getShapes().get_Item(1)
    shape.setX(shape.getX() + shape.getWidth() * 2)
    shape.setY(shape.getY() - shape.getHeight() * 2)
    node = smart_art.getAllNodes().get_Item(2)
    shape = node.getShapes().get_Item(1)
    shape.setWidth(shape.getWidth() + shape.getWidth() * 2)
    node = smart_art.getAllNodes().get_Item(3)
    shape = node.getShapes().get_Item(1)
    shape.setHeight(shape.getHeight() + shape.getHeight() * 2)
    node = smart_art.getAllNodes().get_Item(4)
    shape = node.getShapes().get_Item(1)
    shape.setRotation(90)
    presentation.save("SmartArt.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

Check an Assistant Node

The following source SmartArt shape is used in this example.

SmartArt shape
Figure: Source SmartArt shape on a slide

The following example identifies assistant nodes in a SmartArt node collection and changes them to normal nodes.

  1. Create an instance of the Presentation class and load the presentation containing a SmartArt shape.
  2. Get the first slide by its index.
  3. Iterate through every shape on the first slide.
  4. Check whether the shape is a SmartArt instance.
  5. Iterate through all nodes in the SmartArt shape and check whether they are Assistant Nodes.
  6. Change each assistant node to a normal node.
  7. Save the presentation.
import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, SaveFormat, SmartArt

presentation = Presentation("AddNodes.pptx")
try:
    for shape in presentation.getSlides().get_Item(0).getShapes():
        if isinstance(shape, SmartArt):
            smart_art = shape
            for i in range(smart_art.getAllNodes().size()):
                node = smart_art.getAllNodes().get_Item(i)
                if node.isAssistant():
                    node.setAssistant(False)
    presentation.save("ChangeAssistantNode.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()
SmartArt shape
Figure: Assistant nodes changed in a SmartArt shape on a slide

Set a Node’s Fill Format

Aspose.Slides for Python via Java makes it possible to add custom SmartArt shapes and set their fill format. This article explains how to create and access SmartArt shapes and set their fill format using Aspose.Slides for Python via Java.

Please follow the steps below:

  1. Create an instance of the Presentation class.
  2. Get a slide by its index.
  3. Add a SmartArt shape with the ClosedChevronProcess layout.
  4. Set the FillFormat for the SmartArt shape nodes.
  5. Write the modified presentation as a PPTX file.
import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, SaveFormat, SmartArtLayoutType, FillType
from java.awt import Color

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)
    chevron = slide.getShapes().addSmartArt(10, 10, 800, 60, SmartArtLayoutType.ClosedChevronProcess)
    node = chevron.getAllNodes().addNode()
    node.getTextFrame().setText("Some text")
    for item in node.getShapes():
        item.getFillFormat().setFillType(FillType.Solid)
        item.getFillFormat().getSolidFillColor().setColor(Color.RED)
    presentation.save("TestSmart.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

Generate a Thumbnail of a SmartArt Child Node

To generate a thumbnail of a SmartArt child node, follow these steps:

  1. Create an instance of the Presentation class.
  2. Add a SmartArt shape.
  3. Get a node by its index.
  4. Get the thumbnail image.
  5. Save the thumbnail image in any desired image format.
import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import Presentation, SmartArtLayoutType, ImageFormat

presentation = Presentation()
try:
    smart_art = presentation.getSlides().get_Item(0).getShapes().addSmartArt(10, 10, 400, 300, SmartArtLayoutType.BasicCycle)
    node = smart_art.getNodes().get_Item(1)
    image = node.getShapes().get_Item(0).getImage()
    try:
        image.save("SmartArt_ChildNode_Thumbnail.png", ImageFormat.Png)
    finally:
        image.dispose()
finally:
    presentation.dispose()

FAQ

Is SmartArt animation supported?

Yes. SmartArt is treated as a regular shape, so you can apply standard animations (entrance, exit, emphasis, motion paths) and adjust timing. You can also animate shapes inside SmartArt nodes when needed.

How can I reliably locate a specific SmartArt on a slide if its internal ID is unknown?

Assign and search by alternative text. Setting distinctive alternative text on the SmartArt lets you find it programmatically without relying on internal identifiers.

Will the SmartArt appearance be preserved when converting the presentation to PDF?

Yes. Aspose.Slides renders SmartArt with high visual fidelity during PDF export, preserving layout, colors, and effects.

Can I extract an image of the entire SmartArt (for previews or reports)?

Yes. You can render a SmartArt shape to raster formats or to SVG for scalable vector output, making it suitable for thumbnails, reports, or web use.