0

I have created ppt using the below code:

prs = Presentation()

class MySlide:
    def __init__(self, data):
        self.layout = prs.slide_layouts[data[2]]
        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]

slides = [
    ["USA Weather",       #data[0]
     "Subtitle(Bullet)",
     3],
    ["Malaysia Weather",       #data[0]
     "Content(Bullet)",
     3],
    ["China Weather",       #data[0]
     "This is a brown Fox",
     3]
]

for each_slide in slides:
    MySlide(each_slide)

Now I am trying to add dataframe/image and descriptive text to above slides. I am not sure I understand how I am supposed to do it.

Previously I had tried the below code:

list_of_datasets = [df1, df2, df3]
list_of_slides = [slide[0], slide[1], slide[2]]


for i, j in zip(list_of_datasets, list_of_slides):
    df_to_table(j, i)

But the above code puts table in complete slide which is not what I need. if someone can point to relevant documentation that should also be good for me since I did not understand the existing documentation regarding the same on: https://python-pptx.readthedocs.io/en/latest/dev/analysis/sld-layout.html

How to write data in different slide layouts using python-pptx?

RSM
  • 645
  • 10
  • 25
  • Change the number 3 to change the layout and you can read this https://pythonprogramming.altervista.org/python-and-powerpoint-3-0/ – PythonProgrammi Dec 30 '19 at 13:37

1 Answers1

-1

Put an image called for ex. "girl.png" in the directory to see this code working

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 = [
    ["USA Weather",       #data[0]
     "Subtitle(Bullet)",
     "girl.png",
     3],
    ["Malaysia Weather",       #data[0]
     "Content(Bullet)",
     "",
     3],
    ["China Weather",       #data[0]
     "This is a brown Fox",
     "",
     3]
]

for each_slide in slides:
    MySlide(each_slide)

prs.save("stack.pptx")
os.startfile("stack.pptx")
PythonProgrammi
  • 22,305
  • 3
  • 41
  • 34
  • I am getting the following error: `Traceback (most recent call last): File "C:/Users/test.py", line 33, in MySlide(each_slide) File "C:/Users/test.py", line 15, in __init__ self.slide.placeholders[2].insert_picture(data[2]) AttributeError: 'SlidePlaceholder' object has no attribute 'insert_picture' Process finished with exit code 1` – RSM Jan 02 '20 at 05:32
  • I have simply copied the above code and executed. Am i doing something wrong.? – RSM Jan 02 '20 at 05:34
  • I will think to what is the possible cause, in the meanwhile take a look at this https://pythonprogramming.altervista.org/python-and-powerpoint-3-0/ – PythonProgrammi Jan 02 '20 at 12:03
  • code doesn't work for me either - same error – baxx Dec 09 '20 at 14:58