This page tells you how to edit and style text in your presentation using the Slides API.
About text
Text in your presentation is always contained within a shape or a table cell. The API lets you modify this text in a number of ways:
- You can insert, delete, or replace text in your presentation.
- You can add paragraph formatting to create bulleted lists.
- You can change character formatting such as bold, italics, color, font size, or hyperlinks.
See the concepts page Text Structure and Styling for a general overview of how text styling works in the Slides API. Also check out the above video to see a complete example (Python) combining several of the formatting concepts from the sections below.
Inserting, deleting, or replacing text
There are two ways you can replace text in a presentation using the Slides API: by performing a global search-and-replace, or by explicitly deleting and adding text. Both ways use the batchUpdate method, but with different request types.
Global search and replace
Use ReplaceAllTextRequest to do a global search-and-replace throughout your presentation.
The Text Merging section of the Merging Data guide provides an example of how you can use this request type.
Replacing text within a shape
The Slides API lets you modify the text content of a shape. You can remove individual ranges of text, and you can insert text at a specific location.
Use InsertTextRequest and DeleteTextRequest to perform these operations.
Replacing a specific region of text consists of a deletion and then an insertion, which you can perform using the following steps:
- Identify the page element that contains the text.
- Identify the start and end position of the text to be replaced.
- Call
batchUpdate
with the following two requests:- DeleteTextRequest, specifying the range of text to delete.
- InsertTextRequest, specifying the same start position as well as the text string to be inserted.
To ensure atomicity when you replace text this way, make sure to include both requests in the same batchUpdate call. This is shown in the following example, which replaces all the text in a shape with new text:
Apps Script
Go
Java
JavaScript
Node.js
PHP
Python
Ruby
Changing character formatting
Character formatting determines the rendering of text characters in your presentation, including typeface, color, and hyperlinking.
The concepts page Text Structure and Styling describes how the Slides API represents text styling information.
To change the character formatting of text, use batchUpdate
with the
UpdateTextStyleRequest.
You need to provide the ID of the shape or table cell that contains the text,
as well as a Range
that includes the following information:
- The specifier
FIXED_RANGE
, together with the start and end indexes that define the text range you want to style. - The specifier
FROM_START_INDEX
, together with a start index that defines the beginning of the text range you want to style. - The specifier
ALL
, with no indexes, to style all the text in the target shape.
The following example performs several text styling operations on the text that's contained in a shape:
- Sets the font of characters 0-4 to bold italics.
- Sets the color of characters 5-9 to
blue
14-pt Times New Roman font. - Hyperlinks characters 10-14 to
www.example.com
.
A straightforward way to do this is by building a list of requests and then using one batchUpdate call:
Apps Script
Go
Java
JavaScript
Node.js
PHP
Python
Ruby
Changing paragraph formatting
Paragraph formatting determines how blocks of text are rendered in your presentation, including alignment, indentation, and list ornamentation.
The concepts page Text Structure and Styling describes how the Slides API represents paragraph styling information.
The Slides API supports updating paragraph styles, converting plain paragraphs to bulleted lists, and removing bullets from paragraphs.
Converting paragraphs to a list
A common paragraph formatting operation is converting paragraphs into a bulleted list. The following example converts all the paragraphs of a shape to a bulleted list, and specifies an explicit bullet glyph to use.