In modern business presentations, SmartArt graphics serve as a powerful visualization tool that presents complex information, processes, and hierarchical structures in an intuitive manner. Whether it's organizational charts, flowcharts, or cycle diagrams, SmartArt helps audiences quickly grasp key information. This article demonstrates how to create and customize SmartArt graphics in PowerPoint presentations using Python, enabling automated generation of professional presentation documents.
Environment Setup
First, install the Spire.Presentation for Python library:
pip install Spire.Presentation
This library provides a comprehensive PowerPoint API that supports creating, editing, and saving PPT files, including full control over SmartArt graphics.
Basic Steps for Creating SmartArt Graphics
Initialize Presentation and Add SmartArt
The first step in creating SmartArt is to initialize a PowerPoint document object and then add a SmartArt shape to a slide. The position and size of the SmartArt are specified through coordinate parameters:
from spire.presentation import *
from spire.presentation.common import *
# Create a presentation object
presentation = Presentation()
# Get the first slide
slide = presentation.Slides[0]
# Add SmartArt graphic (position x=200, y=60, width=300, height=300)
# Using gear layout type
smartArt = slide.Shapes.AppendSmartArt(
200, 60, 300, 300, SmartArtLayoutType.Gear)
The AppendSmartArt method accepts five parameters: x-coordinate, y-coordinate, width, height, and layout type. Spire.Presentation supports various built-in layout types, including:
-
BasicCycle- Basic cycle diagram -
Process- Process flowchart -
Hierarchy- Hierarchical structure diagram -
Relationship- Relationship diagram -
Matrix- Matrix diagram -
Gear- Gear diagram
Choosing the appropriate layout type is crucial for effectively conveying information.
Set SmartArt Style and Color
After creating the SmartArt, you can enhance its visual appeal by setting styles and colors:
# Set SmartArt style to subtle effect
smartArt.Style = SmartArtStyleType.SubtleEffect
# Set color theme to gradient loop accent 3
smartArt.ColorStyle = SmartArtColorType.GradientLoopAccent3
Styles control the visual effects of shapes (such as shadows, reflections, glows, etc.), while color themes define the overall color scheme of the SmartArt. Available styles include:
-
SimpleFill- Simple fill -
SubtleEffect- Subtle effect -
ModerateEffect- Moderate effect -
IntenseEffect- Intense effect
Color themes offer dozens of preset color schemes that can be selected based on the overall design style of your presentation.
Managing SmartArt Nodes
Clear Default Nodes
Newly created SmartArt typically contains default nodes. In practical applications, we usually need to clear these default nodes and then add custom content:
# Collect all nodes to be removed
nodes_to_remove = []
for node in smartArt.Nodes:
nodes_to_remove.append(node)
# Remove all default nodes
for node in nodes_to_remove:
smartArt.Nodes.RemoveNode(node)
This two-step deletion method is necessary because directly deleting nodes while iterating through the collection would cause iterator errors. Adding nodes to a temporary list first and then deleting them one by one ensures operation stability.
Add Custom Nodes and Set Text
After clearing the default nodes, you can add custom nodes and set their text content:
# Add the first node
node1 = smartArt.Nodes.AddNode()
node1.TextFrame.Text = "Data Collection"
# Add the second node
node2 = smartArt.Nodes.AddNode()
node2.TextFrame.Text = "Data Processing"
# Set text style
node2.TextFrame.TextRange.Fill.FillType = FillFormatType.Solid
node2.TextFrame.TextRange.Fill.SolidColor.KnownColor = KnownColors.Black
Each node contains a TextFrame object for managing the node's text content. Through the TextRange property, you can further control text formatting, including font, color, size, and more.
Modify Text of Existing Nodes
In addition to adding new nodes, you can also modify the text content of existing nodes:
# Load an existing presentation with SmartArt
presentation.LoadFromFile("existing_presentation.pptx")
# Iterate through all shapes on the slide
for shape in presentation.Slides[0].Shapes:
if isinstance(shape, ISmartArt):
# Get the second root node (index starts from 0)
target_node = shape.Nodes[1]
# Modify node text
target_node.TextFrame.Text = "Updated Content"
This approach is particularly useful for batch updating SmartArt content in presentations, such as dynamically generating reports based on data sources.
Advanced Customization Techniques
Adjust SmartArt Color Themes
You can dynamically adjust SmartArt color themes based on different scenario requirements:
# Load presentation
presentation.LoadFromFile("template.pptx")
# Iterate through all SmartArt graphics
for shape in presentation.Slides[0].Shapes:
if isinstance(shape, ISmartArt):
# Check current color type
if shape.ColorStyle == SmartArtColorType.ColoredFillAccent1:
# Change to colorful accent colors theme
shape.ColorStyle = SmartArtColorType.ColorfulAccentColors
This conditional approach allows intelligent adjustments based on existing styles, avoiding overwriting settings that users have already customized.
Add Nodes by Position
For certain layout types, you can specify the position of new nodes:
# Add child node at a specific position
parent_node = smartArt.Nodes[0]
new_node = parent_node.ChildNodes.AddNodeByPosition(PositionType.After)
new_node.TextFrame.Text = "Sub-process"
This method is very useful when creating hierarchical structures or flowcharts, allowing precise control over node hierarchy relationships and arrangement order.
Set Node Outline Styles
To enhance visual effects, you can set outline styles for SmartArt nodes:
# Set SmartArt node outlines
for node in smartArt.Nodes:
node.Shape.Line.FillType = FillFormatType.Solid
node.Shape.Line.SolidFillColor.Color = Color.get_Blue()
node.Shape.Line.Width = 2.0
Outline settings make SmartArt more clearly visible against different backgrounds, especially improving readability during projected presentations.
Practical Application Example
The following complete example demonstrates how to create a business process SmartArt graphic:
from spire.presentation import *
from spire.presentation.common import *
def create_process_smartart():
# Create presentation
presentation = Presentation()
slide = presentation.Slides[0]
# Add process-type SmartArt
smartArt = slide.Shapes.AppendSmartArt(
100, 100, 500, 200, SmartArtLayoutType.BasicProcess)
# Set style
smartArt.Style = SmartArtStyleType.ModerateEffect
smartArt.ColorStyle = SmartArtColorType.ColorfulAccentColors
# Clear default nodes
nodes_to_remove = list(smartArt.Nodes)
for node in nodes_to_remove:
smartArt.Nodes.RemoveNode(node)
# Add business process nodes
steps = ["Requirements Analysis", "System Design", "Development", "Testing", "Deployment"]
for step in steps:
node = smartArt.Nodes.AddNode()
node.TextFrame.Text = step
node.TextFrame.TextRange.FontHeight = 14
# Save file
presentation.SaveToFile("business_process.pptx", FileFormat.Pptx2010)
presentation.Dispose()
create_process_smartart()
Result preview:
This example creates a five-step business process diagram, with each step clearly labeled and appropriately sized fonts. By modifying the steps list, you can easily generate flowcharts with different quantities and content.
Practical Tips Summary
When working with SmartArt, keep the following points in mind:
Layout Selection: Choose the appropriate layout based on information type. Flowcharts suit linear processes, hierarchies suit organizational relationships, and cycle diagrams suit periodic processes.
Node Quantity Control: Avoid too many nodes in a single SmartArt. Typically, 5-7 nodes work best; excessive nodes reduce readability.
Color Consistency: Maintain consistency between SmartArt colors and the overall presentation style. Avoid using overly bright or conflicting color combinations.
Text Conciseness: Node text should be brief and concise, avoiding long sentences. If content is extensive, consider using notes or separate explanatory slides.
Automated Batch Generation: Combined with data sources (such as Excel or databases), you can automatically generate multiple SmartArt graphics, significantly improving report production efficiency.
Conclusion
Through Python and Spire.Presentation, we can efficiently create and customize SmartArt graphics to achieve automated generation of presentation documents. This approach is particularly suitable for scenarios requiring regular report generation, flowchart updates, or batch creation of training materials. Mastering the programming control techniques of SmartArt will significantly enhance office automation levels and work efficiency.
With deeper understanding of the API, you can further explore more complex customization features, such as node animations, intelligent layout adjustments, and integration with external data sources, injecting more dynamic and interactive elements into your presentation documents.


Top comments (0)