使用 python 为 ppt 中的表格单元格着色

Color tables cells in ppt using python

所以我有一个 excel,其中包含这样一个 table:

我想在 powerpoint 中使用 Python

获得相同的 table

到目前为止完成的工作:

  1. 读取excel到python并存入pandas df
  2. 将 df 添加到 powerpoint

相同努力的代码:

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

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

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)

我需要的是如何使用 Python?

在此 table 中进行颜色格式化

PowerPoint table 中的每个单元格都有自己的 fill,它可以做其他 FillFormat 对象可以做的所有事情::

from pptx.dml.color import RGBColor

cell = table.cell(0, 0)  # ---or whatever cell you choose---
fill = cell.fill
fill.solid()
fill.fore_color.rgb = RGBColor(0xFA, 0x00, 0x37)

FillFormat 对象接口在此处的文档中有进一步描述:
https://python-pptx.readthedocs.io/en/latest/api/dml.html#fillformat-objects