SmartArt (1)
Python: Create, Read or Delete SmartArt in PowerPoint
2023-11-02 01:05:50 Written by support iceblueSmartArt in Microsoft PowerPoint is a valuable tool that allows users to create visually appealing diagrams, charts, and graphics to represent information or concepts. It provides a quick and easy way to transform plain text into visually engaging visuals, making it easier for the audience to understand and remember the information being presented. In this article, we will demonstrate how to create, read and delete SmartArt in a PowerPoint in Python using Spire.Presentation for Python.
- Create SmartArt in PowerPoint in Python
- Read SmartArt in PowerPoint in Python
- Delete SmartArt from PowerPoint in Python
Install Spire.Presentation for Python
This scenario requires Spire.Presentation for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.
pip install Spire.Presentation
If you are unsure how to install, please refer to this tutorial: How to Install Spire.Presentation for Python on Windows
Create SmartArt in PowerPoint in Python
Spire.Presentation for Python provides the ISlide.Shapes.AppendSmartArt() method to add a SmartArt graphic to a specific slide of a PowerPoint presentation. The detailed steps are as follows.
- Create an object of the Presentation class.
- Load a PowerPoint presentation using Presentation.LoadFromFile() method.
- Get a specified slide by its index using Presentation.Slides[index] property.
- Insert a SmartArt graphic into the specified slide using ISlide.Shapes.AppendSmartArt() method.
- Set the style and color of the SmartArt using ISmartArt.Style and ISmartArt.ColorStyle properties.
- Loop through the nodes in the SmartArt and remove all default nodes using ISmartArt.Nodes.RemoveNode() method.
- Add a node to the SmartArt using ISmartArt.Nodes.AddNode() method.
- Add two sub-nodes to the node using ISmartArtNode.ChildNodes.AddNode() method.
- Add two sub-nodes to the first sub-node and the second sub-node respectively using ISmartArtNode.ChildNodes.AddNode() method.
- Add text to each node and sub-node using ISmartArtNode.TextFrame.Text property, and then set the font size for the text using ISmartArtNode.TextFrame.TextRange.FontHeight property.
- Save the resulting presentation using Presentation.SaveToFile() method.
- Python
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 = "CEO" node.TextFrame.TextRange.FontHeight = 14.0 subNode1.TextFrame.Text = "Development Manager" subNode1.TextFrame.TextRange.FontHeight = 12.0 subNode2.TextFrame.Text = "Quality Assurance Manager" subNode2.TextFrame.TextRange.FontHeight = 12.0 subsubNode1.TextFrame.Text = "Developer A" subsubNode1.TextFrame.TextRange.FontHeight = 12.0 subsubNode2.TextFrame.Text = "Developer B" subsubNode2.TextFrame.TextRange.FontHeight = 12.0 subsubNode3.TextFrame.Text = "Tester A" subsubNode3.TextFrame.TextRange.FontHeight = 12.0 subsubNode4.TextFrame.Text = "Tester B" subsubNode4.TextFrame.TextRange.FontHeight = 12.0 # Save the resulting presentation presentation.SaveToFile("InsertSmartArt.pptx", FileFormat.Pptx2016) presentation.Dispose()
Read SmartArt in PowerPoint in Python
To read the text of SmartArt graphics on a PowerPoint slide, you need to find the SmartArt shapes on the slide, then loop through all nodes of each SmartArt shape, and finally retrieve the text of each node through the ISmartArtNode.TextFrame.Text property. The detailed steps are as follows.
- Create an object of the Presentation class.
- Load a PowerPoint presentation using Presentation.LoadFromFile() method.
- Get a specific slide using Presentation.Slides[index] property.
- Create a list to store the extracted text.
- Loop through all the shapes on the slide.
- Check if the shapes are of ISmartArt type. If the result is True, loop through all the nodes of each SmartArt shape, then retrieve the text from each node through ISmartArtNode.TextFrame.Text property and append the text to the list.
- Write the text in the list into a text file.
- Python
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()
Delete SmartArt from PowerPoint in Python
To delete SmartArt graphics from a PowerPoint slide, you need to loop through all the shapes on the slide, find the SmartArt shapes and then delete them from the slide using ISlide.Shapes.Remove() method. The detailed steps are as follows.
- Create an object of the Presentation class.
- Load a PowerPoint presentation using Presentation.LoadFromFile() method.
- Get a specific slide using Presentation.Slides[index] property.
- Create a list to store the SmartArt shapes.
- Loop through all the shapes on the slide.
- Check if the shapes are of ISmartArt type. If the result is True, append them to the list.
- Loop through the SmartArt shapes in the list, then remove them from the slide one by one using ISlide.Shapes.Remove() method.
- Save the resulting presentation using Presentation.SaveToFile() method.
- Python
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()
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.