Search code examples
python-3.xgoogle-drive-apigoogle-slides-api

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.">


Solution

    • You want to create a Google Slides to the specific folder using google-api-python-client with python.
    • You want to use Drive API v2.
      • From your file_metadata, I understood like this.
    • You have already been able to get and put values for Google Slides using Slides API.

    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. In this case, file_metadata = {'title': 'spreadsheet data DEMO','parents': {'id':folder_id}} cannot be used. The reason of your issue is this.
    • In your case, Drive API is required to be used. So please add the scope of 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.
    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.

    Modified script:
    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)
    

    If Drive API v3 is used, it becomes as follows.

    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:

    In this pattern, the new Google Slides is directly created to the specific folder using Drive API.

    Sample script 1: Using 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
    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. So as the sample, I show DRIVE as 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.

    References:

    If I misunderstood your question and this was not the direction you want, I apologize.