python自动生成ppt报告,在python-pptx中使用循环创建多个幻灯片并将数据帧写入幻灯片...

本文介绍了一种使用Python批量创建PowerPoint幻灯片的方法,通过定义函数简化重复任务,提高了工作效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I have the below code for creating ppt out of dataframes:

Below is a snippet of the code for 3 slides:

title_slide_layout = prs.slide_layouts[1]

slide = prs.slides.add_slide(title_slide_layout)

slide2 = prs.slides.add_slide(title_slide_layout)

slide3 = prs.slides.add_slide(title_slide_layout)

title = slide.shapes.title

title.text = "Summary Table"

title2 = slide2.shapes.title

title2.text = "New Table"

title3 = slide3.shapes.title

title3.text = "Old Table"

Since i have to create multiple such slides is there anyway to use loops and create multiple slides instead of creating it one by one manually?

Also, I am writing dataframes to slides like this:

df_to_table(slide, df1, left, top, width, height)

df_to_table(slide2, df2, left, top, width, height)

df_to_table(slide3, df3, left, top, width, height)

Is there anyway to loop this too?

Node: My code is working fine without any issues, just want to limit the lines of code for repetitive tasks.

解决方案

This is what functions are for, to abstract some repetative task down to a (function) call:

def add_slide(prs, layout, title):

"""Return slide newly added to `prs` using `layout` and having `title`."""

slide = prs.slides.add_slide(layout)

slide.shapes.title.text = title

return slide

title_slide_layout = prs.slide_layouts[1]

slide = add_slide(prs, title_slide_layout, "Summary Table")

slide2 = add_slide(prs, title_slide_layout, "New Table")

slide3 = add_slide(prs, title_slide_layout, "Old Table")

Your second question is an entirely different question and I recommend you ask it separately. StackOverflow is designed around a one-question-at-a-time format.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值