-
Notifications
You must be signed in to change notification settings - Fork 546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature: apply table styles #27
Comments
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 |
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. |
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. |
@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. 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. |
I have this in my code as:
where the parameter is the result of an add_table. |
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: 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 :) |
I'd also love a way to apply it to add_table / insert_table ;) |
+1 :) |
You can also monkeypatch the thing:
edit: CT_Table was moved to pptx.oxml.table in recent versions. works fine after updating the import |
Hi Steve, Is there any other workaroud to modify/alter border lines for cells other than going through the xml. Thank you, |
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. |
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, |
Hi Steve, Thank you. |
@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. |
Any progress on this? |
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. |
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. ' No Style, No Grid: {2D5ABB26-0587-4C30-8999-92F81FD0307C} 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: No Style, No Grid: {2D5ABB26-0587-4C30-8999-92F81FD0307C} |
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 :) |
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. |
Sorry for the extra noise, but here's a Python class with constants for the styles if that's of any help:
|
Sorry i cant understand how to use @flutefreak7 class. Anyone can help? |
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().
|
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}'. |
@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:
After we tried this solution with your python class but with no effect inside the pptx
|
@Simonini tblPr = graphicFrame._element.graphic.graphicData.tbl.get_or_add_tblPr()
tableStyleId = OxmlElement('a:tableStyleId')
tableStyleId.text = TableStyle.ThemedStyle2Accent2
tblPr.append(tableStyleId) |
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 |
@shreyaaa4 Guys above provided some work, you can try smth like this:
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 |
Thanks @brautigan for providing an easy way to change the table styles. that's wonderful. |
Please use a dictionary if necessary.
|
@tsuka414 the second line looks to me like it's missing a digit. Please check - and sorry if I'm wrong. |
@MartinPacker I have created a dictionary using the information in the following link But looking at other information... I have also confirmed the table is created correctly with this GUID and corrected the dictionary. Thank you for your message! |
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? |
add this function to your script
to call it use:
replace |
Works like a charm @yash-chandna |
Add
Table.apply_style(guid)
The text was updated successfully, but these errors were encountered: