Skip to content
Advertisement

Python PPTX workaround to add Transitions to slides

I successfully automated the creation of pptx presentations using python-pptx, customising background, inserting text, images, etc.

How can I add custom Transitions to my slides? (E.g. “Transitions” > “Fade” from PowerPoint). As I could not find a function, my idea is to use workaround functions (going deep into xml): where do I start?

python 3.10.4, PowerPoint v16.54, MacOS Big Sur 11.6

enter image description here

Advertisement

Answer

So, on playing with this, the following worked for me:

    xml = '''
      <mc:AlternateContent xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
    <mc:Choice xmlns:p14="http://schemas.microsoft.com/office/powerpoint/2010/main" Requires="p14">
      <p:transition xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" spd="slow" p14:dur="3400">
        <p14:ripple />
      </p:transition>
    </mc:Choice>
    <mc:Fallback>
      <p:transition xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" spd="slow">
        <p:fade />
      </p:transition>
    </mc:Fallback>
  </mc:AlternateContent>
   '''
    xmlFragment = parse_xml(xml)
    slide.element.insert(-1, xmlFragment)

Where slide is the slide object in python-pptx.

You probably need the following import:

from pptx.oxml import parse_xml

Where I have ripple I first tested with reveal – and that worked as well. I’m sure there are other transitions. Before I attempt to add them to md2pptx I will want to find some more and figure out what kind of UI I want to surface them with.

Hope this helps.

(Edited for grammar.)

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement