python-pptx library to generate a Microsoft PowerPoint
presentation. At a high level, the notebook goes through the following steps:
- Define the data to be included in the presentation
- Export the data
- Load the presentation template
- Add single-valued attributes to the presentation
- Add functional data to the presentation
- Save the presentation
1. Define the data to be included in the presentation
First, define some constants that identify the record and attributes to be included in the presentation. The following cell identifies a record by Database Key, Table name, and Record History GUID, but an alternative technique could be used to identify a record, for example searching.GRAPHS dictionary maps functional attributes to a 4-tuple, where each element of the tuple
refers to:
- The chart title
- The y-axis label
- The x-axis label
- Optional: For multiple-series plots, a 2-tuple of the Parameter name and the title of the legend in the report
2. Export the data
Export the data for all the required attributes for the ‘released’ version of the record.3. Load the presentation template
The report template is created in Microsoft PowerPoint, and the ‘python-pptx’ library is used to populate the template with data exported from Granta MI. Import thepython-pptx library and create a Presentation object using the pptx.Presentation
function. The function accepts an optional string as the relative path to a template file, which
is used to create the Presentation object. In this example the relative path to the template
file is "supporting_files/presentation_template.pptx", and the Presentation object is stored
in the prs variable.
pptx.Presentation can also be called without arguments, which creates an empty Presentation
instead.
Info:As
The presentation file used above is an empty python-pptx does not implement all the layout and style functionality in Microsoft
PowerPoint, specifying a pre-defined template allows greater control over the layout of the
presentation and more concise code overall.pptx file, not a potx file.
The presentation file contains no slides, but does contain a PowerPoint
theme, which will be used throughout this example.
4. Add single-valued attributes to the presentation
This section covers adding single-valued and range attributes to the presentation. The presentation makes use of a combination of bulleted lists and tables to present single-valued data, and each approach will be handled separately.Create and populate a title slide
ThePresentation object currently refers to an empty PowerPoint presentation.
First, create a new slide based on the ‘Title’ slide layout, and then
populate the title and sub-title content shapes.
The Presentation.slide_layouts list contains the slide layouts in the same
order as they appear in the ‘Slide Master’ view in PowerPoint. There is no
convenient way to determine the required layout programmatically, so instead
get the slide layout from PowerPoint and hardcode it here. Then add a new
slide based on that layout.
text property.
Presentation object does not provide any feedback. The prs variable can be
saved to disk as a pptx file at any point by using the Presentation.save method.
First, ensure the output directory exists. This directory will already exist if a notebook
was run previously that saved a file to disk.
presentation_title_only.pptx file to see the presentation template populated with
the PRESENTATION_TITLE_ATTRIBUTE attribute value. A screenshot of the presentation title
slide is shown below:

prs.save method to save the
presentation to disk.
Modify the presentation properties
PowerPoint presentations contain metadata describing the presentation content. These are exposed in thePresentation.core_properties
property, and are modified below to specify values for the title,
version, author, and comments.
Material details slide
The material description attributes will be written to a general content slide layout, with each separate attribute formatted as an entry in a bulleted list. The same high-level process will be followed as with the title slide: identify the layout, create the slide, and then populate the slide with the exported data. First however, define a function that formats an attribute value to a suitable string format depending on the attribute type. The default format specifier:.3g uses a general format with at
most 3 digits of precision.

Room temperature properties slide
Next, write the room-temperature material properties to the presentation. This step is similar to the material detail attributes above, except the data is written to a table with separate label, value, and unit columns. First, create the new slide as before, but this time populate the table placeholder with a table containing 3 columns and a number of rows equal to the number of exported attributes. Then set the width of the columns in inches, based on some pre-computed sensible values.
5. Add functional data to the presentation
Plot functional attributes withseaborn and matplotlib, convert the plots to image files, and
then embed these in the presentation. Note: This section assumes the user is familiar with
seaborn and matplotlib. For more examples and guidance in using these libraries see the
previous Data Analytics examples.
First, define a helper function to convert a matplotlib plot to a png file.
seaborn and set the plot style.
seaborn plots.
