Extract note from google slide using google-slide-api in Python
This article is one after the previous post (https://omicsacademy.blogspot.com/2021/03/save-all-google-presentation-slides-as.html).
In this post, you are going to see how we can extract the note information from Google slide using google-slide-api in Python. The first part of the code is the same as the previous post.
import urllib.request
import json
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/presentations.readonly']
# The ID of a sample presentation.
PRESENTATION_ID = '1-aTBNXcSIqlMRzn-FHnRmRPbGlh5eY8MgZNaBwo15IM'
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
service = build('slides', 'v1', credentials=creds)
presentation = service.presentations().get(presentationId=PRESENTATION_ID).execute()
slides = presentation.get('slides')
print('The presentation contains {} slides:'.format(len(slides)))
##
for i, slide in enumerate(slides):
print('- Slide #{} contains {} elements.'.format(i + 1, len(slide.get('pageElements'))))
#print(slide)
# Serializing json
#json_object = json.dumps(slide, indent = 4)
#print(json_object)
print("The note is: ")
print(slide['slideProperties']['notesPage']['pageElements'][1]['shape']['text']['textElements'][1]['textRun']['content'])
If you run the following command line:
python test/quickstart2_notes.py
You should get:
The presentation contains 3 slides:
- Slide #1 contains 2 elements.
The note is:
slide1
- Slide #2 contains 1 elements.
The note is:
slide2
- Slide #3 contains 2 elements.
The note is:
Here are the references we used. If you are interested in, please read the references.
Variable slide is a dictionary with multiple levels. So we can convert the dictionary to a json object. The json object will help us get the path to the note which is slide['slideProperties']['notesPage']['pageElements'][1]['shape']['text']['textElements'][1]['textRun']['content'].
Comments
Post a Comment