Python: Add or Extract Animations in PowerPoint

2024-08-23 01:02:32 Written by  support iceblue
Rate this item
(0 votes)

In Microsoft PowerPoint, animations are not limited to just text; they can also be applied to shapes or other objects to create dynamic and engaging slides. Animations can be used to achieve various effects, such as drawing attention to a specific shape, demonstrating a process, or simply adding a touch of flair to your presentation. For instance, you might want to animate a shape to make it appear, disappear, or move in a particular sequence. Additionally, extracting and reusing animations can save time and ensure consistency across multiple presentations. In this article, we will demonstrate how to add animations to shapes in PowerPoint along with how to extract animation information from slides in PowerPoint in Python using Spire.Presentation for 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

Add Animations to Shapes and Text within Shapes in PowerPoint in Python

You can use the IShape.Slide.Timeline.MainSequence.AddEffect(shape:IShape, animationEffectType:AnimationEffectType) method to add an animation effect to a shape. If you want to apply the animation effect to the text of a specific paragraph(s) within a shape, you can use the AnimationEffect.SetStartEndParagraph(startParaIndex:int, endParaIndex:int) method. The detailed steps are as follows.

  • Create an instance of the Presentation class.
  • Load a PowerPoint presentation using the Presentation.LoadFromFile() method.
  • Access a specific slide using the Presentation.Slides[] property.
  • Add a rectangle shape to the slide using the ISlide.Shapes.AppendShape(shapeType:ShapeType, rectangle:RectangleF) method.
  • Set the fill type, fill color, and border color for the rectangle.
  • Add a text frame to the rectangle using the IShape.AppendTextFrame() method.
  • Add an animation effect to the rectangle using IShape.Slide.Timeline.MainSequence.AddEffect(shape:IShape, animationEffectType:AnimationEffectType) method.
  • Add another animation effect to the rectangle. Then apply the animation effect to specific paragraph(s) within the rectangle using the AnimationEffect.SetStartEndParagraph(startParaIndex:int, endParaIndex:int) method.
  • Save the result presentation using the Presentation.SaveToFile() method.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create an object of the Presentation class
ppt = Presentation()
# Get the first slide in the presentation
slide = ppt.Slides[0]

# Add a rectangle shape to the slide
shape = slide.Shapes.AppendShape(ShapeType.Rectangle, RectangleF.FromLTRB(100, 150, 300, 230))
# Set an alternative title for the shape (optional)
shape.AlternativeTitle = "Rectangle"

# Set the fill type, fill color and border color for the shape
shape.Fill.FillType = FillFormatType.Solid
shape.Fill.SolidColor.Color = Color.get_LightBlue()
shape.ShapeStyle.LineColor.Color = Color.get_White()

# Add a text frame to the shape and set the text content
shape.AppendTextFrame("Animated Shape")

# Add the 'fade-out swivel' animation effect to the shape
shape.Slide.Timeline.MainSequence.AddEffect(shape, AnimationEffectType.FadedSwivel)

# Add the 'float' animation effect to the shape
animation = shape.Slide.Timeline.MainSequence.AddEffect(shape, AnimationEffectType.Float)
# Set the start and end index of the paragraph(s) to apply the 'float' animation
animation.SetStartEndParagraphs(0, 0)

# Save the presentation to a new file
ppt.SaveToFile("ApplyAnimation.pptx", FileFormat.Pptx2013)
ppt.Dispose()

Python: Add or Extract Animations in PowerPoint

Add Exit Animations to Shapes in PowerPoint in Python

In PowerPoint, animations are categorized into four main types: entrance, emphasis, exit, and motion paths. Some animations, like "fly in" or "fade", can be used as both entrance and exit effects. When using Spire.Presentation to add these animations to shapes in your presentations, these animations are typically set as entrance effects by default. If you want to change the type of the animation to exit, you can use the AnimationEffect.PresetClassType property. The detailed steps are as follows.

  • Create an instance of the Presentation class.
  • Load a PowerPoint presentation using the Presentation.LoadFromFile() method.
  • Access a specific slide using the Presentation.Slides[] property.
  • Add a cube shape to the slide using the ISlide.Shapes.AppendShape(shapeType:ShapeType, rectangle:RectangleF) method.
  • Set the fill type, fill color, and border color for the cube.
  • Add a text frame to the cube using the IShape.AppendTextFrame() method.
  • Add an animation effect to the cube using the IShape.Slide.Timeline.MainSequence.AddEffect(shape:IShape, animationEffectType:AnimationEffectType) method.
  • Change the animation effect type to exit using the AnimationEffect.PresetClassType property.
  • Save the presentation using the Presentation.SaveToFile() method.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create an object of the Presentation class
ppt = Presentation()
# Get the first slide in the presentation
slide = ppt.Slides[0]

# Add a cube shape to the slide
shape = slide.Shapes.AppendShape(ShapeType.Cube, RectangleF.FromLTRB(100, 150, 300, 230))
# Set an alternative title for the shape (optional)
shape.AlternativeTitle = "Cube"

# Set the fill type, fill color and border color for the shape
shape.Fill.FillType = FillFormatType.Solid
shape.Fill.SolidColor.Color = Color.get_LightBlue()
shape.ShapeStyle.LineColor.Color = Color.get_White()

# Add a text frame to the shape and set the text content
shape.AppendTextFrame("Exit Animation")

# Add a 'random bars' animation effect to the shape
effect = shape.Slide.Timeline.MainSequence.AddEffect(shape, AnimationEffectType.RandomBars)
# Set the animation effect type to exit animation
effect.PresetClassType = TimeNodePresetClassType.Exit

# Save the presentation to a new file
ppt.SaveToFile("ExitAnimation.pptx", FileFormat.Pptx2013)
ppt.Dispose()

Python: Add or Extract Animations in PowerPoint

Extract the Animation Information from PowerPoint Slides in Python

To extract animation information from slides in a PowerPoint presentation, you need to iterate through all slides and all animations within each slide, then use the properties of the AnimationEffect class to retrieve the information of the animations. The detailed steps are as follows.

  • Create an instance of the Presentation class.
  • Load a PowerPoint presentation using the Presentation.LoadFromFile() method.
  • Iterate through all slides in the presentation and all animations within each slide.
  • Use the AnimationEffect.ShapeTarget.AlternativeTitle property to get the title of the shape affected by the animation.
  • Use the ISlide.SlideNumber property to get the number of the current slide.
  • Use the AnimationEffect.AnimationEffectType property to get the type of animation effect.
  • Use the AnimationEffect.Timing.Duration property to get the duration of the animation effect.
  • Use the AnimationEffect.Timing.RepeatCount property to get the number of repetitions of the animation effect.
  • Save the retrieved information to a text file.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create an object of the Presentation class
ppt = Presentation()
# Load a PowerPoint presentation
ppt.LoadFromFile("ApplyAnimation.pptx")

# Create a list to store the extracted animation information
sb = []

# Iterate through all slides in the presentation
for slide in ppt.Slides:   
    # Iterate through all animation effects in the slide
    for effect in slide.Timeline.MainSequence:       
        # Get the alternative title of the shape affected by the animation
        shapeTitle = effect.ShapeTarget.AlternativeTitle
        sb.append("Shape Title: " + shapeTitle)       
        # Get the number of the current slide
        slideNumber = slide.SlideNumber
        sb.append("Current Slide Number: " + str(slideNumber))
        # Get the type of the animation effect
        animationEffectType = effect.AnimationEffectType
        sb.append("Animation Effect Type: " + str(animationEffectType))        
        # Get the duration of the animation effect
        duration = effect.Timing.Duration
        sb.append("Animation Effect Duration: " + str(duration))
        # Get the number of repetitions of the animation effect
        count = effect.Timing.RepeatCount
        sb.append("Animation Effect Repeat Count: " + str(count))       
        sb.append("\n")

# Save the extracted animation information to a text file
with open("AnimationInformation.txt", "w") as fp:
    for s in sb:
        fp.write(s + "\n")

ppt.Dispose()

Python: Add or Extract Animations in PowerPoint

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.

Additional Info

  • tutorial_title:
Last modified on Friday, 23 August 2024 01:12