Basic writing

The Google Slides API lets you 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:

  • PRESENTATION_ID—Indicates where you provide the presentation ID. You can discover the value for this ID from the presentation URL.
  • PAGE_ID—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.
  • PAGE_ELEMENT_ID—Indicates where you provide the page element object ID. You can specify this ID for elements you create (with some restrictions) or allow the Slides API to automatically create one. Element IDs can be retrieved through an API read request.

These examples are presented as HTTP requests to be language neutral. To learn how to implement a batch update in different languages using the Google API client libraries, see Add shapes and text.

Add a text box to a slide

The following presentations.batchUpdate code sample shows how to use the CreateShapeRequest method to add a new text box (containing the string "My Added Text Box") to a slide specified by the PAGE_ID. Two requests are specified in the request body—one to create the text box shape (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 use it in the same API call, reducing overhead.

The following is the request protocol to add a text box to a slide:

POST https://slides.googleapis.com/v1/presentations/PRESENTATION_ID:batchUpdate
{
  "requests": [
    {
      "createShape": {
        "objectId": PAGE_ELEMENT_ID,
        "shapeType": "TEXT_BOX",
        "elementProperties": {
          "pageObjectId":  PAGE_ID,
          "size": {
            "width": {
              "magnitude": 150,
              "unit": "PT"
            },
            "height": {
              "magnitude": 50,
              "unit": "PT"
            }
          },
          "transform": {
            "scaleX": 1,
            "scaleY": 1,
            "translateX": 200,
            "translateY": 100,
            "unit": "PT"
          }
        }
      }
    },
    {
      "insertText": {
        "objectId": PAGE_ELEMENT_ID,
        "text": "My Added Text Box",
        "insertionIndex": 0
      }
    }
  ]
}

Add an image to a slide

The following presentations.batchUpdate code sample shows how to use the CreateImageRequest method to add an image to a slide specified by the PAGE_ID. The API retrieves the image using the IMAGE_URL. This request also scales and positions the image in the slide.

The following is the request protocol to add an image to a slide:

POST https://slides.googleapis.com/v1/presentations/PRESENTATION_ID:batchUpdate
{
  "requests": [
    {
      "createImage": {
        "url": IMAGE_URL,
        "elementProperties": {
          "pageObjectId":  PAGE_ID,
          "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 code sample shows how to use the DeleteObjectRequest method to delete the page element specified by PAGE_ELEMENT_ID and the slide specified by PAGE_ID using two separate requests.

The following is the request protocol to delete a page or page element:

POST https://slides.googleapis.com/v1/presentations/PRESENTATION_ID:batchUpdate
{
  "requests": [
    {
      "deleteObject": {
        "objectId": PAGE_ELEMENT_ID
      },
      "deleteObject": {
        "objectId":  PAGE_ID
      }
    }
  ]
}

Edit text in a specified shape

The following presentations.batchUpdate code sample shows how to use the DeleteTextRequest method to replace a portion of the text present in the shape specified by PAGE_ELEMENT_ID. To accomplish this, first delete text using the zero-based startIndex and then insert 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 specified shape. To replace text everywhere within a presentation, use the ReplaceAllTextRequest method.

The following is the request protocol to edit text in a specified shape:

POST https://slides.googleapis.com/v1/presentations/PRESENTATION_ID:batchUpdate
{
  "requests": [
    {
      "deleteText": {
        "objectId": PAGE_ELEMENT_ID,
        "textRange": {
          "type": "FROM_START_INDEX",
          "startIndex": 15
        }
      }
    },
    {
      "insertText": {
        "objectId": PAGE_ELEMENT_ID,
        "text": "Trapezoid",
        "insertionIndex": 15
      }
    }
  ]
}

Replace a shape tag with an image

Tags are text boxes or shapes with a unique string name, such as "account-holder-name".

The following presentations.batchUpdate code sample shows how to use the CreateImageRequest method to replace a single instance of a shape tag with an image, maintaining the same position and scaling it to fit the tag's size while keeping 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.

The CreateImageRequest method only replaces a specified shape. To replace tag shapes everywhere within a presentation, use a ReplaceAllShapesWithImageRequest method.

The shape tag has the following PageElement properties (which can be found using a presentations.pages.get request):

{
  "objectId": PAGE_ELEMENT_ID,
  "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"
  }
}

The shape resides on the slide specified by PAGE_ID. To specify the image that replaces the shape, the API retrieves the image using the IMAGE_URL. To preserve the image aspect ratio while limiting it to the size of the tag, the CreateImageRequest method sets both the image size to the product of the tag size and scale, and the image scale factors to 1. For more information, see Preserve aspect ratio.

The following is the request protocol to replace a shape tag with an image:

POST https://slides.googleapis.com/v1/presentations/PRESENTATION_ID:batchUpdate
{
  "requests": [
    {
      "createImage": {
        "url": IMAGE_URL,
        "elementProperties": {
          "pageObjectId":  PAGE_ID,
          "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": PAGE_ELEMENT_ID
      }
    }
  ]
}