feature: Slide.duplicate()

See original GitHub issue

In order to create a presentation by populating template slides with dynamic data As a developer using python-pptx I need the ability to clone a slide

API suggestion:

cloned_slide = prs.slides.clone_slide(original_slide)

The cloned slide would be appended to the end of the presentation and would be functionally equivalent to copying and pasting a slide using the PPT GUI.

Issue Analytics

  • State:open
  • Created 9 years ago
  • Reactions:30
  • Comments:67 (15 by maintainers)

github_iconTop GitHub Comments

7reactions
nshgraphcommented, Aug 17, 2018

FWIW, this is how I added chart support:

from pptx.parts.chart import ChartPart
from pptx.parts.embeddedpackage import EmbeddedXlsxPart

def _get_blank_slide_layout(pres):
    layout_items_count = [len(layout.placeholders)
                          for layout in pres.slide_layouts]
    min_items = min(layout_items_count)
    blank_layout_id = layout_items_count.index(min_items)
    return pres.slide_layouts[blank_layout_id]

def duplicate_slide(pres, index):
    """Duplicate the slide with the given index in pres.

    Adds slide to the end of the presentation"""
    source = pres.slides[index]
    blank_slide_layout = _get_blank_slide_layout(pres)
    dest = pres.slides.add_slide(blank_slide_layout)

    for shape in source.shapes:
        newel = copy.deepcopy(shape.element)
        dest.shapes._spTree.insert_element_before(newel, 'p:extLst')

    for key, value in source.part.rels.items():
        # Make sure we don't copy a notesSlide relation as that won't exist
        if "notesSlide" not in value.reltype:
            target = value._target
            # if the relationship was a chart, we need to duplicate the embedded chart part and xlsx
            if "chart" in value.reltype:
                partname = target.package.next_partname(
                    ChartPart.partname_template)
                xlsx_blob = target.chart_workbook.xlsx_part.blob
                target = ChartPart(partname, target.content_type,
                                   copy.deepcopy(target._element), package=target.package)

                target.chart_workbook.xlsx_part = EmbeddedXlsxPart.new(
                    xlsx_blob, target.package)

            dest.part.rels.add_relationship(value.reltype,
                                            target,
                                            value.rId)

    return dest
4reactions
NarenZencommented, Feb 17, 2021

Has anyone tried duplicating the same slide multiple times into a new pptx file? The code from @nshgraph works for me if I duplicate a slide only one time, although I always get a prompt to let powerpoint repair the file. When duplicating more than once, the first slide will be shown but the others are just blank… I also get a lot of these error warnings when saving to pptx. warnings

@marcomilov Did you solve the problem. I’m also getting duplicate warning and unable to open in Microsoft. How can we skip saving SlideLayouts.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Slide.Duplicate method (PowerPoint) - Microsoft Learn
Creates a duplicate of the specified Slide object, adds the new slide to the Slides collection immediately after the slide specified ...
Read more >
PowerPoint 2016 - Duplicate a Slide - MS Office 365 Tutorial
PowerPoint 2016 - Duplicate a Slide - How to Make and Copy Slides in MS PPT ... and Font Color The NOW Function...
Read more >
Google Slide API-How do i duplicate a slide several times and ...
You want to copy the master slide several times in Google Slides. ... unique ID here. var requests = newIDs.reverse().map(function(id) { var ...
Read more >
Create a copy of a Google Slides presentation using Apps Script
You can get the Id from the URL of the presentation. https://docs.google.com/presentation/d/<SLIDES_ID>/edit. function createCopyUsingDriveApp() { // The Id of ...
Read more >
Class Slide | Apps Script - Google Developers
You can specify which layout to use for each slide when it is created. Methods. Method, Return type, Brief description. duplicate() ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found