Created
January 20, 2017 05:58
-
-
Save danromero/23589fb18552f4d0f3f90f072ca77299 to your computer and use it in GitHub Desktop.
Generate slides from template presentation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
from apiclient import discovery | |
from httplib2 import Http | |
from oauth2client import file, client, tools | |
IMG_FILE = 'google-slides.png' # use your own! | |
TMPLFILE = 'title slide template' # 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': 'Google Slides API template DEMO'} | |
print('** Copying template %r as %r' % (rsp['name'], DATA['name'])) | |
DECK_ID = DRIVE.files().copy(body=DATA, fileId=rsp['id']).execute().get('id') | |
print('** Get slide objects, search for image placeholder') | |
slide = SLIDES.presentations().get(presentationId=DECK_ID, | |
fields='slides').execute().get('slides')[0] | |
obj = None | |
for obj in slide['pageElements']: | |
if obj['shape']['shapeType'] == 'RECTANGLE': | |
break | |
print('** Searching for icon file') | |
rsp = DRIVE.files().list(q="name='%s'" % IMG_FILE).execute().get('files')[0] | |
print(' - Found image %r' % rsp['name']) | |
img_url = '%s&access_token=%s' % ( | |
DRIVE.files().get_media(fileId=rsp['id']).uri, creds.access_token) | |
print('** Replacing placeholder text and icon') | |
reqs = [ | |
{'replaceAllText': { | |
'containsText': {'text': '{{NAME}}'}, | |
'replaceText': 'Hello World!' | |
}}, | |
{'createImage': { | |
'url': img_url, | |
'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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment