5

I am creating a table using pptx python

Reference: https://python-pptx.readthedocs.io/en/latest/dev/analysis/tbl-table.html

When I create a table, I get the default table style and I would like to change it. Any recommendation?

x, y, cx, cy = Inches(4.5), Inches(1.5), Inches(4.0), Inches(0)
shape = slide.shapes.add_table(7, 5, x, y, cx, cy)
table = shape.table

I am thinking of table.apply_style() but I can't figure out the correct syntax

enter image description here

Jonathan Lam
  • 1,237
  • 3
  • 20
  • 49

2 Answers2

14

Ok found the solution

x, y, cx, cy = Inches(4.5), Inches(1.5), Inches(4.0), Inches(0)
shape = slide.shapes.add_table(7, 5, x, y, cx, cy)
table = shape.table

tbl =  shape._element.graphic.graphicData.tbl
style_id = '{1FECB4D8-DB02-4DC6-A0A2-4F2EBAE1DC90}'
tbl[0][-1].text = style_id

I am using Medium Style 1 Accent 3, Replace the sytle_id with the GUI found in this list: https://github.com/scanny/python-pptx/issues/27#issuecomment-263076372

Jonathan Lam
  • 1,237
  • 3
  • 20
  • 49
  • 2
    Thank you so much for this insight. I feel like this should be part of the standard documentation for python pptx regarding table styles. – Brady Feb 19 '21 at 20:00
  • 1
    This thread is such a life saver! Thanks so much! – LegendWK Oct 21 '22 at 10:52
4

I generated slides with each table style demonstrated on them at https://github.com/mbachtell/python-pptx-table-styles.

If anyone else comes across a need for this.