WESLEY CHUN: Greetings, developers.
As you know, images are a necessity in any presentation.
But if you stop to think about it, text is also important.
Because there's less text in a slide deck compared to images,
any text that you do have has higher impact.
Hi, this is Wesley Chun, your friendly neighborhood G Suite
developer advocate.
Today I'll be showing you how to format that very important text
using the Google Slides API.
One, perhaps surprising, thing about Google Slides
is that reading is different from writing text.
What I mean by that is when you read slides,
you see how the text is formatted and stored
internally.
But when you write text, you just provide the raw copy
and where to write it to.
Make sense?
Furthermore, writing text is different from formatting it.
When you format text, you need to know
something about its internal structure
as well as styling requests, neither of which
were really required when you're just writing it.
In this sense, formatting text is more like reading it.
Think of it this way.
Reading isn't going to be what you can do the most with text
in slide decks.
Rather, you focus on writing and formatting it.
And those are designed to be similar operations
that you would do in the Slides user interface.
When you enter or write text, you're
just typing without formatting.
And that's something you would do typically afterwards.
All right, let's see how to do all three.
Writing text is the most straightforward,
so we'll start with that.
To insert text, send and insert text requests
to the presentation's .batch update method,
providing both the raw text plus the object to write that text
into.
In addition to text boxes, you can write text
into any shape, even the weird ones.
Where else can you write text into?
Table cells.
Well, to do that, you must provide the text like normal
but also specify the cell location,
meaning row and column.
And whether you write into a shape or a table cell,
the insertion index defaults to zero,
unless you want that text inserted into another location.
And if so, you need to provide that index.
What else can you do with text besides writing it?
Well, you can delete text, perform a global search
and replace.
But you can also update text formatting
as well as update the bullet style for paragraphs.
Delete and search and replace should
be fairly straightforward.
So today's video is all about text formatting,
so we'll focus on the latter pair.
Before we get started with formatting,
you need to know something about the structure of text
in a presentation.
While writing text doesn't expose you to this,
reading will.
If you read a slide using presentations.get
a peek at your slides will reveal
something that may look like this for a text box.
The text elements include both a paragraph marker,
like bullet glyphs and alignment, as well as
the actual text copy and current styling.
Formatting text involves using the update tech style request,
where you pass in the ID of the shape containing
the affected text, the styling changes to make,
which range of text should be updated,
and which field should be affected by this request.
Adding bullets is similar, using the createParagraphBullets
request.
We'll take a look at both request types in our code
sample.
Now, be sure to have the latest client library installed,
whether it's in Python or in your favorite language.
Also make sure to have created a project in the dev console
with the Slides API enabled.
We recommend you also review the text structure and styling
concept guide for background before proceeding.
If you're all set, let's go to the computer
and look at some formatting code.
We'll start with lines 1 through 13,
which are your standard imports and boilerplate authorization
code, except for what we need for this application,
meaning the read-write scope for slides on line 7
and creation of the service endpoint on line 13.
Lines 15 to 21 is where we create a brand new slide
deck with presentations.create.
From the API response, grab the presentation ID along
with the IDs for the title and subtitle,
text boxes on the front page.
Most commands are sent to the API
by calling presentations.batchupdate.
And lines 23 to 31 are where we build and send the requests
to create a new slide with the main point layout
and also add the main title and subtitle text.
Since createSlide was the first command in the array,
grab the new slide ID at index 0 on line 31,
ignoring the responses for the insert text commands.
Main point layout slides have one giant text box.
And on lines 33 to 36 we use presentations.pages.get
to fetch that ID.
This is the command that you'd use to get
all the objects on any slide.
And since there's only one thing on this slide, the text box,
grab it at index 0 on line 36.
Lines 38 to 44 is where we start putting together
the text formatting commands, starting with inserting six
paragraphs into the text box.
The main point text box default font size is 48.
And on lines 45 to 51, we shrink it down to 32.
A text range of all means to shrink the entire text box.
In future requests, we'll use more specific text ranges,
starting with bolding the first word
in the first paragraph which is at index 0
through 3 on lines 52 to 58.
Note, the end index is excluded.
That's why it's 4.
Bolding is done by setting its style
and using fields to only affect the bold style and nothing
else, meaning leave the font color, type, and size alone.
Lines 59 to 65 is identical to the bold request
but italicize the first word in the second paragraph instead,
which is at indexes 7 to 10.
66 to 72 is where we change the first word
in the last paragraph to a mono-spaced font, specifically
Courier New.
Lines 73 to 77 add bullets to begin all paragraphs
in the entire text range.
The last few lines just send the commands to the API
and lets the user know that everything's done.
Let's run it and see what we get.
We'll see the output as the API requests are made.
And then we're going to confirm, after it's done,
that the new slide deck was created-- ta-da.
You see it appear here in the Drive folder,
where we'll open it up.
There's the title slide.
And boom, here's our fancy formatted second slide.
Wasn't that awesome?
Now you know how to read, write, and format text
in Google Slides presentations.
All it takes is knowing the right request
to send to the API, like the text formatting
ones you see here.
Where can you go now?
Well, if you're brand new to the Slides API,
check out the overview introducing you to the concepts
you need to know.
One of the keys to successfully using the API
is to understand its requests.
The common operations and API samples
docs pages are a great resource to gaining this knowledge.
And if you want to dive straight into the reference docs,
the last link will take you there.
Now, we can all agree that presentations are generally
focused on imparting ideas through images.
Since there's less text, any that you do
have must be more meaningful.
Well, thanks for joining us into this intro to text format
and with Google Slides API.
Now you can format that all-important text,
increasing its impact in your presentations.
Be sure to subscribe to our channel and tune
in again next time.
This is Wesley Chun from Google.
And we'll see you around in the G Suite.
Hey, everyone.
Thanks for watching.
Check out these related videos.
And we'll see you next time.