Python Insert Image into the middle of an existing PowerPoint

Morganis picture Morganis · Mar 7, 2016 · Viewed 6.9k times · Source

I have an existing PowerPoint presentation with 20 slides. This presentation serves as an template with each slide having different backgrounds. I want to take this (existing) PowerPoint presentation, insert an image in slide number 4 (do nothing with the first 3) and save it as a new PowerPoint presentation.

This is what I have up until now. This code loads an existing presentation and saves it as a new one. Now I just need to know how to use this to insert an image to slide number 4 like described above.

Note: I am using normal Python.

from pptx import Presentation

def open_PowerPoint_Presentation(oldFileName, newFileName):
    prs = Presentation(oldFileName)
    #Here I guess I need to type something to complete the task.
    prs.save(newFileName)

open_PowerPoint_Presentation('Template.pptx', 'NewTemplate.pptx')

Answer

Moe picture Moe · Mar 7, 2016

I'm not really familiar with this module, but I looked at their quickstart

from pptx.util import Inches
from pptx import Presentation

def open_PowerPoint_Presentation(oldFileName, newFileName, img, left, top):
    prs = Presentation(oldFileName)
    slide = prs.slides[3]
    pic = slide.shapes.add_picture(img, left, top)
    prs.save(newFileName)

open_PowerPoint_Presentation('Template.pptx', 'NewTemplate.pptx',
                             'mypic.png', Inches(1), Inches(1))