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

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?

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值