Skip to main content
This example shows how to use the python-pptx library to generate a Microsoft PowerPoint presentation. At a high level, the notebook goes through the following steps:
  1. Define the data to be included in the presentation
  2. Export the data
  3. Load the presentation template
  4. Add single-valued attributes to the presentation
  5. Add functional data to the presentation
  6. 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.
The next cells define the attributes to be included in each section of the presentation. Apart from the Presentation Title attribute, these are dictionaries which map an attribute name to a label included in the presentation.
The GRAPHS dictionary maps functional attributes to a 4-tuple, where each element of the tuple refers to:
  1. The chart title
  2. The y-axis label
  3. The x-axis label
  4. 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 the python-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 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.
The presentation file used above is an empty 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

The Presentation 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.
Next, populate the values on the new slide. In PowerPoint, all content is contained within a shape. When editing a new slide, most shapes are placeholders, which means their content is undefined. Generally, adding content to a placeholder will turn it into a full shape. To add text to a placeholder, simply use the text property.
Previous cell output:
Modifying the 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.
Next, save the presentation.
Open the 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: A PowerPoint title slide showing the name of the material. The presentation will not be saved to disk again until the report is complete. If you want to see the effects of individual operations on the presentation, use the 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 the Presentation.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.
Next, create the content slide and populate with the exported data.
A screenshot of the generated slide is shown below. A PowerPoint slide titled "Material details" with a bulleted list of material properties.

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.
Next, iterate through each property, format the value appropriately, and write the label, value, and unit to the table.
A screenshot of the generated slide is shown below. A PowerPoint slide titled "Room temperature properties" with a tabulated list of material properties.

5. Add functional data to the presentation

Plot functional attributes with seaborn 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.
Next, import seaborn and set the plot style.
Now, define a function to create a plot for a functional attribute with specified axis labels and optional constraint information.
Finally, use this function to create a plot image for each functional attribute and add it to the presentation. Each plot is added to a separate slide based on a custom slide layout designed for representing seaborn plots.
Previous cell output:
A screenshot of the stress-strain slide is shown below. A PowerPoint slide titled "Stress vs Strain and Temperature" with a graph showing a stress-strain response at multiple temperatures.

6. Save the presentation

Save a final version of the presentation at the path specified.