我正在使用这个website作为使用谷歌电子表格中的内容自动生成谷歌幻灯片的来源。一切正常,但我无法将其保存到我的谷歌驱动器中的文件夹中。有人可以帮助我吗?
我尝试过:
folder_id = 'xxx'
file_metadata = {'title': 'spreadsheet data DEMO','parents': {'id':folder_id}}
DATA = {'title': 'Generating slides from spreadsheet data DEMO'}
rsp = SLIDES.presentations().create(body=file_metadata).execute()
deckID = rsp['presentationId']
titleSlide = rsp['slides'][0]
titleID = titleSlide['pageElements'][0]['objectId']
subtitleID = titleSlide['pageElements'][1]['objectId']
然后出现错误:
HttpError: https://slides.googleapis.com/v1/presentations?alt=json returned "Invalid JSON payload received. Unknown name "parents": Cannot find field.">
最佳答案
- 您想要使用 google-api-python-client 和 python 在特定文件夹中创建 Google 幻灯片。
- 您想要使用 Drive API v2。
- 来自您的
file_metadata
,我是这样理解的。
- 来自您的
- 您已经能够使用 Slides API 获取和输入 Google Slides 的值。
如果我的理解是正确的,这个答案怎么样?请将此视为几个可能答案之一。
修改点:
-
SLIDES.presentations().create()
用于幻灯片 API。在这种情况下,file_metadata = {'title': 'spreadsheet data DEMO','parents': {'id':folder_id}}
不能使用。您的问题的原因是这样的。 - 根据您的情况,需要使用 Drive API。所以请添加
https://www.googleapis.com/auth/drive
的范围.
准备工作:
在运行脚本之前,请使用以下流程更新范围。如果您的访问 token 具有此范围,则不需要执行以下流程。
- 添加
https://www.googleapis.com/auth/drive
范围。 - 删除凭据文件,包括刷新 token 和访问 token 。该文件是在第一次运行该文件时创建的。
- 运行脚本。请再次授权范围。
由此,将检索具有新范围的刷新 token 和访问 token ,并创建新的凭据文件。
修改后的脚本:
模式 1:
在此模式中,首先,Google Slides 由 Slides API 创建,并将创建的 Google Slides 移动到特定文件夹。
修改后的脚本:SLIDES = build('slides', 'v1', credentials=creds) # or SLIDES = discovery.build('slides', 'v1', http=creds.authorize(Http()))
DRIVE = build('drive', 'v2', credentials=creds) # or DRIVE = discovery.build('drive', 'v2', http=creds.authorize(Http()))
# Create new Google Slides using Slides API.
DATA = {'title': 'spreadsheet data DEMO'}
rsp = SLIDES.presentations().create(body=DATA).execute()
file_id = rsp['presentationId']
# Move created Google Slides to specific folder using Drive API v2.
folder_id = '###'
file_metadata = {'parents': [{'id': folder_id}]}
res = DRIVE.files().update(
fileId=file_id,
body=file_metadata,
).execute()
print(res)
如果使用Drive API v3,则变为如下。
DRIVE = build('drive', 'v3', credentials=creds) # or DRIVE = discovery.build('drive', 'v3', http=creds.authorize(Http()))
folder_id = '###'
res = DRIVE.files().update(
fileId=file_id,
addParents=folder_id,
removeParents='root'
).execute()
print(res)
模式 2:
在此模式中,新的 Google 幻灯片将使用 Drive API 直接创建到特定文件夹。
示例脚本 1:使用 Drive API v2DRIVE = build('drive', 'v2', credentials=creds) # or DRIVE = discovery.build('drive', 'v2', http=creds.authorize(Http()))
folder_id = '###'
file_metadata = {'title': 'spreadsheet data DEMO',
'parents': [{'id': folder_id}],
'mimeType': 'application/vnd.google-apps.presentation'
}
res = DRIVE.files().insert(body=file_metadata).execute()
print(res)
示例脚本 2:使用 Drive API v3
DRIVE = build('drive', 'v3', credentials=creds) # or DRIVE = discovery.build('drive', 'v3', http=creds.authorize(Http()))
folder_id = '###'
file_metadata = {'name': 'spreadsheet data DEMO',
'parents': [folder_id],
'mimeType': 'application/vnd.google-apps.presentation'
}
res = DRIVE.files().create(body=file_metadata).execute()
print(res)
注意:
- 从你的问题来看,我无法理解你正在使用哪个
oauth2client
或google-auth
。因此,作为示例,我显示DRIVE
如DRIVE = build('drive', 'v2', credentials=creds) # or DRIVE = discovery.build('drive', 'v2', http=creds.authorize(Http()))
。请使用DRIVE
和SLIDES
您通过修改版本和名称来使用。
引用文献:
- Files: insert of Drive API v2
- Files: update of Drive API v2
- Files: create of Drive API v3
- Files: update of Drive API v3
如果我误解了您的问题并且这不是您想要的方向,我深表歉意。
关于python-3.x - 使用 google-slides-api 自动生成 powerpoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59799783/