0

I'm trying to streamline a process at work and ran into a problem with using Pandas Styler objects and PowerPoint.

Bottom line, the recipients don't want a Jupyter Notebook or even an HTML version of a Jupyter Notebook with the code hidden/removed. They want PowerPoint, no exceptions.

I've written a number of modules in Python to replicate the process and everything is in perfect working order except for the Pandas dataframes. The recipients want nice colorful tables and I've found a way to replicate exactly what they want. The below code gives me the exact table:

import pandas as pd

df = pd.DataFrame({'Col1': ['a', 'b', 'c', 'd'],
                   'Col2': ['e', 'f', 'g', 'h'],
                   'Col3': [1, 2, 3, 4]})

(df.style
 .background_gradient(cmap='RdYlGn', subset='Col3')
 .set_caption('Test'))

When running this in Juypter Notebooks I get exactly the output I want to have rendered in PowerPoint.

How do I take exactly what I'm looking at and get it into PowerPoint without taking a screenshot? The idea is to automate this as much as possible.

I've tried the Pandas.DataFrame.Styler.render() method but I see no way to inject the raw HTML code into PowerPoint and get a rendered table without a separate plugin (not an option).

I also see no way to export the Styler object as an image so it seems that's out unless I missed it.

I did export it to Excel which does retain the cell coloring but loses number formatting (e.g. $100 turns back into 99.98573822839575) so that's out.

The best I've found is to use the wonderful python-pptx package which does lots of great things with tables but I cannot find a way to use this package to import the Styler object into PowerPoint.

pd.__version__ = '0.24.2'
python.__version__ = '3.7.3'
jupyter.__version__ = '1.0.0'
notebook.__version__ = '5.7.8'

PowerPoint 2013 (15.0.5093.1000) MSO (15.0.5127.1000) 32-bit
Windows 10 Enterprise 10.0.16299 Build 16299
  • Perhaps export the styled dataframe [to excel](http://pandas.pydata.org/pandas-docs/stable/user_guide/style.html#Export-to-Excel) and then copy-paste the excel table into your powerpoint presentation? – Xukrao May 24 '19 at 15:10
  • Hi @Xukrao. Thank you for the answer. I did try that (as explained above) but that came with its own complications and is still mostly a manual process which I'm trying to step away from. – James Sheldon May 24 '19 at 15:48
  • While I don't think this will give exactly the formatting you'd like, Google Slides has an API with a lot of table functionality: https://developers.google.com/slides/. You would need to format the request body with certain attributes of your `styler` object, which can be programmatically done with any html parser (`lxml`, for example). – wkzhu May 24 '19 at 20:30
  • You can use matplotlib's ```table``` to create the way you want the table to look then use matplotlib to export to an image. Simliar to https://stackoverflow.com/questions/26678467/export-a-pandas-dataframe-as-a-table-image/39358722 – Ben Pap May 24 '19 at 20:36
  • Hi @wkzhu. That's a great idea and would work perfectly well but Google Slides isn't an option. The data I'm dealing with is health care data and extremely sensitive. Under no circumstances can the data leave our internal servers so no Google Slides. :-( – James Sheldon May 25 '19 at 10:06
  • Hi @BenPap. Let me look into `table`. It's a hair frustrating that the Styler object in Pandas doesn't have a `savefig` or `to_img` method. Making the styled table is so elegant. – James Sheldon May 25 '19 at 10:09
  • Hi @BenPap. I looked into matplotlib's excellent `table` package and while it can do what I need, I'd need to refactor a large base of code. I'd like to avoid that if possible and take what I've already generated and just pop it out. – James Sheldon May 27 '19 at 11:00
  • Have you looked into imgkit python package? Also you should be aware that ```render()``` does not include the css that the notebook applies automatically, only the css you apply. – Ben Pap May 29 '19 at 00:20
  • Hi all. Thank you for your comments. I've hit a wall with this and really the problem isn't with Pandas but with an existing business process. I'll aim to get that streamlined so I can step away from needing to do this. – James Sheldon Jun 21 '19 at 18:59
  • Thank you everyone for the thoughts and comments. Ultimately, this came down to business requests instead of coding issues. I got the stakeholders to change their minds and go with React.js. Everyone is much happier now. – James Sheldon Aug 09 '19 at 09:53

0 Answers0