通过 Google Slides Api 或 Drive api 将 Google 幻灯片导出为图像

export a Google slide as an image via Google Slides Api or Drive api

我正在使用驱动器中的模板 ppt 动态生成 google 幻灯片。我需要将这个新创建的 ppt 转换为图像,并希望将其存储在驱动器本身的另一个文件夹中。我的演示文稿中可能有不止一张幻灯片。

from google.oauth2 import service_account
import googleapiclient.discovery

SCOPES = (
    'https://www.googleapis.com/auth/drive',
    'https://www.googleapis.com/auth/presentations',
)
SERVICE_ACCOUNT_FILE = 'cred.json'

credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES) 

SLIDES = discovery.build('slides', 'v1', credentials=credentials)
DRIVE  = discovery.build('drive',  'v3', credentials=credentials)

TMPLFILE = 'title slide template' 

rsp = DRIVE.files().list(q="name='%s'" % TMPLFILE).execute().get('files')[0]
print(rsp)
DATA = {'name': 'Google Slides API template DEMO', 'parents': ['id_of_the_folder_for_storing_new_ppt']}
print('** Copying template %r as %r' % (rsp['name'], DATA['name']))
DECK_ID = DRIVE.files().copy(body=DATA, fileId=rsp['id']).execute().get('id')
print(DECK_ID)

print('** Replacing placeholder text')

reqs = [
    {'replaceAllText': {
        'containsText': {'text': '{{text}}'},
        'replaceText': final_til[0]
    }},
]

SLIDES.presentations().batchUpdate(body={'requests': reqs},
        presentationId=DECK_ID).execute()

### till this point I was creating a new ppt 
### now I need to save it as an image file

print('DONE')

实现功能最简单的方法可能是 presentations.pages.getThumbnail

通过指定

使用此方法
  • presentationId
  • slidesId 个要导出的幻灯片
  • mimeType 用于导出 - 例如PNG
  • thumbnailSize - 根据所需的图像质量

该请求将 return 您 contentURL 类型 https://lh3.googleusercontent.com/XXXXXXXXXXXXXXXXXX=s1600" 您可以使用它在浏览器中将幻灯片作为图像查看,或者在您的云端硬盘中创建文件。

旁注:

如果要导出整个演示文稿而不是一张幻灯片,请改用 Files: export。但是,此方法只允许您将演示文稿导出为 PDF,而不能导出为 PNG(至少不能直接导出)。