How to change default table style using pptx python?
Asked Answered
T

2

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

Trippet answered 24/5, 2020 at 6:21 Comment(0)
T
16

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

Trippet answered 24/5, 2020 at 6:31 Comment(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.Nonaligned
This thread is such a life saver! Thanks so much!Miniature
U
6

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.

Uncharitable answered 2/4, 2021 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.