Python 使用幻灯片API更改文本时未添加文本

Python 使用幻灯片API更改文本时未添加文本,python,google-slides-api,Python,Google Slides Api,我目前正在尝试使用Slides API自动更新Google幻灯片演示文稿中的幻灯片,当我运行代码时,它似乎不会更新 我目前的代码是: from datetime import datetime from datetime import timedelta from oauth2client.client import GoogleCredentials from google.oauth2 import service_account from googleapiclient.discovery

我目前正在尝试使用Slides API自动更新Google幻灯片演示文稿中的幻灯片,当我运行代码时,它似乎不会更新

我目前的代码是:

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)
当我运行它时,会得到如下响应:

"
     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>>
我不确定它为什么不起作用,因为我有正确的objectId和presentationId。

答案: 您的代码有两个问题:JSON请求和batchUpdate的执行

更多信息: 首先,根据幻灯片API,objectId和insertionIndex都是应该包含在insertText资源中的表示形式。这需要进行如下更改:

请求=[ { “插入文本”:{ “文本”:新文本, “objectId”:对象ID, “insertionIndex”:0, }, }, ] 另一个问题是您不执行batchUpdate,只引用它。需要添加括号才能调用execute函数:

update=service.presentations.batchUpdatepresentationId=PRESENTATION\u ID,body=body.execute 作为旁注-请记住,您的服务帐户还需要具有幻灯片的编辑权限,才能进行更改

我希望这对你有帮助

参考资料: