scanny / python-pptx

Create Open XML PowerPoint documents in Python
MIT License
2.36k stars 511 forks source link

feature: apply table styles #27

Open scanny opened 11 years ago

scanny commented 11 years ago

Add Table.apply_style(guid)

hanchelc commented 10 years ago

Hi, Steve!

When I used python-pptx to add a table, it used a blue-themed table, but when I add a table through powerpoint, its default table style to add is a red-themed table (which is the one I wanted). How does python-pptx currently determine which style to use when adding tables? If possible, could the default table style for python-pptx be set to PowerPoint's default style?

-Hanchel

scanny commented 10 years ago

As I recall, it turns out there's actually no such thing as a settable default table style in PowerPoint, an unfortunate gap to be sure. Must have fallen on the road to the first PowerPoint release :)

That's one of the reasons I didn't add table styles along with adding a table as I recall. There is a mapping of a GUID to a table style I believe, either a standard style, one of which is what is there now, or a custom style specified in I think it's the tableStyles.xml part, and having an arbitrary GUID. So adding this requires some foundational work to add that new part as a parsed entity and a few objects to get to the table style list. Then I expect you could do something like say table.style = prs.table_styles[2] or whatever.

hanchelc commented 10 years ago

No settable default table style? When I click on a table in ppt, go to Table Tools > Design > Table Styles, and right click any of the options, I see a "Set as Default" option. Yeah, something like table.style = prs.table_styles[2] would be great!

scanny commented 10 years ago

@hanchelc I'll check into this a little more in the next couple days when I have a minute to see if I can be more specific. Here's a link to give you a gist, probably some additional searching around the term "powerpoint default table style" would yield some results.

https://www.google.com/search?q=powerpoint+default+table+style&oq=powerpoint+default+table+style&aqs=chrome..69i57.5839j0j7&sourceid=chrome&espv=2&es_sm=119&ie=UTF-8

http://www.microsoft-questions.com/microsoft/PowerPoint/32069070/default-table-styles.aspx

You might also give opc-diag at try, comparing a snapshot before and after setting a default from the menu and seeing what the XML changes are; that would help focus the conversation on just what possible new features there might be in python-pptx. Anything the XML can do python-pptx can surely do with a little development work.

castaway commented 10 years ago

I have this in my code as:

def set_table_style(graphicFrame):
  tblPr = graphicFrame._tbl_elm._get_or_insert_tblPr()
  tblPr.tableStyleId = '{9D7B26C5-4107-4FEC-AEDC-1716B250A1EF}'

where the parameter is the result of an add_table.

scanny commented 10 years ago

Ah, ok, I just found something that clarifies my memory on this a bit. PowerPoint doesn't allow a custom table style to be created. That's the part that's too bad, because that would be really useful :)

What it does do is allow you to specify one of the stock table styles as the default table for a template/presentation, which must somehow create a setting in the XML, perhaps in the theme part or the tableStyles.xml part.

If that would do the trick for you, the feature would be to create new tables using that default, rather than the essentially hard-coded default it currently uses.

This would still be some work, because there would need to be a lookup into that other part to discover the default setting and to map it to a GUID.

If you want to move things along you can start on the analysis of what gets written to what XML by PowerPoint when you make that setting etc., that will inform the scope of effort and give us an idea what the protocol should be etc.

Here's an example of the sort of analysis I usually invest in at the start of adding a feature: http://python-pptx.readthedocs.org/en/latest/dev/analysis/features/txt-font-color.html

It answers the questions you end up having during development, so getting a fairly solid draft of it going before getting to far into development usually pays off in reduced rework. I always stop as soon as I have a clear idea what it needs to do and how to do it. There doesn't seem to be a lot of residual value in polishing them :)

scanny commented 10 years ago

@castaway, @hanchelc yes, that will do the trick if you just need a workaround to a fixed one of the stock table styles.

castaway commented 10 years ago

I'd also love a way to apply it to add_table / insert_table ;)

DonClemente commented 9 years ago

+1 :)

emilecaron commented 9 years ago

You can also monkeypatch the thing:

from pptx.oxml.shapes.table import CT_Table
style_id = '{2D5ABB26-0587-4C30-8999-92F81FD0307C}'
CT_Table.new_tbl = partial(CT_Table.new_tbl, tableStyleId=style_id)

edit: CT_Table was moved to pptx.oxml.table in recent versions. works fine after updating the import

NGnome commented 9 years ago

Hi Steve,

Is there any other workaroud to modify/alter border lines for cells other than going through the xml. I tried emilecaron's and castaway's methods to use certain tablestyle's but none worked for me so far.

Thank you, Ngnome

scanny commented 9 years ago

I don't know the details of how cell borders are applied @NGnome, at least as far as how that relates to table styles. Looking back at the analysis I did for tables a while back, it looks like they can be applied directly in the MS API using the Cell.Borders object. As I recall that's a fairly complex object and it might take a couple layers down from there to actually do the needful. https://msdn.microsoft.com/EN-US/library/office/ff745952.aspx

The reason I mention that is it's a reasonably good way to get a rough idea how sophisticated the python-pptx API would need to be.

The other approach, and what I would probably recommend, is to see what you can do from the PowerPoint UI, then examine the XML produced when to do those operations. If you can get borders by applying a table style from the UI for example, there's a good bet it could be done from an API.

But either way, you'll be on your own for implementing it for the foreseeable future, and that will mean manipulating the XML at the lxml level, or perhaps the so-called "oxml" level, the lxml wrapper bits built into python-pptx. I don't have any near-term plans to do any enhancements. Busy with other things, you know how it goes.

NGnome commented 9 years ago

I know. Still, Thanks a lot. I will try to dig through oxml to find a workaround code. By the way, do you remember (https://groups.google.com/forum/#!topic/python-pptx/iaQU9H1IYV0 ) SubElement patch trough oxml. It does not work directly, so I tried to fix the flow, but I am missing something I guess. Is it possible you can provide a call routine example for Parse_xml(), I tried several times to overwrite the xml, but if failed so far

Thank you, NGnome

mkajman commented 8 years ago

Hi Steve, in the code, I cannot find the function apply_style(), which is mentioned as the Table class method in the documentation. (http://python-pptx.readthedocs.org/en/latest/dev/analysis/features/shp-table.html#table-class) Does it mean that, presently, there is no direct way how to set table style?

Thank you. Marek

scanny commented 8 years ago

@mkajman: Short answer is yes, that's what it means :)

The page you linked to is a feature analysis page, which is a precursor to the code that implements that feature. In the case of Table.apply_style(...), that feature hasn't been implemented yet.

syamajala commented 8 years ago

Any progress on this?

syamajala commented 8 years ago

It seems like as a work around that if you create a template and set a default table style in it that python-pptx should respect that when inserting a new table, but it seems to the powerpoint default instead of what has been set. So that's probably a bug that should be fixed.

flutefreak7 commented 7 years ago

The following link has a list of all the GUID's for various table styles in PowerPoint 2010:

https://msdn.microsoft.com/en-us/library/office/hh273476(v=office.14).aspx

Here's the list in case the link goes bad...

' The following list includes the name and style for each of the available table styles. ' This list is undocumented, and was created using inspection. Any values could ' change in any version, and this list has only been tested for PowerPoint 2010:

' No Style, No Grid: {2D5ABB26-0587-4C30-8999-92F81FD0307C} ' Themed Style 1 - Accent 1: {3C2FFA5D-87B4-456A-9821-1D502468CF0F} ' Themed Style 1 - Accent 2: {284E427A-3D55-4303-BF80-6455036E1DE7} ' Themed Style 1 - Accent 3: {69C7853C-536D-4A76-A0AE-DD22124D55A5} ' Themed Style 1 - Accent 4: {775DCB02-9BB8-47FD-8907-85C794F793BA} ' Themed Style 1 - Accent 5: {35758FB7-9AC5-4552-8A53-C91805E547FA} ' Themed Style 1 - Accent 6: {08FB837D-C827-4EFA-A057-4D05807E0F7C} ' No Style, Table Grid: {5940675A-B579-460E-94D1-54222C63F5DA} ' Themed Style 2 - Accent 1: {D113A9D2-9D6B-4929-AA2D-F23B5EE8CBE7} ' Themed Style 2 - Accent 2: {18603FDC-E32A-4AB5-989C-0864C3EAD2B8} ' Themed Style 2 - Accent 3: {306799F8-075E-4A3A-A7F6-7FBC6576F1A4} ' Themed Style 2 - Accent 4: {E269D01E-BC32-4049-B463-5C60D7B0CCD2} ' Themed Style 2 - Accent 5: {327F97BB-C833-4FB7-BDE5-3F7075034690} ' Themed Style 2 - Accent 6: {638B1855-1B75-4FBE-930C-398BA8C253C6} ' Light Style 1: {9D7B26C5-4107-4FEC-AEDC-1716B250A1EF} ' Light Style 1 - Accent 1: {3B4B98B0-60AC-42C2-AFA5-B58CD77FA1E5} ' Light Style 1 - Accent 2: {0E3FDE45-AF77-4B5C-9715-49D594BDF05E} ' Light Style 1 - Accent 3: {C083E6E3-FA7D-4D7B-A595-EF9225AFEA82} ' Light Style 1 - Accent 4: {D27102A9-8310-4765-A935-A1911B00CA55} ' Light Style 1 - Accent 5: {5FD0F851-EC5A-4D38-B0AD-8093EC10F338} ' Light Style 1 - Accent 6: {68D230F3-CF80-4859-8CE7-A43EE81993B5} ' Light Style 2: {7E9639D4-E3E2-4D34-9284-5A2195B3D0D7} ' Light Style 2 - Accent 1: {69012ECD-51FC-41F1-AA8D-1B2483CD663E} ' Light Style 2 - Accent 2: {72833802-FEF1-4C79-8D5D-14CF1EAF98D9} ' Light Style 2 - Accent 3: {F2DE63D5-997A-4646-A377-4702673A728D} ' Light Style 2 - Accent 4: {17292A2E-F333-43FB-9621-5CBBE7FDCDCB} ' Light Style 2 - Accent 5: {5A111915-BE36-4E01-A7E5-04B1672EAD32} ' Light Style 2 - Accent 6: {912C8C85-51F0-491E-9774-3900AFEF0FD7} ' Light Style 3: {616DA210-FB5B-4158-B5E0-FEB733F419BA} ' Light Style 3 - Accent 1: {BC89EF96-8CEA-46FF-86C4-4CE0E7609802} ' Light Style 3 - Accent 2: {5DA37D80-6434-44D0-A028-1B22A696006F} ' Light Style 3 - Accent 3: {8799B23B-EC83-4686-B30A-512413B5E67A} ' Light Style 3 - Accent 4: {ED083AE6-46FA-4A59-8FB0-9F97EB10719F} ' Light Style 3 - Accent 5: {BDBED569-4797-4DF1-A0F4-6AAB3CD982D8} ' Light Style 3 - Accent 6: {E8B1032C-EA38-4F05-BA0D-38AFFFC7BED3} ' Medium Style 1: {793D81CF-94F2-401A-BA57-92F5A7B2D0C5} ' Medium Style 1 - Accent 1: {B301B821-A1FF-4177-AEE7-76D212191A09} ' Medium Style 1 - Accent 2: {9DCAF9ED-07DC-4A11-8D7F-57B35C25682E} ' Medium Style 1 - Accent 3: {1FECB4D8-DB02-4DC6-A0A2-4F2EBAE1DC90} ' Medium Style 1 - Accent 4: {1E171933-4619-4E11-9A3F-F7608DF75F80} ' Medium Style 1 - Accent 5: {FABFCF23-3B69-468F-B69F-88F6DE6A72F2} ' Medium Style 1 - Accent 6: {10A1B5D5-9B99-4C35-A422-299274C87663} ' Medium Style 2: {073A0DAA-6AF3-43AB-8588-CEC1D06C72B9} ' Medium Style 2 - Accent 1: {5C22544A-7EE6-4342-B048-85BDC9FD1C3A} ' Medium Style 2 - Accent 2: {21E4AEA4-8DFA-4A89-87EB-49C32662AFE0} ' Medium Style 2 - Accent 3: {F5AB1C69-6EDB-4FF4-983F-18BD219EF322} ' Medium Style 2 - Accent 4: {00A15C55-8517-42AA-B614-E9B94910E393} ' Medium Style 2 - Accent 5: {7DF18680-E054-41AD-8BC1-D1AEF772440D} ' Medium Style 2 - Accent 6: {93296810-A885-4BE3-A3E7-6D5BEEA58F35} ' Medium Style 3: {8EC20E35-A176-4012-BC5E-935CFFF8708E} ' Medium Style 3 - Accent 1: {6E25E649-3F16-4E02-A733-19D2CDBF48F0} ' Medium Style 3 - Accent 2: {85BE263C-DBD7-4A20-BB59-AAB30ACAA65A} ' Medium Style 3 - Accent 3: {EB344D84-9AFB-497E-A393-DC336BA19D2E} ' Medium Style 3 - Accent 4: {EB9631B5-78F2-41C9-869B-9F39066F8104} ' Medium Style 3 - Accent 5: {74C1A8A3-306A-4EB7-A6B1-4F7E0EB9C5D6} ' Medium Style 3 - Accent 6: {2A488322-F2BA-4B5B-9748-0D474271808F} ' Medium Style 4: {D7AC3CCA-C797-4891-BE02-D94E43425B78} ' Medium Style 4 - Accent 1: {69CF1AB2-1976-4502-BF36-3FF5EA218861} ' Medium Style 4 - Accent 2: {8A107856-5554-42FB-B03E-39F5DBC370BA} ' Medium Style 4 - Accent 3: {0505E3EF-67EA-436B-97B2-0124C06EBD24} ' Medium Style 4 - Accent 4: {C4B1156A-380E-4F78-BDF5-A606A8083BF9} ' Medium Style 4 - Accent 5: {22838BEF-8BB2-4498-84A7-C5851F593DF1} ' Medium Style 4 - Accent 6: {16D9F66E-5EB9-4882-86FB-DCBF35E3C3E4} ' Dark Style 1: {E8034E78-7F5D-4C2E-B375-FC64B27BC917} ' Dark Style 1 - Accent 1: {125E5076-3810-47DD-B79F-674D7AD40C01} ' Dark Style 1 - Accent 2: {37CE84F3-28C3-443E-9E96-99CF82512B78} ' Dark Style 1 - Accent 3: {D03447BB-5D67-496B-8E87-E561075AD55C} ' Dark Style 1 - Accent 4: {E929F9F4-4A8F-4326-A1B4-22849713DDAB} ' Dark Style 1 - Accent 5:{8FD4443E-F989-4FC4-A0C8-D5A2AF1F390B} ' Dark Style 1 - Accent 6: {AF606853-7671-496A-8E4F-DF71F8EC918B} ' Dark Style 2: {5202B0CA-FC54-4496-8BCA-5EF66A818D29} ' Dark Style 2 - Accent 1/Accent 2: {0660B408-B3CF-4A94-85FC-2B1E0A45F4A2} ' Dark Style 2 - Accent 3/Accent 4: {91EBBBCC-DAD2-459C-BE2E-F6DE35CF9A28} ' Dark Style 2 - Accent 5/Accent 6: {46F890A9-2807-4EBB-B81D-B2AA78EC7F39}

Not sure if this is helpful, but I'm trying to implement table styles and for features that aren't in python-pptx I'm switching over to win32com and using the PowerPoint object model... I still haven't found the list for PowerPoint 2013, but I thought these codes might help since they apparently didn't bother making an Enum or whatever for table ids.

Edit: Generated 2013's myself (1 at a time with a macro... fun):

No Style, No Grid: {2D5ABB26-0587-4C30-8999-92F81FD0307C} Themed Style 1 - Accent 1: {3C2FFA5D-87B4-456A-9821-1D502468CF0F} Themed Style 1 - Accent 2: {284E427A-3D55-4303-BF80-6455036E1DE7} Themed Style 1 - Accent 3: {69C7853C-536D-4A76-A0AE-DD22124D55A5} Themed Style 1 - Accent 4: {775DCB02-9BB8-47FD-8907-85C794F793BA} Themed Style 1 - Accent 5: {35758FB7-9AC5-4552-8A53-C91805E547FA} Themed Style 1 - Accent 6: {08FB837D-C827-4EFA-A057-4D05807E0F7C} No Style, Table Grid: {5940675A-B579-460E-94D1-54222C63F5DA} Themed Style 2 - Accent 1: {D113A9D2-9D6B-4929-AA2D-F23B5EE8CBE7} Themed Style 2 - Accent 2: {18603FDC-E32A-4AB5-989C-0864C3EAD2B8} Themed Style 2 - Accent 3: {306799F8-075E-4A3A-A7F6-7FBC6576F1A4} Themed Style 2 - Accent 4: {E269D01E-BC32-4049-B463-5C60D7B0CCD2} Themed Style 2 - Accent 5: {327F97BB-C833-4FB7-BDE5-3F7075034690} Themed Style 2 - Accent 6: {638B1855-1B75-4FBE-930C-398BA8C253C6} Light Style 1: {9D7B26C5-4107-4FEC-AEDC-1716B250A1EF} Light Style 1 - Accent 1: {3B4B98B0-60AC-42C2-AFA5-B58CD77FA1E5} Light Style 1 - Accent 2: {0E3FDE45-AF77-4B5C-9715-49D594BDF05E} Light Style 1 - Accent 3: {C083E6E3-FA7D-4D7B-A595-EF9225AFEA82} Light Style 1 - Accent 4: {D27102A9-8310-4765-A935-A1911B00CA55} Light Style 1 - Accent 5: {5FD0F851-EC5A-4D38-B0AD-8093EC10F338} Light Style 1 - Accent 6: {68D230F3-CF80-4859-8CE7-A43EE81993B5} Light Style 2: {7E9639D4-E3E2-4D34-9284-5A2195B3D0D7} Light Style 2 - Accent 1: {69012ECD-51FC-41F1-AA8D-1B2483CD663E} Light Style 2 - Accent 2: {72833802-FEF1-4C79-8D5D-14CF1EAF98D9} Light Style 2 - Accent 3: {F2DE63D5-997A-4646-A377-4702673A728D} Light Style 2 - Accent 4: {17292A2E-F333-43FB-9621-5CBBE7FDCDCB} Light Style 2 - Accent 5: {5A111915-BE36-4E01-A7E5-04B1672EAD32} Light Style 2 - Accent 6: {912C8C85-51F0-491E-9774-3900AFEF0FD7} Light Style 3: {616DA210-FB5B-4158-B5E0-FEB733F419BA} Light Style 3 - Accent 1: {BC89EF96-8CEA-46FF-86C4-4CE0E7609802} Light Style 3 - Accent 2: {5DA37D80-6434-44D0-A028-1B22A696006F} Light Style 3 - Accent 3: {8799B23B-EC83-4686-B30A-512413B5E67A} Light Style 3 - Accent 4: {ED083AE6-46FA-4A59-8FB0-9F97EB10719F} Light Style 3 - Accent 5: {BDBED569-4797-4DF1-A0F4-6AAB3CD982D8} Light Style 3 - Accent 6: {E8B1032C-EA38-4F05-BA0D-38AFFFC7BED3} Medium Style 1: {793D81CF-94F2-401A-BA57-92F5A7B2D0C5} Medium Style 1 - Accent 1: {B301B821-A1FF-4177-AEE7-76D212191A09} Medium Style 1 - Accent 2: {9DCAF9ED-07DC-4A11-8D7F-57B35C25682E} Medium Style 1 - Accent 3: {1FECB4D8-DB02-4DC6-A0A2-4F2EBAE1DC90} Medium Style 1 - Accent 4: {1E171933-4619-4E11-9A3F-F7608DF75F80} Medium Style 1 - Accent 5: {FABFCF23-3B69-468F-B69F-88F6DE6A72F2} Medium Style 1 - Accent 6: {10A1B5D5-9B99-4C35-A422-299274C87663} Medium Style 2: {073A0DAA-6AF3-43AB-8588-CEC1D06C72B9} Medium Style 2 - Accent 1: {5C22544A-7EE6-4342-B048-85BDC9FD1C3A} Medium Style 2 - Accent 2: {21E4AEA4-8DFA-4A89-87EB-49C32662AFE0} Medium Style 2 - Accent 3: {F5AB1C69-6EDB-4FF4-983F-18BD219EF322} Medium Style 2 - Accent 4: {00A15C55-8517-42AA-B614-E9B94910E393} Medium Style 2 - Accent 5: {7DF18680-E054-41AD-8BC1-D1AEF772440D} Medium Style 2 - Accent 6: {93296810-A885-4BE3-A3E7-6D5BEEA58F35} Medium Style 3: {8EC20E35-A176-4012-BC5E-935CFFF8708E} Medium Style 3 - Accent 1: {6E25E649-3F16-4E02-A733-19D2CDBF48F0} Medium Style 3 - Accent 2: {85BE263C-DBD7-4A20-BB59-AAB30ACAA65A} Medium Style 3 - Accent 3: {EB344D84-9AFB-497E-A393-DC336BA19D2E} Medium Style 3 - Accent 4: {EB9631B5-78F2-41C9-869B-9F39066F8104} Medium Style 3 - Accent 5: {74C1A8A3-306A-4EB7-A6B1-4F7E0EB9C5D6} Medium Style 3 - Accent 6: {2A488322-F2BA-4B5B-9748-0D474271808F} Medium Style 4: {D7AC3CCA-C797-4891-BE02-D94E43425B78} Medium Style 4 - Accent 1: {69CF1AB2-1976-4502-BF36-3FF5EA218861} Medium Style 4 - Accent 2: {8A107856-5554-42FB-B03E-39F5DBC370BA} Medium Style 4 - Accent 3: {0505E3EF-67EA-436B-97B2-0124C06EBD24} Medium Style 4 - Accent 4: {C4B1156A-380E-4F78-BDF5-A606A8083BF9} Medium Style 4 - Accent 5: {22838BEF-8BB2-4498-84A7-C5851F593DF1} Medium Style 4 - Accent 6: {16D9F66E-5EB9-4882-86FB-DCBF35E3C3E4} Dark Style 1: {E8034E78-7F5D-4C2E-B375-FC64B27BC917} Dark Style 1 - Accent 1: {125E5076-3810-47DD-B79F-674D7AD40C01} Dark Style 1 - Accent 2: {37CE84F3-28C3-443E-9E96-99CF82512B78} Dark Style 1 - Accent 3: {D03447BB-5D67-496B-8E87-E561075AD55C} Dark Style 1 - Accent 4: {E929F9F4-4A8F-4326-A1B4-22849713DDAB} Dark Style 1 - Accent 5: {8FD4443E-F989-4FC4-A0C8-D5A2AF1F390B} Dark Style 1 - Accent 6: {AF606853-7671-496A-8E4F-DF71F8EC918B} Dark Style 2: {5202B0CA-FC54-4496-8BCA-5EF66A818D29} Dark Style 2 - Accent 1/Accent 2: {0660B408-B3CF-4A94-85FC-2B1E0A45F4A2} Dark Style 2 - Accent 3/Accent 4: {91EBBBCC-DAD2-459C-BE2E-F6DE35CF9A28} Dark Style 2 - Accent 5/Accent 6: {46F890A9-2807-4EBB-B81D-B2AA78EC7F39}

scanny commented 7 years ago

This is really helpful, thanks @flutefreak7 :)

I'm inclined to think these would be pretty stable, since there's no guarantee a presentation is going to be opened with the same version it was created with. So I expect they will remain the same, although perhaps some new ones will be added.

In any case, these will end up in the feature I'm sure, as an enumeration I expect, as you mentioned. When the time comes this reference will make that part quick work. Thanks again for investing the time, this is definitely a contribution :)

flutefreak7 commented 7 years ago

Well now I feel silly... a quick glance between the 2010 values and the 2013 ones and they do appear the same. I'll have to run a comparison tomorrow to check, but what you said makes complete sense about backwards compatibility. I suppose if they ever add or rearrange table styles then these could change.

flutefreak7 commented 7 years ago

Sorry for the extra noise, but here's a Python class with constants for the styles if that's of any help:

class TableStyle:
    NoStyleNoGrid = '{2D5ABB26-0587-4C30-8999-92F81FD0307C}'
    ThemedStyle1Accent1 = '{3C2FFA5D-87B4-456A-9821-1D50468CF0F}'
    ThemedStyle1Accent2 = '{284E427A-3D55-4303-BF80-6455036E1DE7}'
    ThemedStyle1Accent3 = '{69C7853C-536D-4A76-A0AE-DD22124D55A5}'
    ThemedStyle1Accent4 = '{775DCB02-9BB8-47FD-8907-85C794F793BA}'
    ThemedStyle1Accent5 = '{35758FB7-9AC5-4552-8A53-C91805E547FA}'
    ThemedStyle1Accent6 = '{08FB837D-C827-4EFA-A057-4D05807E0F7C}'
    NoStyleTableGrid = '{5940675A-B579-460E-94D1-54222C63F5DA}'
    ThemedStyle2Accent1 = '{D113A9D2-9D6B-4929-AA2D-F23B5EE8CBE7}'
    ThemedStyle2Accent2 = '{18603FDC-E32A-4AB5-989C-0864C3EAD2B8}'
    ThemedStyle2Accent3 = '{306799F8-075E-4A3A-A7F6-7FBC6576F1A4}'
    ThemedStyle2Accent4 = '{E269D01E-BC32-4049-B463-5C60D7B0CCD2}'
    ThemedStyle2Accent5 = '{327F97BB-C833-4FB7-BDE5-3F7075034690}'
    ThemedStyle2Accent6 = '{638B1855-1B75-4FBE-930C-398BA8C253C6}'
    LightStyle1 = '{9D7B26C5-4107-4FEC-AEDC-1716B250A1EF}'
    LightStyle1Accent1 = '{3B4B98B0-60AC-42C2-AFA5-B58CD77FA1E5}'
    LightStyle1Accent2 = '{0E3FDE45-AF77-4B5C-9715-49D594BDF05E}'
    LightStyle1Accent3 = '{C083E6E3-FA7D-4D7B-A595-EF9225AFEA82}'
    LightStyle1Accent4 = '{D27102A9-8310-4765-A935-A1911B00CA55}'
    LightStyle1Accent5 = '{5FD0F851-EC5A-4D38-B0AD-8093EC10F338}'
    LightStyle1Accent6 = '{68D230F3-CF80-4859-8CE7-A43EE81993B5}'
    LightStyle2 = '{7E9639D4-E3E2-4D34-9284-5A2195B3D0D7}'
    LightStyle2Accent1 = '{69012ECD-51FC-41F1-AA8D-1B2483CD663E}'
    LightStyle2Accent2 = '{72833802-FEF1-4C79-8D5D-14CF1EAF98D9}'
    LightStyle2Accent3 = '{F2DE63D5-997A-4646-A377-4702673A728D}'
    LightStyle2Accent4 = '{17292A2E-F333-43FB-9621-5CBBE7FDCDCB}'
    LightStyle2Accent5 = '{5A111915-BE36-4E01-A7E5-04B1672EAD32}'
    LightStyle2Accent6 = '{912C8C85-51F0-491E-9774-3900AFEF0FD7}'
    LightStyle3 = '{616DA210-FB5B-4158-B5E0-FEB733F419BA}'
    LightStyle3Accent1 = '{BC89EF96-8CEA-46FF-86C4-4CE0E7609802}'
    LightStyle3Accent2 = '{5DA37D80-6434-44D0-A028-1B22A696006F}'
    LightStyle3Accent3 = '{8799B23B-EC83-4686-B30A-512413B5E67A}'
    LightStyle3Accent4 = '{ED083AE6-46FA-4A59-8FB0-9F97EB10719F}'
    LightStyle3Accent5 = '{BDBED569-4797-4DF1-A0F4-6AAB3CD982D8}'
    LightStyle3Accent6 = '{E8B1032C-EA38-4F05-BA0D-38AFFFC7BED3}'
    MediumStyle1 = '{793D81CF-94F2-401A-BA57-92F5A7B2D0C5}'
    MediumStyle1Accent1 = '{B301B821-A1FF-4177-AEE7-76D212191A09}'
    MediumStyle1Accent2 = '{9DCAF9ED-07DC-4A11-8D7F-57B35C25682E}'
    MediumStyle1Accent3 = '{1FECB4D8-DB02-4DC6-A0A2-4F2EBAE1DC90}'
    MediumStyle1Accent4 = '{1E171933-4619-4E11-9A3F-F7608DF75F80}'
    MediumStyle1Accent5 = '{FABFCF23-3B69-468F-B69F-88F6DE6A72F2}'
    MediumStyle1Accent6 = '{10A1B5D5-9B99-4C35-A422-299274C87663}'
    MediumStyle2 = '{073A0DAA-6AF3-43AB-8588-CEC1D06C72B9}'
    MediumStyle2Accent1 = '{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}'
    MediumStyle2Accent2 = '{21E4AEA4-8DFA-4A89-87EB-49C32662AFE0}'
    MediumStyle2Accent3 = '{F5AB1C69-6EDB-4FF4-983F-18BD219EF322}'
    MediumStyle2Accent4 = '{00A15C55-8517-42AA-B614-E9B94910E393}'
    MediumStyle2Accent5 = '{7DF18680-E054-41AD-8BC1-D1AEF772440D}'
    MediumStyle2Accent6 = '{93296810-A885-4BE3-A3E7-6D5BEEA58F35}'
    MediumStyle3 = '{8EC20E35-A176-4012-BC5E-935CFFF8708E}'
    MediumStyle3Accent1 = '{6E25E649-3F16-4E02-A733-19D2CDBF48F0}'
    MediumStyle3Accent2 = '{85BE263C-DBD7-4A20-BB59-AAB30ACAA65A}'
    MediumStyle3Accent3 = '{EB344D84-9AFB-497E-A393-DC336BA19D2E}'
    MediumStyle3Accent4 = '{EB9631B5-78F2-41C9-869B-9F39066F8104}'
    MediumStyle3Accent5 = '{74C1A8A3-306A-4EB7-A6B1-4F7E0EB9C5D6}'
    MediumStyle3Accent6 = '{2A488322-F2BA-4B5B-9748-0D474271808F}'
    MediumStyle4 = '{D7AC3CCA-C797-4891-BE02-D94E43425B78}'
    MediumStyle4Accent1 = '{69CF1AB2-1976-4502-BF36-3FF5EA218861}'
    MediumStyle4Accent2 = '{8A107856-5554-42FB-B03E-39F5DBC370BA}'
    MediumStyle4Accent3 = '{0505E3EF-67EA-436B-97B2-0124C06EBD24}'
    MediumStyle4Accent4 = '{C4B1156A-380E-4F78-BDF5-A606A8083BF9}'
    MediumStyle4Accent5 = '{22838BEF-8BB2-4498-84A7-C5851F593DF1}'
    MediumStyle4Accent6 = '{16D9F66E-5EB9-4882-86FB-DCBF35E3C3E4}'
    DarkStyle1 = '{E8034E78-7F5D-4C2E-B375-FC64B27BC917}'
    DarkStyle1Accent1 = '{125E5076-3810-47DD-B79F-674D7AD40C01}'
    DarkStyle1Accent2 = '{37CE84F3-28C3-443E-9E96-99CF82512B78}'
    DarkStyle1Accent3 = '{D03447BB-5D67-496B-8E87-E561075AD55C}'
    DarkStyle1Accent4 = '{E929F9F4-4A8F-4326-A1B4-22849713DDAB}'
    DarkStyle1Accent5 = '{8FD4443E-F989-4FC4-A0C8-D5A2AF1F390B}'
    DarkStyle1Accent6 = '{AF606853-7671-496A-8E4F-DF71F8EC918B}'
    DarkStyle2 = '{5202B0CA-FC54-4496-8BCA-5EF66A818D29}'
    DarkStyle2Accent1Accent2 = '{0660B408-B3CF-4A94-85FC-2B1E0A45F4A2}'
    DarkStyle2Accent3Accent4 = '{91EBBBCC-DAD2-459C-BE2E-F6DE35CF9A28}'
    DarkStyle2Accent5Accent6 = '{46F890A9-2807-4EBB-B81D-B2AA78EC7F39}'
descovi commented 7 years ago

Sorry i cant understand how to use @flutefreak7 class. Anyone can help?

flutefreak7 commented 7 years ago

I wanted table style formatting, but python-pptx hasn't implemented it yet, so I used PowerPoint's COM interface to do it (this requires that PowerPoint be installed where python-pptx manipulates the file XML directly). Here's an excerpt of my code that implements a COM interface (with a context manager) and implements table formatting based on filename, slide number, and table name. The TableStyle class is used to provide the necessary constant for a given table style to Table.ApplyStyle().

import win32com.client
from contextlib import contextmanager

def start_ppt_com():
    COM_Application = win32com.client.Dispatch("PowerPoint.Application")
    return COM_Application

@contextmanager
def pptcom(filename, show=False, save=True, save_as=None):
    """
    Silently start a PowerPoint COM server and open a file
    Once the context is finished, save the file, close the file, and close the connection
    :param filename: Name of PowerPoint file to open with COM server
    :param show: Whether or not the application window will be visible
    :param save: Whether or not to save the file after context closes
    :param save_as: New filename to save to
    :return: an opened Powerpoint Presentation object
    :rtype: MSPPT.Presentation
    """
    com_app = start_ppt_com()

    try:
        ComPres = com_app.Presentations.Open(filename, False, False, show)

        yield ComPres

        if save or save_as is not None:
            if save_as is not None:
                ComPres.SaveAs(save_as)
            else:
                ComPres.Save()

    finally:
        try:
            ComPres.Close()
        except Exception as e:
            print('Exception while closing presentation')
            print(e)

        # Only quit the application if there are no other presentations open
        if com_app.Presentations.Count == 0:
            com_app.Quit()

        del com_app

def format_table(filename, slide_number=None, table_name=None, table_style=TableStyle.LightStyle3):

    with pptcom(filename) as ComPres:
        for slide in ComPres.Slides:

            # If a slide number was specified, skip other slides
            if slide_number is not None:
                if slide.SlideNumber != slide_number:
                    continue

            for shape in slide.Shapes:
                # If a table name was specified, skip other tables
                if table_name is not None:
                    if shape.Name != table_name:
                        continue

                if shape.HasTable:
                    table = shape.Table
                    table.ApplyStyle(table_style)
flutefreak7 commented 7 years ago

If the function in that previous comment works, then the TableStyle class can be used as an easier way to provide style_id. The goal was to be helpful so that when an apply_style() method is implemented in python-pptx, the style could be provided as a friendly name like "LightStyle3" or using the constant TableStyle.LightStyle3 rather than having to carry around junk like '{616DA210-FB5B-4158-B5E0-FEB733F419BA}'.

descovi commented 7 years ago

@flutefreak7 thanks for your solution, unfortunately we cant use com interface (we are on a linux server)

We tried the solution you linked after but we think that it uses old methods and properties of python-pptx and returns an error.

Old solution:

def set_table_style(graphicFrame):
  tblPr = graphicFrame._tbl_elm._get_or_insert_tblPr()
  tblPr.tableStyleId = '{9D7B26C5-4107-4FEC-AEDC-1716B250A1EF}'

After we tried this solution with your python class but with no effect inside the pptx

tblPr = graphicFrame._element.graphic.graphicData.tbl
tblPr.tableStyleId = TableStyle.ThemedStyle2Accent2
scanny commented 7 years ago

@simonini tbl (table) is not the same as tblPr (table properties). I think you want something like this. Note that this makes assumptions about what elements are already present; you'll have to make it more sophisticated if you're not dealing with the more complex case where the a:tableStyleId element is already present:

tblPr = graphicFrame._element.graphic.graphicData.tbl.get_or_add_tblPr()
tableStyleId = OxmlElement('a:tableStyleId')
tableStyleId.text = TableStyle.ThemedStyle2Accent2
tblPr.append(tableStyleId)
Jecko-o commented 7 years ago

@scanny In the end we just searched witch was the node with the style information after table creation and manually set it to the one we choose. Giacomo (working with @simonini)

tbl = graphicFrame._element.graphic.graphicData.tbl
tbl[0][-1].text = '{5940675A-B579-460E-94D1-54222C63F5DA}'
shreyaaa4 commented 5 years ago

Hello @scanny

Is there any update to change the table style in powerpoint? Is it possible through simple python code? If yes, then please help!!!

Thanks Shreya

brautigan commented 5 years ago

@shreyaaa4 Guys above provided some work, you can try smth like this:

#slide
slide = prs.slides[4]

#new table    
x, y, cx, cy = Inches(2), Inches(2), Inches(4), Inches(1.5)
shape = slide.shapes.add_table(3, 3, x, y, cx, cy)

#set table look
tbl =  shape._element.graphic.graphicData.tbl
style_id = '{E8B1032C-EA38-4F05-BA0D-38AFFFC7BED3}'
tbl[0][-1].text = style_id

you can get Style_id from flutefreak7 comment with all possible styles

vsansica commented 4 years ago

@shreyaaa4 Guys above provided some work, you can try smth like this:

#slide
slide = prs.slides[4]

#new table    
x, y, cx, cy = Inches(2), Inches(2), Inches(4), Inches(1.5)
shape = slide.shapes.add_table(3, 3, x, y, cx, cy)

#set table look
tbl =  shape._element.graphic.graphicData.tbl
style_id = '{E8B1032C-EA38-4F05-BA0D-38AFFFC7BED3}'
tbl[0][-1].text = style_id

you can get Style_id from flutefreak7 comment with all possible styles

Thanks @brautigan for making it easy to us copy/paste python community XD

autowus commented 4 years ago

@shreyaaa4 Guys above provided some work, you can try smth like this:

#slide
slide = prs.slides[4]

#new table    
x, y, cx, cy = Inches(2), Inches(2), Inches(4), Inches(1.5)
shape = slide.shapes.add_table(3, 3, x, y, cx, cy)

#set table look
tbl =  shape._element.graphic.graphicData.tbl
style_id = '{E8B1032C-EA38-4F05-BA0D-38AFFFC7BED3}'
tbl[0][-1].text = style_id

you can get Style_id from flutefreak7 comment with all possible styles

Thanks @brautigan for providing an easy way to change the table styles. that's wonderful.

tsuka414 commented 2 years ago

Please use a dictionary if necessary.

dict = { 'NoStyleNoGrid' : '{2D5ABB26-0587-4C30-8999-92F81FD0307C}', 'ThemedStyle1Accent1' : '{3C2FFA5D-87B4-456A-9821-1D502468CF0F}', 'ThemedStyle1Accent2' : '{284E427A-3D55-4303-BF80-6455036E1DE7}', 'ThemedStyle1Accent3' : '{69C7853C-536D-4A76-A0AE-DD22124D55A5}', 'ThemedStyle1Accent4' : '{775DCB02-9BB8-47FD-8907-85C794F793BA}', 'ThemedStyle1Accent5' : '{35758FB7-9AC5-4552-8A53-C91805E547FA}', 'ThemedStyle1Accent6' : '{08FB837D-C827-4EFA-A057-4D05807E0F7C}', 'NoStyleTableGrid' : '{5940675A-B579-460E-94D1-54222C63F5DA}', 'ThemedStyle2Accent1' : '{D113A9D2-9D6B-4929-AA2D-F23B5EE8CBE7}', 'ThemedStyle2Accent2' : '{18603FDC-E32A-4AB5-989C-0864C3EAD2B8}', 'ThemedStyle2Accent3' : '{306799F8-075E-4A3A-A7F6-7FBC6576F1A4}', 'ThemedStyle2Accent4' : '{E269D01E-BC32-4049-B463-5C60D7B0CCD2}', 'ThemedStyle2Accent5' : '{327F97BB-C833-4FB7-BDE5-3F7075034690}', 'ThemedStyle2Accent6' : '{638B1855-1B75-4FBE-930C-398BA8C253C6}', 'LightStyle1' : '{9D7B26C5-4107-4FEC-AEDC-1716B250A1EF}', 'LightStyle1Accent1' : '{3B4B98B0-60AC-42C2-AFA5-B58CD77FA1E5}', 'LightStyle1Accent2' : '{0E3FDE45-AF77-4B5C-9715-49D594BDF05E}', 'LightStyle1Accent3' : '{C083E6E3-FA7D-4D7B-A595-EF9225AFEA82}', 'LightStyle1Accent4' : '{D27102A9-8310-4765-A935-A1911B00CA55}', 'LightStyle1Accent5' : '{5FD0F851-EC5A-4D38-B0AD-8093EC10F338}', 'LightStyle1Accent6' : '{68D230F3-CF80-4859-8CE7-A43EE81993B5}', 'LightStyle2' : '{7E9639D4-E3E2-4D34-9284-5A2195B3D0D7}', 'LightStyle2Accent1' : '{69012ECD-51FC-41F1-AA8D-1B2483CD663E}', 'LightStyle2Accent2' : '{72833802-FEF1-4C79-8D5D-14CF1EAF98D9}', 'LightStyle2Accent3' : '{F2DE63D5-997A-4646-A377-4702673A728D}', 'LightStyle2Accent4' : '{17292A2E-F333-43FB-9621-5CBBE7FDCDCB}', 'LightStyle2Accent5' : '{5A111915-BE36-4E01-A7E5-04B1672EAD32}', 'LightStyle2Accent6' : '{912C8C85-51F0-491E-9774-3900AFEF0FD7}', 'LightStyle3' : '{616DA210-FB5B-4158-B5E0-FEB733F419BA}', 'LightStyle3Accent1' : '{BC89EF96-8CEA-46FF-86C4-4CE0E7609802}', 'LightStyle3Accent2' : '{5DA37D80-6434-44D0-A028-1B22A696006F}', 'LightStyle3Accent3' : '{8799B23B-EC83-4686-B30A-512413B5E67A}', 'LightStyle3Accent4' : '{ED083AE6-46FA-4A59-8FB0-9F97EB10719F}', 'LightStyle3Accent5' : '{BDBED569-4797-4DF1-A0F4-6AAB3CD982D8}', 'LightStyle3Accent6' : '{E8B1032C-EA38-4F05-BA0D-38AFFFC7BED3}', 'MediumStyle1' : '{793D81CF-94F2-401A-BA57-92F5A7B2D0C5}', 'MediumStyle1Accent1' : '{B301B821-A1FF-4177-AEE7-76D212191A09}', 'MediumStyle1Accent2' : '{9DCAF9ED-07DC-4A11-8D7F-57B35C25682E}', 'MediumStyle1Accent3' : '{1FECB4D8-DB02-4DC6-A0A2-4F2EBAE1DC90}', 'MediumStyle1Accent4' : '{1E171933-4619-4E11-9A3F-F7608DF75F80}', 'MediumStyle1Accent5' : '{FABFCF23-3B69-468F-B69F-88F6DE6A72F2}', 'MediumStyle1Accent6' : '{10A1B5D5-9B99-4C35-A422-299274C87663}', 'MediumStyle2' : '{073A0DAA-6AF3-43AB-8588-CEC1D06C72B9}', 'MediumStyle2Accent1' : '{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}', 'MediumStyle2Accent2' : '{21E4AEA4-8DFA-4A89-87EB-49C32662AFE0}', 'MediumStyle2Accent3' : '{F5AB1C69-6EDB-4FF4-983F-18BD219EF322}', 'MediumStyle2Accent4' : '{00A15C55-8517-42AA-B614-E9B94910E393}', 'MediumStyle2Accent5' : '{7DF18680-E054-41AD-8BC1-D1AEF772440D}', 'MediumStyle2Accent6' : '{93296810-A885-4BE3-A3E7-6D5BEEA58F35}', 'MediumStyle3' : '{8EC20E35-A176-4012-BC5E-935CFFF8708E}', 'MediumStyle3Accent1' : '{6E25E649-3F16-4E02-A733-19D2CDBF48F0}', 'MediumStyle3Accent2' : '{85BE263C-DBD7-4A20-BB59-AAB30ACAA65A}', 'MediumStyle3Accent3' : '{EB344D84-9AFB-497E-A393-DC336BA19D2E}', 'MediumStyle3Accent4' : '{EB9631B5-78F2-41C9-869B-9F39066F8104}', 'MediumStyle3Accent5' : '{74C1A8A3-306A-4EB7-A6B1-4F7E0EB9C5D6}', 'MediumStyle3Accent6' : '{2A488322-F2BA-4B5B-9748-0D474271808F}', 'MediumStyle4' : '{D7AC3CCA-C797-4891-BE02-D94E43425B78}', 'MediumStyle4Accent1' : '{69CF1AB2-1976-4502-BF36-3FF5EA218861}', 'MediumStyle4Accent2' : '{8A107856-5554-42FB-B03E-39F5DBC370BA}', 'MediumStyle4Accent3' : '{0505E3EF-67EA-436B-97B2-0124C06EBD24}', 'MediumStyle4Accent4' : '{C4B1156A-380E-4F78-BDF5-A606A8083BF9}', 'MediumStyle4Accent5' : '{22838BEF-8BB2-4498-84A7-C5851F593DF1}', 'MediumStyle4Accent6' : '{16D9F66E-5EB9-4882-86FB-DCBF35E3C3E4}', 'DarkStyle1' : '{E8034E78-7F5D-4C2E-B375-FC64B27BC917}', 'DarkStyle1Accent1' : '{125E5076-3810-47DD-B79F-674D7AD40C01}', 'DarkStyle1Accent2' : '{37CE84F3-28C3-443E-9E96-99CF82512B78}', 'DarkStyle1Accent3' : '{D03447BB-5D67-496B-8E87-E561075AD55C}', 'DarkStyle1Accent4' : '{E929F9F4-4A8F-4326-A1B4-22849713DDAB}', 'DarkStyle1Accent5' : '{8FD4443E-F989-4FC4-A0C8-D5A2AF1F390B}', 'DarkStyle1Accent6' : '{AF606853-7671-496A-8E4F-DF71F8EC918B}', 'DarkStyle2' : '{5202B0CA-FC54-4496-8BCA-5EF66A818D29}', 'DarkStyle2Accent1Accent2' : '{0660B408-B3CF-4A94-85FC-2B1E0A45F4A2}', 'DarkStyle2Accent3Accent4' : '{91EBBBCC-DAD2-459C-BE2E-F6DE35CF9A28}', 'DarkStyle2Accent5Accent6' : '{46F890A9-2807-4EBB-B81D-B2AA78EC7F39}'}
MartinPacker commented 2 years ago

@tsuka414 the second line looks to me like it's missing a digit. Please check - and sorry if I'm wrong.

tsuka414 commented 2 years ago

@MartinPacker You're right...

I have created a dictionary using the information in the following link https://github.com/scanny/python-pptx/issues/27#issuecomment-263076372

But looking at other information... I have confirmed that one number is missing. https://github.com/scanny/python-pptx/issues/27#issuecomment-263042222

I have also confirmed the table is created correctly with this GUID and corrected the dictionary.

Thank you for your message!

valerianrossigneux commented 2 years ago

Would there be a way to read from a template slide a particular table style and then copy they style to apply it to other tables?

yash-chandna commented 7 months ago

add this function to your script

def set_table_style(table, new_style_id):
    """
    Set the style of a PowerPoint table by directly manipulating the XML.
    :param table: The table to modify.
    :param style_id: The desired style ID.
    """
    # Access the XML of the table
    tbl = table._tbl
    # Create an XML representation of a tblPr element with the new style ID
    tblPr_xml = """
    <a:tblPr xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
             xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main"
             xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
             firstRow="1" bandRow="1">
      <a:tableStyleId>{NEW_STYLE_ID}</a:tableStyleId>
    </a:tblPr>
    """.replace("{NEW_STYLE_ID}", new_style_id
    new_tblPr = parse_xml(tblPr_xml)

    # Find the existing tblPr element and remove it
    existing_tblPr = tbl.xpath('./a:tblPr')
    if existing_tblPr:
        tbl.remove(existing_tblPr[0])

    # Add the new tblPr element to the table XML
    tbl.insert(0, new_tblPr)

to call it use:

set_table_style(<tablename>,"{<styleid>}")

replace <styleid> from the styles provided above and <tablename> with your table name

DasarathSelvakumar commented 7 months ago

Works like a charm @yash-chandna