Adding Shapes and Text to a Slide

Each slide in a presentation can contain a number of page elements, each of which is one of:

This page focuses on how to add shapes to a slide, as well as how you can add text to those shapes.

Shapes

A shape is a page element such as a rectangle, arc, arrow, text box, or other defined type of shape. When you create a shape, you specify:

The transform page property defines how the shape is rendered on the page, including its location and scaling. For more information about transforms, see Sizing and Positioning Page Elements.

Text and shapes

Text can appear on a slide in one of two ways:

  • Within a shape
  • Within a cell in a table

You insert text into a shape or table cell using the insertText request.

Example

The following example adds a square text box to an existing slide and inserts some text into it.

Apps Script

slides/api/Snippets.gs
// Create a new square textbox, using the supplied element ID.
var elementId = 'MyTextBox_01';
var pt350 = {
  magnitude: 350,
  unit: 'PT'
};
var requests = [{
  createShape: {
    objectId: elementId,
    shapeType: 'TEXT_BOX',
    elementProperties: {
      pageObjectId: pageId,
      size: {
        height: pt350,
        width: pt350
      },
      transform: {
        scaleX: 1,
        scaleY: 1,
        translateX: 350,
        translateY: 100,
        unit: 'PT'
      }
    }
  }
},

// Insert text into the box, using the supplied element ID.
{
  insertText: {
    objectId: elementId,
    insertionIndex: 0,
    text: 'New Box Text Inserted!'
  }
}];

// Execute the request.
var createTextboxWithTextResponse = Slides.Presentations.batchUpdate({
  requests: requests
}, presentationId);
var createShapeResponse = createTextboxWithTextResponse.replies[0].createShape;
console.log('Created textbox with ID: %s', createShapeResponse.objectId);

Go

slides/snippets/presentations.go
// Create a new square text box, using a supplied object ID.
textBoxId := "MyTextBox_01"
pt350 := slides.Dimension{
	Magnitude: 350,
	Unit:      "PT",
}
requests := []*slides.Request{{	// Create a new square text box, using a supplied object ID.
	CreateShape: &slides.CreateShapeRequest{
		ObjectId:  textBoxId,
		ShapeType: "TEXT_BOX",
		ElementProperties: &slides.PageElementProperties{
			PageObjectId: slideId,
			Size: &slides.Size{
				Height: &pt350,
				Width:  &pt350,
			},
			Transform: &slides.AffineTransform{
				ScaleX:     1.0,
				ScaleY:     1.0,
				TranslateX: 350.0,
				TranslateY: 100.0,
				Unit:       "PT",
			},
		},
	},
}, {
	// Insert text into the box, using the object ID given to it.
	InsertText: &slides.InsertTextRequest{
		ObjectId:       textBoxId,
		InsertionIndex: 0,
		Text:           "New Box Text Inserted",
	},
}}
// Execute the requests.
body := &slides.BatchUpdatePresentationRequest{
	Requests: requests,
}
response, err := slidesService.Presentations.BatchUpdate(presentationId, body).Do()
if err != nil {
	log.Printf("Unable to create text box. %v", err)
}
fmt.Printf("Created text box with ID: %s", response.Replies[0].CreateShape.ObjectId)

Java

slides/snippets/src/main/java/Snippets.java
// Create a new square text box, using a supplied object ID.
List<Request> requests = new ArrayList<>();
String textBoxId = "MyTextBox_01";
Dimension pt350 = new Dimension().setMagnitude(350.0).setUnit("PT");
requests.add(new Request()
        .setCreateShape(new CreateShapeRequest()
                .setObjectId(textBoxId)
                .setShapeType("TEXT_BOX")
                .setElementProperties(new PageElementProperties()
                        .setPageObjectId(slideId)
                        .setSize(new Size()
                                .setHeight(pt350)
                                .setWidth(pt350))
                        .setTransform(new AffineTransform()
                                .setScaleX(1.0)
                                .setScaleY(1.0)
                                .setTranslateX(350.0)
                                .setTranslateY(100.0)
                                .setUnit("PT")))));

// Insert text into the box, using the object ID given to it.
requests.add(new Request()
        .setInsertText(new InsertTextRequest()
                .setObjectId(textBoxId)
                .setInsertionIndex(0)
                .setText("New Box Text Inserted")));

// Execute the requests.
BatchUpdatePresentationRequest body =
        new BatchUpdatePresentationRequest().setRequests(requests);
BatchUpdatePresentationResponse response =
        slidesService.presentations().batchUpdate(presentationId, body).execute();
CreateShapeResponse createShapeResponse = response.getReplies().get(0).getCreateShape();
System.out.println("Created textbox with ID: " + createShapeResponse.getObjectId());

JavaScript

slides/snippets/snippets.js
// Create a new square textbox, using the supplied element ID.
var elementId = 'MyTextBox_01';
var pt350 = {
  magnitude: 350,
  unit: 'PT'
};
var requests = [{
  createShape: {
    objectId: elementId,
    shapeType: 'TEXT_BOX',
    elementProperties: {
      pageObjectId: pageId,
      size: {
        height: pt350,
        width: pt350
      },
      transform: {
        scaleX: 1,
        scaleY: 1,
        translateX: 350,
        translateY: 100,
        unit: 'PT'
      }
    }
  }
},

// Insert text into the box, using the supplied element ID.
{
  insertText: {
    objectId: elementId,
    insertionIndex: 0,
    text: 'New Box Text Inserted!'
  }
}];

// Execute the request.
gapi.client.slides.presentations.batchUpdate({
  presentationId: presentationId,
  requests: requests
}).then((createTextboxWithTextResponse) => {
  var createShapeResponse = createTextboxWithTextResponse.result.replies[0].createShape;
  console.log(`Created textbox with ID: ${createShapeResponse.objectId}`);
});

Node.js

slides/snippets/snippets.js
// Create a new square textbox, using the supplied element ID.
let elementId = 'MyTextBox_01';
let pt350 = {
  magnitude: 350,
  unit: 'PT',
};
let requests = [{
  createShape: {
    objectId: elementId,
    shapeType: 'TEXT_BOX',
    elementProperties: {
      pageObjectId: pageId,
      size: {
        height: pt350,
        width: pt350,
      },
      transform: {
        scaleX: 1,
        scaleY: 1,
        translateX: 350,
        translateY: 100,
        unit: 'PT',
      },
    },
  },
},
// Insert text into the box, using the supplied element ID.
{
  insertText: {
    objectId: elementId,
    insertionIndex: 0,
    text: 'New Box Text Inserted!',
  },
}];
// Execute the request.
this.slidesService.presentations.batchUpdate({
  presentationId,
  resource: {requests},
}, (err, createTextboxWithTextResponse) => {
  let createShapeResponse = createTextboxWithTextResponse.replies[0].createShape;
  console.log(`Created textbox with ID: ${createShapeResponse.objectId}`);
});

PHP

slides/snippets/src/SlidesSnippets.php
<?php
// Create a new square textbox, using the supplied element ID.
$elementId = 'MyTextBox_01';
$pt350 = array('magnitude' => 350, 'unit' => 'PT');
$requests = array();
$requests[] = new Google_Service_Slides_Request(array(
    'createShape' => array(
        'objectId' => $elementId,
        'shapeType' => 'TEXT_BOX',
        'elementProperties' => array(
            'pageObjectId' => $pageId,
            'size' => array(
                'height' => $pt350,
                'width' => $pt350
            ),
            'transform' => array(
                'scaleX' => 1,
                'scaleY' => 1,
                'translateX' => 350,
                'translateY' => 100,
                'unit' => 'PT'
            )
        )
    )
));

// Insert text into the box, using the supplied element ID.
$requests[] = new Google_Service_Slides_Request(array(
    'insertText' => array(
        'objectId' => $elementId,
        'insertionIndex' => 0,
        'text' => 'New Box Text Inserted!'
    )
));

// Execute the requests.
$batchUpdateRequest = new Google_Service_Slides_BatchUpdatePresentationRequest(array(
    'requests' => $requests
));
$response = $slidesService->presentations->batchUpdate($presentationId, $batchUpdateRequest);
$createShapeResponse = $response->getReplies()[0]->getCreateShape();
printf("Created textbox with ID: %s\n", $createShapeResponse->getObjectId());

Python

slides/snippets/slides_snippets.py
# Create a new square textbox, using the supplied element ID.
element_id = 'MyTextBox_01'
pt350 = {
    'magnitude': 350,
    'unit': 'PT'
}
requests = [
    {
        'createShape': {
            'objectId': element_id,
            'shapeType': 'TEXT_BOX',
            'elementProperties': {
                'pageObjectId': page_id,
                'size': {
                    'height': pt350,
                    'width': pt350
                },
                'transform': {
                    'scaleX': 1,
                    'scaleY': 1,
                    'translateX': 350,
                    'translateY': 100,
                    'unit': 'PT'
                }
            }
        }
    },

    # Insert text into the box, using the supplied element ID.
    {
        'insertText': {
            'objectId': element_id,
            'insertionIndex': 0,
            'text': 'New Box Text Inserted!'
        }
    }
]

# Execute the request.
body = {
    'requests': requests
}
response = slides_service.presentations() \
    .batchUpdate(presentationId=presentation_id, body=body).execute()
create_shape_response = response.get('replies')[0].get('createShape')
print('Created textbox with ID: {0}'.format(
    create_shape_response.get('objectId')))

Ruby

slides/snippets/lib/file_snippets.rb
# Create a new square textbox, using the supplied element ID.
element_id = 'MyTextBox_01'
pt350 = {
  magnitude: '350',
  unit:      'PT'
}
requests = [
  {
    create_shape: {
      object_id_prop:     element_id,
      shape_type:         'TEXT_BOX',
      element_properties: {
        page_object_id: page_id,
        size:           {
          height: pt350,
          width:  pt350
        },
        transform:      {
          scale_x:     '1',
          scale_y:     '1',
          translate_x: '350',
          translate_y: '100',
          unit:        'PT'
        }
      }
    }
  },
  # Insert text into the box, using the supplied element ID.
  {
    insert_text: {
      object_id_prop:  element_id,
      insertion_index: 0,
      text:            'New Box Text Inserted!'
    }
  }
]

# Execute the request.
req = Google::Apis::SlidesV1::BatchUpdatePresentationRequest.new(requests: requests)
response = slides_service.batch_update_presentation(
  presentation_id,
  req
)
create_shape_response = response.replies[0].create_shape
puts "Created textbox with ID: #{create_shape_response.object_id}"

For more information about controlling the size and position of shapes, see Size and Position Shapes.