简体   繁体   English

使用 google-slides-api 自动生成 powerpoint

[英]generate powerpoint automatically using google-slides-api

I was using this website as a source to auto-generate google slides using the contents in google spreadsheet.我使用这个网站作为使用谷歌电子表格中的内容自动生成谷歌幻灯片的来源。 All works fine but I cant make it save it to a folder in my google drive.一切正常,但我无法将其保存到我的谷歌驱动器中的文件夹中。 Can someone help me?有人能帮我吗?

I tried:我试过:

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']

then getting an error:然后得到一个错误:

HttpError: https://slides.googleapis.com/v1/presentations?alt=json returned "Invalid JSON payload received. Unknown name "parents": Cannot find field."> HttpError:https://slides.googleapis.com/v1/presentations?alt=json 返回“收到的 JSON 负载无效。未知名称“parents”:找不到字段。">

  • You want to create a Google Slides to the specific folder using google-api-python-client with python.您想使用带有 python 的 google-api-python-client 为特定文件夹创建 Google 幻灯片。
  • You want to use Drive API v2.您想使用 Drive API v2。
    • From your file_metadata , I understood like this.从你的file_metadata ,我是这样理解的。
  • You have already been able to get and put values for Google Slides using Slides API.您已经能够使用 Slides API 获取和放置 Google Slides 的值。

If my understanding is correct, how about this answer?如果我的理解是正确的,这个答案怎么样? Please think of this as just one of several possible answers.请将此视为几种可能的答案之一。

Modification points:改装要点:

  • SLIDES.presentations().create() is used for the Slides API. SLIDES.presentations().create()用于幻灯片 API。 In this case, file_metadata = {'title': 'spreadsheet data DEMO','parents': {'id':folder_id}} cannot be used.在这种情况下, file_metadata = {'title': 'spreadsheet data DEMO','parents': {'id':folder_id}} The reason of your issue is this.你的问题的原因是这个。
  • In your case, Drive API is required to be used.在您的情况下,需要使用 Drive API。 So please add the scope of https://www.googleapis.com/auth/drive .所以请添加https://www.googleapis.com/auth/drive的范围。

Preparation:准备:

Before you run the script, please update the scopes using the following flow.在运行脚本之前,请使用以下流程更新范围。 If your access token has this scope, it is not required to do the following flow.如果您的访问令牌具有此范围,则不需要执行以下流程。

  1. Add https://www.googleapis.com/auth/drive to the scopes.https://www.googleapis.com/auth/drive添加到范围。
  2. Remove the credential file including the refresh token and access token.删除包括刷新令牌和访问令牌的凭证文件。 This file is created at the first run of the file.该文件是在第一次运行文件时创建的。
  3. Run the script.运行脚本。 And please authorize the scopes again.并请再次授权范围。

By this, the refresh token and access token with new scopes are retrieved and new credential file is created.这样,将检索具有新范围的刷新令牌和访问令牌,并创建新的凭证文件。

Modified script:修改后的脚本:

Pattern 1:模式一:

In this pattern, at first, the Google Slides is created by Slides API and the created Google Slides is moved to the specific folder.在此模式中,首先通过 Slides API 创建 Google Slides,并将创建的 Google Slides 移动到特定文件夹。

Modified script: 修改后的脚本:
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)

If Drive API v3 is used, it becomes as follows.如果使用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)

Pattern 2:模式2:

In this pattern, the new Google Slides is directly created to the specific folder using Drive API.在这种模式中,新的 Google 幻灯片是使用 Drive API 直接创建到特定文件夹的。

Sample script 1: Using Drive API v2 示例脚本 1:使用 Drive API v2
 DRIVE = 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)
Sample script 2: Using Drive API v3 示例脚本 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)

Note:笔记:

  • From your question, I couldn't understand which you are using oauth2client or google-auth .从您的问题中,我无法理解您使用的是oauth2client还是google-auth So as the sample, I show DRIVE as DRIVE = build('drive', 'v2', credentials=creds) # or DRIVE = discovery.build('drive', 'v2', http=creds.authorize(Http())) .因此,作为示例,我将DRIVE显示为DRIVE = build('drive', 'v2', credentials=creds) # or DRIVE = discovery.build('drive', 'v2', http=creds.authorize(Http())) Please use DRIVE and SLIDES you are using by modifying the version and name.请通过修改版本和名称来使用您正在使用的DRIVESLIDES

References:参考:

If I misunderstood your question and this was not the direction you want, I apologize.如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM