DEV Community

Allen Yang
Allen Yang

Posted on

Add Animations to PowerPoint Shapes in Python

When creating PowerPoint presentations, animation effects can significantly enhance the visual appeal. By adding animations to shapes, text, and other elements, you can make your presentation more engaging, guide audience attention, and improve information delivery.

This article will introduce how to add animation effects to shapes in PowerPoint presentations using Python, including entrance animations, exit animations, and animation repeat settings.

Why Animation Effects Are Needed

There are several practical purposes for adding animations in presentations:

  • Guide Audience Attention: Control the appearance order of elements through animations to guide the audience to understand content in a logical sequence
  • Enhance Visual Appeal: Appropriate animation effects make presentations more professional and attractive
  • Step-by-Step Information Display: Gradually reveal complex content to avoid information overload
  • Improve Presentation Flow: Smooth transition effects make presentations more natural

Environment Setup

First, install the Spire.Presentation library:

pip install Spire.Presentation
Enter fullscreen mode Exit fullscreen mode

This library provides comprehensive PowerPoint document processing capabilities, supporting creation, editing, and conversion of presentation files.

Core Implementation

Basic Animation Application

The core steps for adding animations to shapes include: creating a presentation, adding shapes, and applying animation effects. The following code demonstrates how to create a rectangle with a "Faded Swivel" animation effect:

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

# Create a presentation instance
ppt = Presentation()

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

# Add a rectangle shape
shape = slide.Shapes.AppendShape(
    ShapeType.Rectangle, 
    RectangleF.FromLTRB(100, 150, 300, 230)
)

# Set shape fill color
shape.Fill.FillType = FillFormatType.Solid
shape.Fill.SolidColor.Color = Color.get_LightBlue()
shape.ShapeStyle.LineColor.Color = Color.get_White()

# Add text content
shape.AppendTextFrame("Animated Shape")

# Apply FadedSwivel animation effect
shape.Slide.Timeline.MainSequence.AddEffect(
    shape, 
    AnimationEffectType.FadedSwivel
)

# Save the file
ppt.SaveToFile("ApplyAnimationOnShape.pptx", FileFormat.Pptx2013)
ppt.Dispose()
Enter fullscreen mode Exit fullscreen mode

The result:

Add Animations to PowerPoint Shapes in Python

Key API Explanation:

  • Timeline.MainSequence: Accesses the main timeline sequence of the slide; all animation effects play according to this sequence
  • AddEffect(): Adds an animation effect to a specified shape, accepting a shape object and animation type parameter
  • AnimationEffectType: Enumeration type that defines all available preset animation effects

Entrance and Exit Animations

Animation effects can be divided into three types: Entrance, Exit, and Emphasis. By default, AddEffect() adds an entrance animation. By modifying the PresetClassType property, you can convert an entrance animation to an exit animation:

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

# Create a presentation
ppt = Presentation()
slide = ppt.Slides[0]

# Add a five-pointed star shape
starShape = slide.Shapes.AppendShape(
    ShapeType.FivePointedStar, 
    RectangleF.FromLTRB(250, 100, 450, 300)
)

# Set fill color
starShape.Fill.FillType = FillFormatType.Solid
starShape.Fill.SolidColor.KnownColor = KnownColors.LightBlue

# Add RandomBars entrance animation
effect = slide.Timeline.MainSequence.AddEffect(
    starShape, 
    AnimationEffectType.RandomBars
)

# Change animation type from entrance to exit
effect.PresetClassType = TimeNodePresetClassType.Exit

# Save the file
ppt.SaveToFile("AddExitAnimationForShape.pptx", FileFormat.Pptx2013)
ppt.Dispose()
Enter fullscreen mode Exit fullscreen mode

The result:

Add Exit Animations to PowerPoint Shapes in Python

Important Concepts:

  • TimeNodePresetClassType.Exit: Sets the animation effect to exit type; the shape will disappear on click or timed trigger
  • The same shape can have both entrance and exit animations, forming a complete appear-disappear sequence

Animation Repeat Settings

For animations that need to loop, you can configure them using the AnimationRepeatType property:

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

# Load an existing presentation
presentation = Presentation()
presentation.LoadFromFile("Animation.pptx")

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

# Access the animation sequence
animations = slide.Timeline.MainSequence

# Set the first animation to repeat until the slide ends
animations[0].Timing.AnimationRepeatType = AnimationRepeatType.UtilEndOfSlide

# Save the modified file
presentation.SaveToFile("SetAnimationRepeatType.pptx", FileFormat.Pptx2013)
presentation.Dispose()
Enter fullscreen mode Exit fullscreen mode

The result:

Set Animation Repeating for PowerPoint Shapes in Python

Repeat Type Options:

  • UtilEndOfSlide: Animation continues to repeat until switching to the next slide
  • Other options include fixed-number repeats, suitable for different presentation scenarios

Common Animation Effect Types

The AnimationEffectType enumeration provides a rich set of preset animation effects. Here are some common types:

Entrance Animations:

  • FadedSwivel: Fade in with rotation
  • RandomBars: Expand with random lines
  • FlyIn: Fly in
  • Float: Float appearance

Exit Animations:

  • Any entrance animation can be converted to an exit animation by setting PresetClassType to Exit

Emphasis Animations:

  • Pulse: Pulse effect
  • Spin: Spin emphasis
  • GrowShrink: Scale effect

Practical Techniques

Combining Multiple Animation Effects

You can add multiple animation effects to the same shape to create complex animation sequences:

# Add entrance animation
enterEffect = slide.Timeline.MainSequence.AddEffect(
    shape, 
    AnimationEffectType.FlyIn
)

# Add emphasis animation
emphasisEffect = slide.Timeline.MainSequence.AddEffect(
    shape, 
    AnimationEffectType.Pulse
)

# Add exit animation
exitEffect = slide.Timeline.MainSequence.AddEffect(
    shape, 
    AnimationEffectType.RandomBars
)
exitEffect.PresetClassType = TimeNodePresetClassType.Exit
Enter fullscreen mode Exit fullscreen mode

Animation Timing Control

You can precisely control animation playback timing through the Timing property:

# Set animation delay time (seconds)
effect.Timing.Duration = 2.0

# Set animation duration
effect.Timing.Delay = 0.5

# Set trigger method (click or automatic)
effect.Timing.TriggerType = EffectTriggerType.AfterPrevious
Enter fullscreen mode Exit fullscreen mode

Batch Processing Multiple Slides

Add animations to shapes across multiple slides in a loop:

for slide_index in range(ppt.Slides.Count):
    slide = ppt.Slides[slide_index]
    for shape in slide.Shapes:
        if shape.Type == ShapeType.Rectangle:
            slide.Timeline.MainSequence.AddEffect(
                shape, 
                AnimationEffectType.FadedSwivel
            )
Enter fullscreen mode Exit fullscreen mode

Summary

This article introduced the core techniques for adding animation effects to PowerPoint shapes using Python, including:

  • Basic methods for applying entrance animations
  • Creating exit animations by modifying PresetClassType
  • Setting animation repeat playback using AnimationRepeatType
  • Selection and application of common animation effect types
  • Combining multiple animation effects to create complex animation sequences

After mastering these techniques, you can create more visually appealing presentations and improve the efficiency and quality of information delivery through carefully designed animation effects.

In practical applications, it is recommended to choose appropriate animation effects based on presentation content and audience characteristics, avoiding excessive use of animations that may distract the audience. Moderate and appropriate animation design can significantly enhance the professional level of presentations.

Top comments (0)