Abhishek Kamal
Abhishek Kamal

Reputation: 798

python error - AttributeError: 'SlidePlaceholder' object has no attribute 'insert_picture'

I am writing a python script to create a ppt file of images and text.
Each slide will contain a heading, subheading and below of it will contain the image as shown in picture below. enter image description here I run code but it is giving me error -
AttributeError: 'SlidePlaceholder' object has no attribute 'insert_picture'
Code is -

from pptx import Presentation
import os

prs = Presentation()

class MySlide:
    def __init__(self, data):
        self.layout = prs.slide_layouts[data[3]]
        self.slide=prs.slides.add_slide(self.layout)
        self.title=self.slide.shapes.title
        self.title.text=data[0]
        self.subtitle=self.slide.placeholders[1]
        self.subtitle.text=data[1]
        if data[2] != "":
            self.slide.placeholders[2].insert_picture(data[2])

slides = [
    ["Sample Title 1",       #data[0]
     "Sample Subtitle 1",
     "image1.jpg",
     3],
    ["Sample Title 2",       #data[0]
     "Sample Subtitle 2",
     "image2.jpg",
     3],
    ["Sample Title 3",       #data[0]
     "Sample Subtitle 3",
     "image3.jpg",
     3]
]

for each_slide in slides:
    MySlide(each_slide)

prs.save("stack.pptx")
os.startfile("stack.pptx")

Currently, code has 3 slides.

Upvotes: 0

Views: 3588

Answers (2)

Abhishek Kamal
Abhishek Kamal

Reputation: 798

Thanks for helping me, and I resolved my query.

from pptx import Presentation
from pptx.util import Inches

prs = Presentation()

#positioning image
top = Inches(2.5)
left = Inches(1)
height = Inches(4.5)

###############################
class MySlide:
    def __init__(self, data):
        #preparing layout
        self.layout = prs.slide_layouts[data[3]]
        #adding slide
        self.slide= prs.slides.add_slide(self.layout)
        #working with heading
        self.heading= self.slide.shapes.title
        self.heading.text= data[0]
        #working with sub_heading
        self.sub_heading=self.slide.placeholders[1]
        self.sub_heading.text=data[1]
        
        if data[2] != "":
            #adding image
            self.slide.shapes.add_picture(data[2], left, top, height=height)

#end of class

slides = [
    ["heading 1",       #data[0]
     "sub_heading 1",
     "image1.jpg",
     1],
    ["heading 2",       #data[0]
     "sub_heading 2",
     "image2.jpg",
     1],
    ["heading 3",       #data[0]
     "sub_heading 3",
     "image3.jpg",
     1],
     ["heading 4",       #data[0]
     "sub_heading 4",
     "image4.jpg",
     1],
     ["heading 5",       #data[0]
     "sub_heading 5",
     "image5.jpg",
     1]
]


for each_slide in slides:
    MySlide(each_slide)


#saving pptx
prs.save('result.pptx')


and output will look like as -
enter image description here

Upvotes: 1

Martin Packer
Martin Packer

Reputation: 763

It's add_picture().

By the way, if you want a pre-built project that does this - using Markdown - then check out my md2pptx GitHub open source project. Sorry if this seems like an advert - but you look like you're trying to solve a problem I already solved. In any case the code might inspire you.

EDIT: I forgot to mention md2pptx uses python-pptx.

Upvotes: 0

Related Questions