Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add or Remove Shapes in PowerPoint Slides using Python
import aspose.slides as slides
import aspose.pydrawing as drawing
# Create presentation
with slides.Presentation() as presentation:
# Access shapes collection for selected slide
shapes = presentation.slides[0].shapes
# Add ellipse
ellipse = shapes.add_auto_shape(slides.ShapeType.ELLIPSE, 0, 100, 100, 100)
# Add auto shape rectangle
rectangle = shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 100, 300, 100, 100)
# Add connector to connect shapes
connector = shapes.add_connector(slides.ShapeType.BENT_CONNECTOR2, 0, 0, 10, 10)
# Join shapes using connector
connector.start_shape_connected_to = ellipse
connector.end_shape_connected_to = rectangle
# Call reroute to set the automatic shortest path between shapes
connector.reroute()
# Save presenation
presentation.save("add-connector.pptx", slides.export.SaveFormat.PPTX)
import aspose.slides as slides
import aspose.pydrawing as drawing
# Create a new presentation
with slides.Presentation() as pres:
# Get the first slide
sld = pres.slides[0]
# Add auto shape of ellipse type
shp = sld.shapes.add_auto_shape(slides.ShapeType.ELLIPSE, 50, 150, 150, 50)
# Apply some formatting to ellipse shape
shp.fill_format.fill_type = slides.FillType.SOLID
shp.fill_format.solid_fill_color.color = drawing.Color.pink
# Apply some formatting to the line of Ellipse
shp.line_format.fill_format.fill_type = slides.FillType.SOLID
shp.line_format.fill_format.solid_fill_color.color = drawing.Color.purple
shp.line_format.width = 5
# Save the PPTX file to disk
pres.save("add-ellipse.pptx", slides.export.SaveFormat.PPTX)
import aspose.slides as slides
# Load presentation
with slides.Presentation("add-ellipse.pptx") as presentation:
# Access shapes of the source slide
sourceShapes = presentation.slides[0].shapes
# Add a new blank slide
blankLayout = presentation.masters[0].layout_slides.get_by_type(slides.SlideLayoutType.BLANK)
destSlide = presentation.slides.add_empty_slide(blankLayout)
# Access shapes of destination slide
destShapes = destSlide.shapes
# Clone shapes
destShapes.add_clone(sourceShapes[0], 50, 150 + sourceShapes[0].height)
destShapes.add_clone(sourceShapes[0])
destShapes.insert_clone(0, sourceShapes[0], 50, 150)
# Save file
presentation.save("clone-shapes.pptx", slides.export.SaveFormat.PPTX)
# Load presentation
with slides.Presentation("add-ellipse.pptx") as presentation:
# Get the first slide
sld = presentation.slides[0]
alttext = "User Defined"
# Loop through shapes
for i in range(len(sld.shapes)):
# Find shape by alternative text
ashp = sld.shapes[0]
if ashp.alternative_text == alttext:
sld.shapes.remove(ashp)
# Save file
presentation.save("remove-shapes.pptx", slides.export.SaveFormat.PPTX)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment