0

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.

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

4 Comments

See my answer to this very recent question.
Will you please give me whole code, because I don't know where to insert your code in my code.
Well the repo I pointed you to has the whole code but for this fragment you just need to have created an empty slide - which is called slide. It has an attribute shapes when you create it. The rest, I hope, is obvious. (The slide doesn't HAVE to be empty, of course.)
I am new to python-pptx, If possible, please try to update your answer with full code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.