在一张幻灯片中获得表格和标题

2024-04-16 04:45:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的代码,它生成了一个布局为1的示例面,Title作为Summary 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 = 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的同一张幻灯片上。你知道吗

总之,我希望df和标题放在一张幻灯片中

需要帮忙吗?你知道吗


Tags: topath代码fromimportdftitletable
1条回答
网友
1楼 · 发布于 2024-04-16 04:45:05

我自己想出来的。只需要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之后,它就成功了!!你知道吗

相关问题 更多 >