在幻灯片布局3中使用python-pptx在ppt中添加数据框和文本

时间:2019-12-27 04:36:57

标签: python python-3.x dataframe python-pptx

我使用以下代码创建了ppt:

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)

现在,我尝试将数据框/图像和描述性文本添加到上面的幻灯片中。我不确定我该怎么做。

以前我尝试过以下代码:

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)

但是上面的代码将表格放到完整的幻灯片中,这不是我所需要的。如果有人可以指向对我也有帮助的相关文档,因为我不了解关于https://python-pptx.readthedocs.io/en/latest/dev/analysis/sld-layout.html

的现有文档,

如何使用python-pptx在不同的幻灯片布局中写入数据?

1 个答案:

答案 0 :(得分:0)

放入要求ex的图像。目录中的“ girl.png”以查看此代码是否有效

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