Automatize presentation reports using Google Slides API

Víctor Pérez Berruezo
Tiendeo Tech
Published in
4 min readFeb 27, 2019

— Update: check out: https://medium.com/@victor.perez.berruezo/python-meetup-automatize-reports-using-google-slides-api-at-pybcn-4f4cadb7ed7d

Usually (and increasingly) in the last years, most of the companies who offer services to 3rd ones, usually need to “show them the numbers”. In the beginning, the reports are created manually by the sellers themselves. But as the company and client portfolio grows, so do the number of reports. That's what’s happening in Tiendeo, and this is our solution.

What will you learn from this post

To make presentations programmatically. First using a template and then some tricks to build the presentation from scratch. Code snippets will be in python.

Which data will be used

To keep it simple I’ll use dummy data, but when needed I’ll take it from our content database.

The basics

  • Guidelines to create manually a template (with placeholders for the content)
  • Create a copy of the template
  • Replace placeholders with text
  • Replace shape placeholders with images
  • Create Batch Requests to reduce the time and requests to GoogleAPI. (# TODO)

Advanced

(# TODO: Ask me if you would like me to start with a specific one)

  • Add slides to a presentation
  • Adding content using shapes and inserting text and images
  • Creating and adding graphs made in Matplotlib
  • Creating custom graphs and figures (handmade)
  • Using template notes to communicate with the “backend”
  • Moving presentations to a specific folder inside the Team Drive tree

Creating the template — guidelines

Guidelines to create manually a template:
We will take a look at all the existing objects so you can identify your needs and jump to the desired part. Example template

  • The easiest actions are replacing text and images, we should aim the design to maximize this kind of interactions.
  • Is not trivial to delete a certain object of the presentation so, initially, we should go for other solutions.

When we copy a presentation the slides and objects ids remain the same

Types of objects we can use:

1. Images

The replace way: We create a shape and we write inside the shape a text that will be used as an “image_id”. We will use this text to search the shape placeholder to replace it with the real image by using the method: ReplaceAllShapesWithImageRequest

I use {{this_notation}} to create the ids. So it has no interference with other notations.

The handmade way: We can also insert the image in a certain slide by using the method: CreateImageRequest

2. Text, values, titles (strings in general)

We can place placeholder strings around the template with a {{similar_id_structure}} than the used for images. So with another function, we will be able to replace them. The used method is ReplaceAllTextRequest

3. Graphs and Plots

The replace way: We can craft them with Matplotlib or any other library, export them as image and follow the previous steps to insert them in the Slides presentation.
The handmade way: We can manually create them using shapes inside slides.

This last solution could be useful if we are sure that we will use a very custom type of graph more than once. (How to craft a custom graph in GoogleSlides using shapes # TODO)

Create a copy of the template

After creating a well-made template, we will need to create a copy to replace the placeholders with the report data by using the drive request files.copy

The file will be copied in the same dir where the template is located. To move the file look up Google Documentation (# TODO: Post about copying files)

To make the requests to GoogleAPIs you need to create a service_account, retrieve credentials and start a service.

# Checkout package: https://pypi.org/project/google-api-support/drive_service = get_service(
secrets_path,
'https://www.googleapis.com/auth/drive',
'drive',
'v3'
)
drive_response = drive_service.files().copy(
fileId=file_from_id,
body=body,
supportsTeamDrives=True, ).execute()

new_file_id = drive_response.get('id')

“supportsTeamDrives” is used when our files use Google Team Drives instead default Google Drive folders

Replace placeholders with text

By using the following function based on the SlidesAPI method ReplaceAllTextRequestwe will be able to replace the “old” text with the “new” one.

Example:

text_replace('main_title', 'This is my new title', presentation_id)

Function:

# Checkout package: https://pypi.org/project/google-api-support/def text_replace(old: str, new: str,presentation_id: str,
pages: list = []):
slides_service = get_service(
secrets_path,
'https://www.googleapis.com/auth/presentations',
'presentations',
'v1'
)

slides_service.presentations().batchUpdate(
body={
"requests": [
{
"replaceAllText": {
"containsText": {
"text": '{{' + old + '}}'
},
"replaceText": new,
"pageObjectIds": pages,
}
}
]
},
presentationId=presentation_id
).execute()

Replace shape placeholders with images

By using the following function based on the SlidesAPI method ReplaceAllShapesWithImageRequest we will be able to replace the placeholder shape with the image.

Example:

replace_shape_with_image(image_url,presentation_id,'first_page_banner')

Function:

# Checkout package: https://pypi.org/project/google-api-support/def replace_shape_with_image(url: str, presentation_id: str, contains_text: str=None):

slides_service.presentations().batchUpdate(
body={
"requests": [
{
"replaceAllShapesWithImage": {
"imageUrl": url,
"replaceMethod": "CENTER_INSIDE",
"containsText": {
"text": "{{" + contains_text + "}}",
}
}
}
]
},
presentationId=presentation_id
).execute()

replaceMethod indicates how image will be replace can have the following arguments `CENTER_INSIDE` or `CENTER_CROP`. More info

Interesting links

--

--

Víctor Pérez Berruezo
Tiendeo Tech

Easy | Enjoy finding solutions · Tecnopolítica · Dades Obertes · Python