Reputation: 45
Simple problem; hard fix. I'm trying to make a Python .pptx. Whenever I use generic PowerPoint templates, the code works. Whenever I reference a slide from a slide template, my presentation crashes and deletes. Why does this happen?
This code works.
from pptx import Presentation
from pptx.util import Inches
img_path = 'monty-truth.png'
prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
left = top = Inches(1)
pic = slide.shapes.add_picture(img_path, left, top)
prs.save('test.pptx')
This code does not work.
from pptx import Presentation
from pptx.util import Inches
prs = Presentation('End_of_Programme Report Master DRAFT.pptx')
# Title
title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)
title = slide.shapes.title
title.text = coursename
img_path = 'path.png'
prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
left = top = Inches(1)
pic = slide.shapes.add_picture(img_path, left, top)
prs.save('test.pptx')
Upvotes: 0
Views: 100
Reputation: 6288
Since not all slides have title placeholder, you should verify that slide.shapes.title
is not None.
Upvotes: 1