0

So I have a excel which contains a table like this:

enter image description here

I want to get the same table in powerpoint using Python

Work Done till now:

  1. Read the excel to python and store in pandas df
  2. Add df to powerpoint

Code for the same effort:

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)

All I need is how to do color formatting in this table using Python?

Rahul Agarwal
  • 4,034
  • 7
  • 27
  • 51

1 Answers1

3

Each cell in a PowerPoint table has its own fill, which can do everything other FillFormat objects can do::

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)

The FillFormat object interface is further described in the documentation here:
https://python-pptx.readthedocs.io/en/latest/api/dml.html#fillformat-objects

scanny
  • 26,423
  • 5
  • 54
  • 80
  • 1
    Thanks for the reply!! one clarification..here your have done `table.cell` now my table is in a pandas df, how to fill `cell` value. If I do this `cell = df.iloc[0,0]` then `cell.fill` gives me this error `'str' object has no attribute 'fill'` – Rahul Agarwal May 27 '19 at 08:19
  • 1
    The return value of `table.cell()` (`cell` in code above) is a `_Cell` object, which provides the properties and methods needed to change the PowerPoint table cell. If you assign `df.iloc[0, 0]` to `cell` you lose that `_Cell` object and the ability to change the table cell. Maybe you want `cell.text = df.iloc[0, 0]`. – scanny May 28 '19 at 17:01
  • I tried `cell.text = df.iloc[0, 0]` it gives me an error `NameError: name 'cell' is not defined` – Rahul Agarwal May 29 '19 at 10:49
  • @RahulAgarwal I can't diagnose your code without seeing it. Perhaps post the code in a new question and tag it with "python-pptx" and I'll see it. – scanny May 29 '19 at 16:40