Python Automate SmartArt in PowerPoint (Create, Read, and Delete)

Casie LiuCasie Liu
5 min read

SmartArt is a popular feature in PowerPoint used to present structured information like processes, hierarchies, and cycles. If you frequently work with presentations, automating SmartArt tasks with Python can save time and reduce repetitive edits. In this guide, you’ll learn how to use Python to create, read, and delete SmartArt in PowerPoint—making it easier to manage visual elements and keep your slides up to date.

How to Create SmartArt in PowerPoint Using Python

In company presentations, SmartArt offers a more intuitive way to showcase information—like using an organizational chart to clearly illustrate team structure. Likewise, in project reports, flowchart-style SmartArt helps visualize processes, task sequences, and milestones, making it easier for your audience to follow. In this chapter, you’ll learn how to use Python to create SmartArt in PowerPoint, streamlining your workflow and enhancing the clarity of your slides.

In this guide, I’ll use Spire.Presentation, a professional and powerful Python PowerPoint library, as an example to demonstrate the process.

Steps to create SmartArt tasks in PowerPoint presentations:

  • Create an object of the Presentation class and read a PowerPoint document using the Presentation.LoadFromFile() method.

  • Get a slide through the Presentation.Slides[] property.

  • Insert a SmartArt graphic to the slide with the ISlide.Shapes.AppendSmartArt() method.

  • Customize the style and color of the SmartArt using the ISmartArt.Style and ISmartArt.ColorStyle properties.

  • Loop through the SmartArt and clear default nodes.

  • Add a node to the SmartArt through the ISmartArt.Nodes.AddNode() method, and insert sub-nodes through the ISmartArtNode.ChildNodes.AddNode() method.

  • Add text to nodes and sub-nodes with the ISmartArtNode.TextFrame.Text property.

  • Save the updated PowerPoint presentation using the Presentation.SaveToFile() method.

from spire.presentation.common import *
from spire.presentation import *

# Create a Presentation object
presentation = Presentation()
# Load a PowerPoint presentation
presentation.LoadFromFile("/sample.pptx")

# Get the first slide
slide = presentation.Slides[0]

# Add a SmartArt (Organization Chart) to the slide
smartArt = slide.Shapes.AppendSmartArt(200, 60, 500, 430, SmartArtLayoutType.OrganizationChart)

# Set style and color for the SmartArt
smartArt.Style = SmartArtStyleType.ModerateEffect
smartArt.ColorStyle = SmartArtColorType.ColorfulAccentColors5to6

# Remove the default nodes from the SmartArt
list = []
for node in smartArt.Nodes:
    list.append(node)
for subnode in list:
    smartArt.Nodes.RemoveNode(subnode)

# Add a node to the SmartArt
node = smartArt.Nodes.AddNode()

# Add two sub-nodes to the node
subNode1 = node.ChildNodes.AddNode()
subNode2 = node.ChildNodes.AddNode()

# Add two sub-nodes to the first sub-node
subsubNode1 = subNode1.ChildNodes.AddNode()
subsubNode2 = subNode1.ChildNodes.AddNode()
# Add two sub-nodes to the second sub-node
subsubNode3 = subNode2.ChildNodes.AddNode()
subsubNode4 = subNode2.ChildNodes.AddNode()

# Set text and font size for the node and sub-nodes
node.TextFrame.Text = "University President"
node.TextFrame.TextRange.FontHeight = 14.0
subNode1.TextFrame.Text = "Academic Dean"
subNode1.TextFrame.TextRange.FontHeight = 12.0
subNode2.TextFrame.Text = "Administrative Director"
subNode2.TextFrame.TextRange.FontHeight = 12.0
subsubNode1.TextFrame.Text = "Professor A"
subsubNode1.TextFrame.TextRange.FontHeight = 12.0
subsubNode2.TextFrame.Text = "Professor B"
subsubNode2.TextFrame.TextRange.FontHeight = 12.0
subsubNode3.TextFrame.Text = "Officer A"
subsubNode3.TextFrame.TextRange.FontHeight = 12.0
subsubNode4.TextFrame.Text = "Officer B"
subsubNode4.TextFrame.TextRange.FontHeight = 12.0

# Save the resulting presentation
presentation.SaveToFile("/InsertSmartArt.pptx", FileFormat.Pptx2016)
presentation.Dispose()

Create a SmartArt in PowerPoint with Python

Read Text from SmartArt in PowerPoint with Python

Sometimes, you may need to extract text from SmartArt elements—for example, to reuse process steps, organizational roles, or key messages elsewhere. With Spire.Presentation, this can be done easily using the ISmartArtNode.TextFrame.Text property, which allows you to access and read the text content from each node in the SmartArt. Here's how it works in Python.

Steps to read text from SmartArt in a PowerPoint presentation:

  • Create an instance of the Presentation class and load a PowerPoint document with the Presentation.LoadFromFile() method.

  • Rrtrieve a specific slide through the Presentation.Slides[] property.

  • Loop through all shapes on the slide.

  • Check if these shapes are of ISmartArt type. If so, iterate through all nodes in each SmartArt shape, then retrieve the text from each node using the ISmartArtNode.TextFrame.Text property and append the text to a list.

  • Save the text in the list as a TXT file.

Here is the code example showing how to extract text from a SmartArt on the first slide:

from spire.presentation.common import *
from spire.presentation import *

# Create a Presentation object
presentation = Presentation()
# Load a PowerPoint presentation
presentation.LoadFromFile("/InsertSmartArt.pptx")

# Get the first slide
slide = presentation.Slides[0]

# Create a list to store the extracted text
str = []
str.append("Text Extracted from SmartArt:")

# Loop through the shapes on the slide and find the SmartArt shapes
for shape in slide.Shapes:
    if isinstance(shape, ISmartArt):
        smartArt = shape
        # Extract text from the SmartArt shapes and append the text to the list
        for node in smartArt.Nodes:
            str.append(node.TextFrame.Text)

# Write the text in the list into a text file
with open("/ExtractTextFromSmartArt.txt", "w", encoding = "utf-8") as text_file:
    for text in str:
        text_file.write(text + "\n")

presentation.Dispose()

Python Read Text from SmartArt in PowerPoint Presentations

Delete SmartArt in PowerPoint Using Python

Sometimes you may need to update an outdated PowerPoint presentation or simplify its visual elements. In such cases, removing unnecessary SmartArt can help clean up the slides. With Python, you can easily delete any SmartArt using the ISlide.Shapes.Remove() method, once the target slide and shape are identified.

Steps to delete a SmartArt in PowerPoint presentations:

  • Instantiate a Presentation object and load a source PowerPoint document through the Presentation.LoadFromFile() method.

  • Get a specified slide with the Presentation.Slides[] property.

  • Loop through all shapes on the slide.

  • Check if shapes are of ISmartArt type, if so, append them to a list.

  • Loop through all SmartArt shapes in the list and remove them from the slide using the ISlide.Shapes.Remove() method.

  • Save the resulting PowerPoint presentation.

from spire.presentation.common import *
from spire.presentation import *

# Create a Presentation object
presentation = Presentation()
# Load a PowerPoint presentation
presentation.LoadFromFile("/InsertSmartArt.pptx")

# Get the first slide
slide = presentation.Slides[0]

# Create a list to store the SmartArt shapes
list = []

# Loop through all the shapes on the slide
for shape in slide.Shapes:
    # Find the SmartArt shapes and append them to the list
    if isinstance (shape, ISmartArt):
        list.append(shape)

# Remove the SmartArt from the slide
for smartArt in list:
    slide.Shapes.Remove(smartArt)


# Save the resulting presentation
presentation.SaveToFile("/DeleteSmartArt.pptx", FileFormat.Pptx2016)
presentation.Dispose()

The Conclusion

In this blog, we explored how to create, read, and delete SmartArt in PowerPoint using Python, with clear steps and practical code examples. Whether you're designing a visually engaging slide, extracting key details, or simplifying existing presentations, Python makes the process easier and more efficient. With just a few lines of code, you can streamline your workflow and save valuable time—give it a try today!

0
Subscribe to my newsletter

Read articles from Casie Liu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Casie Liu
Casie Liu