我是Google Slides API的新手,并且正在尝试通过替换图像和文本占位符(供参考,请参见https://www.youtube.com/watch?v=8LSUbKZq4ZY和http://wescpy.blogspot.com/2016/11/using-google-slides-api-with-python.html)来构建每日新闻标题的幻灯片组。
但是当我尝试运行修改后的程序时,出现一条错误消息,提示不存在名为“ client_secret.json”的文件或目录(API教程的代码中包含该文件或目录)。该教程代码来自2年前,所以我不确定Google Slides API中是否有任何更新,但是我非常感谢帮助您解决此问题。下面是我的代码(请注意:“抓取的列表”是词典的列表,每个词典都包含键“ headline”和“ imgURL”的值。)
from __future__ import print_function
from apiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools
from datetime import date
from scrapef2 import scrape
scrapedlist = scrape()
TMPLFILE = 'CrimsonTemplate' # use your own!
SCOPES = (
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/presentations',
)
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
HTTP = creds.authorize(Http())
DRIVE = discovery.build('drive', 'v3', http=HTTP)
SLIDES = discovery.build('slides', 'v1', http=HTTP)
rsp = DRIVE.files().list(q="name='%s'" % TMPLFILE).execute().get('files')[0]
DATA = {'name': '[DN] '+ str(date.today())}
print('** Copying template %r as %r' % (rsp['name'], DATA['name']))
DECK_ID = DRIVE.files().copy(body=DATA, fileId=rsp['id']).execute().get('id') # TO DO: How to copy into a specific folder
for i in range(3):
print('** Get slide objects, search for image placeholder')
slide = SLIDES.presentations().get(presentationId=DECK_ID,
fields='slides').execute().get('slides')[i]
obj = None
for obj in slide['pageElements']:
if obj['shape']['shapeType'] == 'RECTANGLE':
break
print('** Replacing placeholder text and icon')
reqs = [
{'replaceAllText': {
'containsText': {'text': '{{Headline}}'},
'replaceText': scrapedlist[i]["headline"]
}},
{'createImage': {
'url': scrapedlist[i]["imgURL"],
'elementProperties': {
'pageObjectId': slide['objectId'],
'size': obj['size'],
'transform': obj['transform'],
}
}},
{'deleteObject': {'objectId': obj['objectId']}},
]
SLIDES.presentations().batchUpdate(body={'requests': reqs},
presentationId=DECK_ID).execute()
print('DONE')
答案 0 :(得分:0)
从未使用过python google api,但错误表明您没有'client_secret.json'文件或放置在错误的位置。
API使用此文件来自动验证您的身份。这样,所有API调用都是由您进行的。要获取此文件:
在这种情况下,我帮不上什么忙,只是尝试检查库以了解它在哪里查找文件并将其放在那里(库目录,项目根目录,很难分辨)。
让我知道它是否有效,因为Google API及其库有时会发生意外行为。