0

I know that there is a way to change the color of one individual cell in a table by naming that cell from that table and then coloring that cell; however I'm looking for a way to change the color of all cells in one table at the same time.

The table is 7x3 and I already asked a question about changing the font size for all cells in the table so I figure the way to change the color of all of the cells is similar to this.

def iter_cells(urbanicity_table):
    for row in urbanicity_table.rows:
        for cell in row.cells:
            yield cell

for cell in iter_cells(urbanicity_table):
    for paragraph in cell.text_frame.paragraphs:
        for run in paragraph.runs:
            run.font.size = Pt(6)

for cell in iter_cells(urbanicity_table):
    for paragraph in cell.text_frame.paragraphs:
        paragraph.font.size = Pt(6)

The table comes pre-colored with a default color scheme of the template PowerPoint used for this slideshow. Please help, thanks!

KingT753
  • 81
  • 1
  • 5

1 Answers1

2

PowerPoint has a notion of "table-style" which can be applied to a table by selecting from a gallery in one of the ribbons. A table style allows setting fill color and font-formatting for column-headings and row-headings and allows selection of horizontal banding, vertical banding and cell borders.

There is not yet any API support in python-pptx for applying a table-style to a table. So the approach you mention would probably be the easiest way to accomplish a similar look, at least as far as cell background color is concerned.

Something like:

from pptx.dml.color import RGBColor

for cell in iter_cells(table):
    fill = cell.fill
    fill.solid()
    fill.fore_color.rgb = RGBColor(255, 0, 0)
scanny
  • 26,423
  • 5
  • 54
  • 80
  • Nice. but how can I get the font color, cell background color, font size of a existing cell? I see here that you assign a color to cell but how can I extract the existing properties of a each cell in a table such as font color , font size, cell background color and text alignment in cell? You can read more here - https://stackoverflow.com/questions/73202432/python-pptx-access-table-cell-properties-and-apply-it-back-during-replace – The Great Aug 02 '22 at 05:55