Caleb Shade
Caleb Shade

Reputation: 1

Text not being added when using Slides API to change text

I am currently trying to automatically update a slide in a Google Slide presentation with the Slides API and when I run the code it does not seem to update.

My current code is:

from datetime import datetime
from datetime import timedelta
from oauth2client.client import GoogleCredentials
from google.oauth2 import service_account
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
from apiclient import discovery
import httplib2
import json
import requests
import string

credentials = service_account.Credentials.from_service_account_file("credentials.json")

service = build('slides', 'v1', credentials=credentials)

#Getting the lunch for the next day
tomorrow = datetime.now() + timedelta(days=1)
url = tomorrow.strftime("https://udas.nutrislice.com/menu/api/digest/school/upper-dauphin-high/menu-type/lunch/date/%Y/%m/%d/")
data = requests.get(url)
test = data.json()['menu_items']

#Converting the lunch to look nice and printing it
lunchtext = json.dumps(test, indent=4, sort_keys=True, separators=('\n', ':'))
newlunchtext = lunchtext.replace(r'"', ' ')
newlunchtext = newlunchtext.replace(r'[', r'"')
newlunchtext = newlunchtext.replace(r']', r'"')
print(newlunchtext)

#Preparing to make the request
PRESENTATION_ID = '1DE0NxsRH6nWxlZGsOP-IzZD-qodw_0YLcMKE0QaQ7vE'
OBJECT_ID = 'g6d3de38d16_0_115'
requests = [
        {
        'insertText': {
            'text': newlunchtext
        },
        'objectId': OBJECT_ID,
        'insertionIndex': 0,
    },
] 
body = {
    'requests': requests
}

update = service.presentations().batchUpdate(presentationId = PRESENTATION_ID, body = body).execute

print(update)

When I run it I get a response that looks like this:

"
     French Toast Sticks & Sausage 

     Pulled Pork BBQ on Bun 

     McCain Crispy Tater Tots 

     Garden Salad 
"
<bound method HttpRequest.execute of <googleapiclient.http.HttpRequest object at 0x105519c70>>

I am not sure why it is not working as I have the correct objectId and presentationId.

Upvotes: 0

Views: 384

Answers (1)

Rafa Guillermo
Rafa Guillermo

Reputation: 15377

Answer:

There are a couple of issues with your code: the request JSON and the execution of the batchUpdate

More Information:

Firstly, as per the request documentation of the Slides API, both objectId and insertionIndex are representations that should be included inside the insertText resource. This needs to be changed as such:

requests = [
        {
        'insertText': {
            'text': newlunchtext,
            'objectId': OBJECT_ID,
            'insertionIndex': 0,
        },
    },
]

The other issue is that yo uare not executing the batchUpdate only referencing it. You need to add parentheses in order to call the execute() function:

update = service.presentations().batchUpdate(presentationId = PRESENTATION_ID, body = body).execute()

As a side note - do remember that your service account also needs to have edit access to the Slide for it to be able to make changes.

References:

Upvotes: 2

Related Questions