Integrating with the Google Slides API using Python allows developers to automate slide creation, modify presentations programmatically, and integrate with Google Workspace applications. This guide provides a step-by-step tutorial on how to authenticate, create, edit, and manage Google Slides presentations using Python.
Before you begin, ensure you have the following:
google-auth
, google-auth-oauthlib
, google-auth-httplib2
, and google-api-python-client
.credentials.json
file and store it securely.For more details, check the Google Slides API Quickstart Guide.
To interact with the Google Slides API, install the required Python libraries:
pip install google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client
You can find additional installation details in the Google API Python Client Documentation.
Create an authentication script in Python:
from google.oauth2 import service_account
For more details on authentication, refer to the Google API Authentication Guide.
from googleapiclient.discovery import build
SCOPES = ["https://www.googleapis.com/auth/presentations"]
SERVICE_ACCOUNT_FILE = "path/to/credentials.json"
creds = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = build("slides", "v1", credentials=creds)
Once authentication is complete, you can start interacting with Google Slides API to create, modify, and manage presentations programmatically.
To create a new Google Slides presentation, use the following code:
def create_presentation():
presentation = service.presentations().create(body={"title": "My New Presentation"}).execute()
print(f'Created presentation with ID: {presentation["presentationId"]}')
return presentation["presentationId"]
presentation_id = create_presentation()
Adding a New SlideOnce a presentation is created, you can add a new slide using batchUpdate
:
def add_slide(presentation_id):
requests = [{
"createSlide": {
"objectId": "new_slide_id",
"insertionIndex": 1
}
}]
body = {"requests": requests}
service.presentations().batchUpdate(presentationId=presentation_id, body=body).execute()
print("Added a new slide")
add_slide(presentation_id)
Modifying SlidesYou can also modify existing slides, such as inserting text or images dynamically:
def add_text(presentation_id, slide_id, text):
requests = [{
"createShape": {
"objectId": "text_box_1",
"shapeType": "TEXT_BOX",
"elementProperties": {"pageObjectId": slide_id}
}
}, {
"insertText": {
"objectId": "text_box_1",
"text": text
}
}]
body = {"requests": requests}
service.presentations().batchUpdate(presentationId=presentation_id, body=body).execute()
print("Added text to the slide")
add_text(presentation_id, "new_slide_id", "Hello, Google Slides API!")
ConclusionBy following this guide, you can successfully integrate the Google Slides API with Python to automate presentation creation and management. Whether you need to add slides, insert images, update text, or modify an existing slide deck, the Google Slides API provides powerful capabilities for automation.
Start building today with the Google Slides API documentation!
For more discussions and troubleshooting, visit the Google Slides API Community.