One very useful application of the Slides API is to merge information from one or more data sources into a templated slide deck. There are a number of reasons why this approach is useful, including:
It's easy for designers to fine-tune a presentation's design using the Google Slides editor. This is much easier than adjusting parameters in your app to adjust the rendered slide design.
Separating content from presentation is a well-known design principle with many benefits.
This page outlines how you can take data from an external source and insert it into an existing "template" presentation. The concept is similar to that of a mail merge using a word processor and spreadsheet.
A basic recipe
Here's an example of how you might merge data into a presentation with the Slides API. The overall steps are:
Create your presentation exactly as you want it to appear using dummy content to help you with the design.
For each content element that you'll be inserting, replace the dummy content with a tag. Tags are just text boxes or shapes with a unique string.
In your code, use the Google Drive API to make a fresh copy of the presentation.
In your code, use the Slides API's batchUpdate method, with a set of replaceAllText requests, to perform all the text substitutions throughout the presentation. Use replaceAllShapesWithImage requests to perform image substitutions throughout the presentation.
When setting tags, make sure to use strings that are unlikely
to occur normally. For example,
{{account-holder-name}}
might be a good tag.
Once you've created a deck with tags in it, be sure to make a copy and use the Slides API to manipulate the copy. Don't use the Slides API to manipulate your master "template" copy!
The following sections include code snippets that illustrate some of this process. You can also view the above video to see a complete example (Python) combining several of the concepts from the individual sections below.
Text merging
You can use a replaceAllText request to replace all instances of a given text string in a presentation with new text. For merges, this is usually far simpler than finding and replacing each instance of text individually. One reason this is the most robust approach is that the IDs of page elements is difficult to predict, especially as collaborators work to refine and maintain the template presentation.
Example
This example uses the Google Drive API to copy a template presentation, making a new instance of the presentation. Then it uses the Sheets API to read data from a Google Sheets spreadsheet, and finally uses the Slides API to update the new presentation.
The example takes data from three cells in one row of a named range in the
spreadsheet; it then substitutes that data into the presentation wherever
the strings {{customer-name}}
,
{{case-description}}
, and
{{total-portfolio}}
occur.
Apps Script
Go
Java
JavaScript
Node.js
PHP
Python
Ruby
Image merging
You can also merge images into your presentation using a replaceAllShapesWithImage request. This request replaces all instances of shapes containing the provided text string with the provided image. The request automatically positions and scales the image to fit within the tag shape's bounds while preserving the image's aspect ratio.
Example
This example uses the Google Drive API to copy a template presentation, making
a new instance of the presentation. Then it uses the Slides API to find
any shape with the text {{company-logo}}
and replace it with a company logo image. The request will also replace any
shape with the text {{customer-graphic}}
with a different image.
Apps Script
Go
Java
JavaScript
Node.js
PHP
Python
Ruby
Replacing specific text box or image instances
The replaceAllText and replaceAllShapesWithImage requests are useful for replacing tags throughout a presentation, but sometimes you only need to replace elements according to some criteria, such as being located on a specific slide.
In these cases, you must retrieve the IDs of the tag shapes you want to replace. For text replacements, you delete the existing text in those shapes and then insert the new text (see the Edit text in a specified shape example).
Image replacements are more complex. To merge in an image, you need to:
- Get the tag shape's ID.
- Copy the size and transform information from the tag.
- Add your image to the page, using the size and transform information.
- Delete the tag shape.
Preserving the image's aspect ratio while scaling it to the desired size may require some care, as described in the following paragraphs. See also the Replace a shape tag with an image example.
Preserving aspect ratio
When you create images using the Slides API, aspect fits are based only on the image size, not on the size and transform data. The size data that you provide in the createImage request is considered to be the desired size of the image. The API fits the image's aspect ratio to this desired size, then applies the provided transform.
When replacing a tag with an image, you preserve the image's aspect ratio by setting the image's size and scaling as follows:
- width: set to the product of the tag's
width
andscaleX
- height: set to the product of the tag's
height
andscaleY
- scale_x: set to
1
- scale_y: set to
1
This causes the Slides API to aspect fit the image according to the
tag's visual size, rather than its non-scaled size (see the
Replace a shape tag with an image).
Setting the scaling parameters to 1
prevents the image from being scaled
twice.
This arrangement ensures the image's aspect ratio is preserved, and prevents the image from exceeding the size of the tag shape. The image will have the same center point as the tag shape.
Managing templates
For template presentations defined and owned by the application, create the template using a dedicated account representing the application. Service accounts are a good choice and avoid complications with GSuite policies that restrict sharing.
When you create instances of presentations from templates, always use end-user credentials. This gives users full control over the resulting presentation and prevents scaling issues related to per-user limits in Google Drive.
To create a template using a service account, perform the following steps using the application credentials:
- Create a new presentation using presentation.create in the Slides API
- Update the permissions to allow the presentation recipients to read using permissions.create in the Drive API
- Update the permissions to allow template authors to write using permissions.create in the Drive API
- Edit the template as required
To create a new instance of the presentation, perform the following steps using the user credentials:
- Create a copy of the template using files.copy in the Drive API
- Replace values using presentation.batchUpdate in the Slides API.