Shapes are the fundamental building blocks that bring your PowerPoint slides to life. From simple geometric forms to complex icons and illustrations, these versatile visual elements enable you to add interest, highlight key information, and craft visually striking layouts. Whether you are creating professional-looking slides from scratch or enhancing existing ones, knowing how to insert and manipulate shapes is an essential skill. In this guide, we'll cover how to insert, rotate, resize, reposition, and reorder shapes in PowerPoint presentations in Python using Spire.Presentation for Python.
- Insert Shapes in PowerPoint in Python
- Rotate Shapes in PowerPoint in Python
- Resize and Reposition Shapes in PowerPoint in Python
- Reorder Shapes in 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
Insert Shapes in PowerPoint in Python
Spire.Presentation for Python enables you to add various types of shapes such as rectangles, circles, triangles, arrows, and eclipses to a PowerPoint slide by using the ISlide.Shapes.AppendShape() method.
Here are the steps to insert shapes in PowerPoint using Spire.Presentation for Python:
- Create an object of the Presentation class.
- Load a PowerPoint presentation using Presentation.LoadFromFile() method.
- Get a specific slide in the presentation using Presentation.Slides[index] property.
- Add various types of shapes to the slide using ISlide.Shapes.AppendShape() method and then set styles for the shapes.
- Save the resulting presentation using Presentation.SaveToFile() method.
- Python
from spire.presentation import * # Create an object of the Presentation class ppt = Presentation() # Get the first slide slide = ppt.Slides[0] # Add a triangle shape to the slide shape = slide.Shapes.AppendShape(ShapeType.Triangle, RectangleF.FromLTRB (115, 130, 215, 230)) shape.Fill.FillType = FillFormatType.Solid shape.Fill.SolidColor.Color = Color.get_LightGreen() shape.ShapeStyle.LineColor.Color = Color.get_White() # Add an ellipse shape to the slide shape = slide.Shapes.AppendShape(ShapeType.Ellipse, RectangleF.FromLTRB (290, 130, 440, 230)) shape.Fill.FillType = FillFormatType.Solid shape.Fill.SolidColor.Color = Color.get_LightSkyBlue() shape.ShapeStyle.LineColor.Color = Color.get_White() # Add a heart shape to the slide shape = slide.Shapes.AppendShape(ShapeType.Heart, RectangleF.FromLTRB (470, 130, 600, 230)) shape.Fill.FillType = FillFormatType.Solid shape.Fill.SolidColor.Color = Color.get_Red() shape.ShapeStyle.LineColor.Color = Color.get_LightGray() # Add a five-pointed star shape to the slide shape = slide.Shapes.AppendShape(ShapeType.FivePointedStar, RectangleF.FromLTRB (90, 270, 240, 420)) shape.Fill.FillType = FillFormatType.Gradient shape.Fill.SolidColor.Color = Color.get_Black() shape.ShapeStyle.LineColor.Color = Color.get_White() # Add a rectangle shape to the slide shape = slide.Shapes.AppendShape(ShapeType.Rectangle, RectangleF.FromLTRB (320, 290, 420, 410)) shape.Fill.FillType = FillFormatType.Solid shape.Fill.SolidColor.Color = Color.get_Pink() shape.ShapeStyle.LineColor.Color = Color.get_LightGray() # Add an arrow shape to the slide shape = slide.Shapes.AppendShape(ShapeType.BentUpArrow, RectangleF.FromLTRB (470, 300, 580, 400)) shape.Fill.FillType = FillFormatType.Gradient shape.Fill.Gradient.GradientStops.AppendByKnownColors(1, KnownColors.Olive) shape.Fill.Gradient.GradientStops.AppendByKnownColors(0, KnownColors.PowderBlue) shape.ShapeStyle.LineColor.Color = Color.get_Red() # Save the resulting presentation to a new file ppt.SaveToFile("InsertShapes.pptx", FileFormat.Pptx2010) ppt.Dispose()
Rotate Shapes in PowerPoint in Python
The IShape.Rotation property in Spire.Presentation for Python is used to rotate a shape on a PowerPoint slide. Setting this property to a positive value will rotate the shape clockwise, while setting it to a negative value will rotate the shape counterclockwise.
Here are the steps to rotate a shape in PowerPoint using Spire.Presentation for Python:
- Create an object of the Presentation class.
- Load a PowerPoint presentation using Presentation.LoadFromFile() method.
- Get a specific slide in the presentation using Presentation.Slides[index] property.
- Get a specific shape on the slide using ISlide.Shapes[index] property.
- Rotate the shape by specific degrees using IShape.Rotation property.
- Save the resulting presentation using Presentation.SaveToFile() method.
- Python
from spire.presentation import * # Create an object of the Presentation class ppt = Presentation() # Load a PowerPoint presentation ppt.LoadFromFile("ShapeTemplate1.pptx") # Get the first slide slide = ppt.Slides[0] # Get the first shape on the slide shape = slide.Shapes[0] if isinstance(slide.Shapes[0], IAutoShape) else None # Rotate the shape 180 degrees clockwise shape.Rotation = 180 # Save the resulting presentation to a new file ppt.SaveToFile("RotateShape.pptx", FileFormat.Pptx2016) ppt.Dispose()
Resize and Reposition Shapes in PowerPoint in Python
The size and position of a shape can be reset through the IShape.Height, IShape.Width and IShape.Left, IShape.Top properties.
Here are the steps to reset the size and position of shapes in PowerPoint using Spire.Presentation for Python:
- Create an object of the Presentation class.
- Load a PowerPoint presentation using Presentation.LoadFromFile() method.
- Get the original slide height and width using Presentation.SlideSize.Size.Height and Presentation.SlideSize.Size.Width properties.
- Change the slide size using Presentation.SlideSize.Type property, and then get the new slide height and width.
- Calculate the ratio for resetting the size and position of the shapes based on the original and new slide heights and widths.
- Iterate through the slides in the presentation and the shapes on each slide.
- Reset the size and position of each shape based on the specified ratio using IShape.Height, IShape.Width, IShape.Left and IShape.Top properties.
- Save the resulting presentation using Presentation.SaveToFile() method.
- Python
from spire.presentation import * # Create an object of the Presentation class ppt = Presentation() # Load a PowerPoint presentation ppt.LoadFromFile("ShapeTemplate.pptx") # Get the original slide height and width currentHeight = ppt.SlideSize.Size.Height currentWidth = ppt.SlideSize.Size.Width # Change the slide size to A3 ppt.SlideSize.Type = SlideSizeType.A3 # Get the new slide height and width newHeight = ppt.SlideSize.Size.Height newWidth = ppt.SlideSize.Size.Width # Calculate the ratio for resizing shapes based on the original and new slide heights and widths ratioHeight = newHeight / currentHeight ratioWidth = newWidth / currentWidth # Iterate through the slides in the presentation for slide in ppt.Slides: # Iterate through the shapes on the slide for shape in slide.Shapes: if isinstance(shape, IAutoShape): # Reset the size of the shape based on the specified ratio shape.Height = shape.Height * ratioHeight shape.Width = shape.Width * ratioWidth # Reset the position (x and y coordinates) of the shape based on the specified ratio shape.Top = shape.Top * ratioHeight shape.Left = shape.Left * ratioWidth # Save the resulting presentation to a new file ppt.SaveToFile("ResizeAndRepositionShapes.pptx", FileFormat.Pptx2016) ppt.Dispose()
Reorder Shapes in PowerPoint in Python
The order in which shapes are arranged determines which shapes appear in front of or behind others. Using the ISlide.Shapes.ZOrder() method, you can easily change the order of multiple overlapping shapes on a PowerPoint slide.
Here are the steps to change the order of shapes in PowerPoint using Spire.Presentation for Python:
- Create an object of the Presentation class.
- Load a PowerPoint presentation using Presentation.LoadFromFile() method.
- Get a specific slide in the presentation using Presentation.Slides[index] property.
- Get a specific shape on the slide using ISlide.Shapes[index] property.
- Change the stacking order of the shape using ISlide.Shapes.ZOrder() method.
- Save the resulting presentation using Presentation.SaveToFile() method.
- Python
from spire.presentation import * # Create an object of the Presentation class ppt = Presentation() # Load a PowerPoint presentation ppt.LoadFromFile("ShapeTemplate3.pptx") # Get the first slide slide = ppt.Slides[0] # Get the first shape on the slide shape = slide.Shapes[0] if isinstance(slide.Shapes[0], IAutoShape) else None # Change the stacking order of the shape slide.Shapes.ZOrder(1, shape) # Save the resulting presentation to a new file ppt.SaveToFile("ReorderShapes.pptx", FileFormat.Pptx2016) ppt.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.