The Slides API allows you to add and modify elements on presentation pages. The examples on this page show how to perform common read operations using the presentations.batchUpdate method.
These examples use the following variables:
- presentationId — indicates where you provide the presentation ID. You can discover the value for this ID from the presentation URL.
- pageId — indicates where you provide the page object ID. You can retrieve the value for this from the URL or by using an API read request.
- pageElementId — indicates where you provide the page element object ID. You can specify this ID for elements you create (with some restrictions) or allow the API to automatically create one; element IDs can be retrieved through an API read request.
Add a text box to a slide
The following presentations.batchUpdate request adds a new text box (containing the string "My Added Text Box") to a slide specified by pageId. Two requests are specified in the request body—one to create the text box (with a given size and location) and a second to insert text into it.
The first request specifies the object ID to use for the text box. This lets the second request modify it in the same API call, reducing overhead.
The request protocol is shown below. The Add Text and Shapes guide shows an example that implements a batch update in different languages using the Google API client libraries.
POST https://slides.googleapis.com/v1/presentations/presentationId:batchUpdate
{ "requests": [ { "createShape": { "objectId": pageElementId, "shapeType": "TEXT_BOX", "elementProperties": { "pageObjectId": pageId, "size": { "width": { "magnitude": 150, "unit": "PT" }, "height": { "magnitude": 50, "unit": "PT" } }, "transform": { "scaleX": 1, "scaleY": 1, "translateX": 200, "translateY": 100, "unit": "PT" } } } }, { "insertText": { "objectId": pageElementId, "text": "My Added Text Box", "insertionIndex": 0 } } ] }
Add an image to a slide
The following presentations.batchUpdate request adds a new image to a slide specified by pageId. The image is specified by providing a URL (imageUrl) that the API fetches from. This request scales and positions the image in the slide.
The request protocol is shown below. The Add Text and Shapes guide shows an example that implements a batch update in different languages using the Google API client libraries.
POST https://slides.googleapis.com/v1/presentations/presentationId:batchUpdate
{ "requests": [ { "createImage": { "url": imageUrl, "elementProperties": { "pageObjectId": pageId, "size": { "width": { "magnitude": 30, "unit": "PT" }, "height": { "magnitude": 30, "unit": "PT" } }, "transform": { "scaleX": 1, "scaleY": 1, "translateX": 200, "translateY": 100, "unit": "PT" } } } } ] }
Delete a page or page element
The following presentations.batchUpdate request deletes the page element specified by pageElementId and the slide specified by pageId using two separate requests.
The request protocol is shown below. The Add Text and Shapes guide shows an example that implements a batch update in different languages using the Google API client libraries.
POST https://slides.googleapis.com/v1/presentations/presentationId:batchUpdate
{ "requests": [ { "deleteObject": { "objectId": pageElementId }, "deleteObject": { "objectId": pageId } } ] }
Edit text in a specified shape
The following presentations.batchUpdate request replaces part of the text present in the shape specified by pageElementId. This is done by first deleting text starting from a start index, then inserting new text in that position. In this example, the original text string "My Shape Text: ????" is replaced with "My Shape Text: Trapezoid".
This request only affects text in a single specified shape. To replace text everywhere within a presentation, use a ReplaceAllTextRequest request.
The request protocol is shown below. The Add Text and Shapes guide shows an example that implements a batch update in different languages using the Google API client libraries.
POST https://slides.googleapis.com/v1/presentations/presentationId:batchUpdate
{ "requests": [ { "deleteText": { "objectId": pageElementId, "textRange": { "type": "FROM_START_INDEX", "startIndex": 15 } } }, { "insertText": { "objectId": pageElementId, "text": "Trapezoid", "insertionIndex": 15 } } ] }
Replace a shape tag with an image
The following presentations.batchUpdate request replaces a single instance of a tag shape with an image, maintaining the same position and scaling it to fit the tag's size while maintaining the image's aspect ratio. The request can also be used to replace one image with another. The request consists of adding the new image and then deleting the tag.
This request only replaces a single specified shape. To replace tag shapes everywhere within a presentation, use a replaceAllShapesWithImage request.
The tag shape has the following properties (which can be found with a presentations.pages.get request):
{ "objectId": pageElementId, "size": { "width": { "magnitude": 3000000, "unit": "EMU" }, "height": { "magnitude": 3000000, "unit": "EMU" } }, "transform": { "scaleX": 1.13, "scaleY": 0.62, "translateX": 4800000, "translateY": 450000, "unit": "EMU" }, "shape": { "shapeType": "RECTANGLE" } }
This shape resides on the slide specified by pageId. To specify the
image that will replace the shape, provide a URL (imageUrl)
for the API to fetch the image from. In order to preserve the image aspect
ratio while limiting it to the size of the tag, the
createImage
request sets the (desired) image size to the product of the tag size and
scale, and the image scale factors to 1
(see the
Preserving aspect ratio guide).
POST https://slides.googleapis.com/v1/presentations/presentationId:batchUpdate
{ "requests": [ { "createImage": { "url": imageUrl, "elementProperties": { "pageObjectId": pageId, "size": { "width": { "magnitude": 3000000 * 1.13, "unit": "EMU" }, "height": { "magnitude": 3000000 * 0.62, "unit": "EMU" } }, "transform": { "scaleX": 1, "scaleY": 1, "translateX": 4800000, "translateY": 450000, "unit": "PT" } } } }, { "deleteObject": { "objectId": pageElementId } } ] }