1
votes

Here is what I am trying to learn how to do:

  • The company I work for has approx. 120+.pptx files that were all created manually over several years using a .potx PowerPoint template
  • A graphics designer created a snazzy new template
  • My boss assigned me the task of converting all 120+ decks from the outdated template into the new one

The old template had a dozen or so custom layouts w/ names like: Title Slide, Intro Slide, Generic Process, Blank, Comparison [...] The new template has updated layouts w/ the same names (roughly). In the XML data, I'm sure there is some kind of <layout_id> for each layout. I was hoping to be able to work with Python to extract what I need from the old presentations and generate new presentations with that data, but with the new layouts.

I have read through all the interactions on the python-pptx GitHub repository and watched this YouTube video on creating new Presentations using an existing template. Despite a full day of trial and much error, I am simply not far enough along in my learning get this project under control. Obviously, I do not expect full details, but any nudge(s) in the right direction would be incredibly appreciated.

1
Graphic designers have no idea how to create a new template that successfully accepts legacy slides. Here's my article on the 5 parameters that need to be managed for successful updating of legacy presentations: brandwares.com/bestpractices/2018/11/…John Korchok
Your link is a really nice essay on this issue John! I've admired your work for a while now and am glad to have found this additional piece I hadn't come across yet :)scanny

1 Answers

0
votes

Well, I expect there are several challenges ahead of you to get this working, and SO is a single-question/single-answer kind of place, so let's narrow this down to a first question which we can call "Mapping layouts between PPTX templates with python-pptx". It might be worth considering changing the question title to something like that.

You can of course ask further focused questions at your next step and the one after that, etc.

My understanding of the first problem is this:

For each slide in a source presentation, create a slide in a target presentation using the new-version slide layout that corresponds to the source slide layout.

I'm inclined to think this reduces to a function like this:

layout_map = {
    "Title Slide": "Title",
    "Intro Slide": "Introduction",
    "Generic Process": "Process",
    ...
}

def corresponding_slide_layout(slide, new_prs):
    legacy_layout_name = slide.slide_layout.name
    new_name = layout_map.get(legacy_layout_name)
    if new_name is None:
        # --- there's a gap in the mapping ---
        return None
    return new_prs.slide_layouts.get_by_name(new_name)

new_prs = Presentation("my-new-template.pptx")
for slide in prs.slides:
    layout = corresponding_slide_layout(slide, new_prs)
    if layout is None:
        layout = new_prs.slide_layouts[5]  # --- or whatever default ---
    new_slide = new_prs.slides.add_slide(layout)
    # --- then pass to other function you write to do the shape copying ---
    copy_slide_contents(slide, new_slide)

The general gist is that you map old-style layouts to new-style layouts by name and then use that mapping to choose the layout for each target slide. This should be pretty reliable with the possible addition of a default where there is either no clear mapping or perhaps a user edited an original name or something.