scanny / python-pptx

Create Open XML PowerPoint documents in Python
MIT License
2.4k stars 520 forks source link

I wanted to activate loop until stopped in playback tab but unable to find a way #871

Closed matt-song27 closed 1 year ago

matt-song27 commented 1 year ago

Hello,

I was able to create a new slide and add a video to the slide. I also wanted to make the video have loop until stopped option in playback tab activated using python-pptx. Unfortunately, I was unable to do so. Is there anyone who was able to make it?

Best, Matt

bowespublishing commented 1 year ago

Not particularly good with the XML side of things but this seems to do the trick...

from pptx import Presentation
from pptx.oxml.xmlchemy import OxmlElement, qn

presentation = Presentation("video.pptx")
for slide in presentation.slides:
    try:
        pic = slide._element
        rId = pic.xpath('./p:timing/p:tnLst/p:par/p:cTn/p:childTnLst/p:video/p:cMediaNode')[0]
        rId2 = pic.xpath('./p:timing/p:tnLst/p:par/p:cTn/p:childTnLst/p:video/p:cMediaNode/p:cTn')[0]
        rId.remove(rId2)
        jj = OxmlElement("p:cTn")
        jj.set("repeatCount","indefinite")
        rId.insert(0,jj)
    except:
        pass
presentation.save("videoloop.pptx")
matt-song27 commented 1 year ago

@bowespublishing Thank you for answering. I was able to implement it thanks to your help. I needed to make it work with multiple videos on a slide as below.

from pptx import Presentation
from pptx.oxml.xmlchemy import OxmlElement, qn

presentation = Presentation("video.pptx")

for slide in pres.slides:
    try:
        pic = slide._element
        videos1 = pic.xpath('./p:timing/p:tnLst/p:par/p:cTn/p:childTnLst/p:video/p:cMediaNode')
        videos2 = pic.xpath('./p:timing/p:tnLst/p:par/p:cTn/p:childTnLst/p:video/p:cMediaNode/p:cTn')

        if videos1 and videos2: # if at least a video is added
            for i in range(len(videos1)):
                rId = videos1[i]
                rId2 = videos2[i]
                rId.remove(rId2)
                jj = OxmlElement("p:cTn")
                jj.set("repeatCount","indefinite")
                rId.insert(0,jj)
        except:
            pass

presentation.save("videoloop.pptx")
MartinPacker commented 1 year ago

Thank you for this. I think in md2pptx I can expose this via the HTML video autoplay attribute.