Python-pptx: copy slide

前端 未结 6 1027
谎友^
谎友^ 2020-12-16 05:59

How can I copy slide?

I created a template slide and I need to copy it and edit shapes of each copy separately.

Or how I can add my template slide to present

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-16 06:50

    I wanted to present my workaround to copy slides. I use a template ppt and populate it. I know before populating the slides which slides of the template need to be copied and how often. What I then do is copying the slides and saving the new ppt with the copied slides. After saving I can open the ppt with the copied slides and use pptx to populate the slides.

    import win32com.client
    ppt_instance = win32com.client.Dispatch('PowerPoint.Application')
    #open the powerpoint presentation headless in background
    read_only = True
    has_title = False
    window    = False
    prs = ppt_instance.Presentations.open('path/ppt.pptx',read_only,has_title,window)
    
    nr_slide = 1
    insert_index = 1
    prs.Slides(nr_slide).Copy()
    prs.Slides.Paste(Index=insert_index)
    
    prs.SaveAs('path/new_ppt.pptx')
    prs.Close()
    
    #kills ppt_instance
    ppt_instance.Quit()
    del ppt_instance
    

    In this case the firste slide would be copied of the presentation and inserted after the first slide of the same presentation.

    Hope this helps some of you!

提交回复
热议问题