Adding dataframe and text in ppt using python-pptx in slide layout 3

你离开我真会死。 提交于 2020-01-10 06:01:11

问题


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?


回答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")


来源:https://stackoverflow.com/questions/59496023/adding-dataframe-and-text-in-ppt-using-python-pptx-in-slide-layout-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!