在一张幻灯片中获取 Table 和标题

Get Table and Title in one Slide

我有下面的代码,它生成一个示例面,布局 1,TitleSummary Table

from pd2ppt import df_to_powerpoint
from pd2ppt import df_to_table
import pandas as pd
from pptx import Presentation
from pptx.util import Inches

path =r"mypath\Sample PPT.pptx"
prs = Presentation(path)
title_slide_layout = prs.slide_layouts[1]
slide = prs.slides.add_slide(title_slide_layout)
title = slide.shapes.title
title.text = "Summary Table"
prs.save(path)

现在在同一张幻灯片中,我想在占位符部分添加 dataframe

样本 df:

df = pd.DataFrame(
    {'District':['Hampshire', 'Dorset', 'Wiltshire', 'Worcestershire'],
     'Population':[25000, 500000, 735298, 12653],
     'Ratio':[1.56, 7.34, 3.67, 8.23]})

我的代码:

df_to_powerpoint(
    r"mypath\Sample PPT.pptx", df)

上面的代码有效并在幻灯片上获取了 df,但它在下一张幻灯片中输出数据,而不是在我将 title 打印为 Summary Table.[=19 的同一张幻灯片上输出数据=]

总而言之,我想要一张幻灯片中的 df 和标题

有什么帮助吗?

我自己想出来了。只需要 df_to_table 而不是 df_to_powerpoint

top = Inches(1.5)
left =Inches(0.25)
width =Inches(9.25)
height= Inches(5.0)

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

prs.save(path)

只需要将代码放在我的 title.text 之后就可以了!!