Imagine you need to create plenty of presentations that look the same, but have a slightly different content, like text or images. Well, you can do it all by hand if you have tons of time and find relaxation in doing repeatable tasks over and over again. However, today I’ll give you a better solution. A few months ago Google provided API to its Slides service, which we’ll use to achieve our goal: automating the process of creating presentations.
Real-life use case
One of our clients came to us with a problem. He needed to create many presentations for each of his (very many) products. Up until then, his employees had to do it by hand, but the client found it too time-consuming. He wanted a tool which would automatically generate ready-to-use presentations from a template with just some input. I’ll show you how we achieved it.
Requirements
What you need? Not much, really. Basic PHP knowledge and familiarity with composer (a PHP dependency manager). And a Google account, of course. I strongly recommend you also to read Google PHP quickstart first, as it shows a sample script we based our solution upon. There is also an instruction on how to enable Google Slides API in the Google Api Console, which is a required step for our script to work.
Templates concept
Google Slides API offers a great many possibilities. You can create a presentation adding every single text programmatically, moving it to the correct place and then styling it. However, this process is very time consuming and complicated. Alternatively, you can create a presentation template by hand and then, via API, replace elements that are already positioned and styled. The whole process of creating a template is enclosed in just a few steps:
- Create a new Google Slides presentation, which will be the template from which we will generate other presentations.
- Fill the template with common static content.
- For elements that need to be replaced, use text and shapes with {{ placeholders }}. {{ variableName }} is just a convention which helps to distinguish static text from placeholders.
- Format elements according to your needs.
And here we are! In 4 steps, we have a ready-to-use template. Cool, isn’t it?
Let’s go!
Make sure you’ve enabled the Google Slides API, authenticated yourself and downloaded the credentials as described in the aforementioned quickstart. The script is divided into 4 steps:
- Clone template as a new presentation
- Upload images to Google Drive
- Replace placeholders with target content
- Download presentation as PDF
Clone template as a new presentation
We have to use Google Drive API for this because Google Slides API doesn’t have such a functionality. The code is mostly self-explanatory, so I’ll focus on only some parts of it.
Take a look at the file listing query. To find our template presentation on Drive, we’ll look for it by name (put in TEMPLATE_NAME constant) and mimeType which is application/vnd.google-apps.presentation. The query always returns a collection, so we need to check if any elements are present and choose the first of them. Later on, we just copy the template we’ve found.
Upload images to Google Drive
We don’t want everyone to have access to our uploaded images and, at the same time, we need URLs for them. That’s why after uploading an image we create a private link with our access token and use it for embedding the image later on.
Replace placeholders with target content
I created 2 wrapper functions – one for replacing a text placeholder and the other one for replacing a shape placeholder with an image from URL. There is also a function to execute an array of requests.
Download presentation as PDF
After your work is done, you can also download your presentation as a PDF file. It’s a pretty simple Google Drive API call.
Put all steps together
And here it is! If you use the functions from the steps above, our main function to generate a presentation is very simple:
You can download the full code from here. In README.md you’ll find the instructions for installation and some other details. The project is just a quick start for you to generate presentations according to your needs, so feel free to download and modify.
Let me know if you used this – or if you have any feedback – in the comments 🙂