Python-pptx: copy slide

前端 未结 6 1035
谎友^
谎友^ 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条回答
  •  旧时难觅i
    2020-12-16 06:41

    Since I also found another usecase for the code shared by @d_bergeron, I just wanted to share it here. In my case, I wanted to copy a slide from another presentation into the one I generated with python-pptx:

    As argument I pass in the Presentation() object I created using python-pptx (prs = Presenation()).

    from pptx import Presentation
    import copy
    
    def copy_slide_from_external_prs(prs):
    
        # copy from external presentation all objects into the existing presentation
        external_pres = Presentation("PATH/TO/PRES/TO/IMPORT/from.pptx")
    
        # specify the slide you want to copy the contents from
        ext_slide = external_pres.slides[0]
    
        # Define the layout you want to use from your generated pptx
        SLD_LAYOUT = 5
        slide_layout = prs.slide_layouts[SLD_LAYOUT]
    
        # create now slide, to copy contents to 
        curr_slide = prs.slides.add_slide(slide_layout)
    
        # now copy contents from external slide, but do not copy slide properties
        # e.g. slide layouts, etc., because these would produce errors, as diplicate
        # entries might be generated
    
        for shp in ext_slide.shapes:
            el = shp.element
            newel = copy.deepcopy(el)
            curr_slide.shapes._spTree.insert_element_before(newel, 'p:extLst')
    
        return prs
    

    I am mainly posting it here, since I was looking for a way to copy an external slide into my presentation and ended up in this thread.

提交回复
热议问题