通过Google Slides Api或Drive api将Google幻灯片导出为图像 [英] export a Google slide as an image via Google Slides Api or Drive api

查看:280
本文介绍了通过Google Slides Api或Drive api将Google幻灯片导出为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用驱动器中存在的模板ppt动态生成Google幻灯片.我需要将此新创建的ppt转换为图像,并将其存储在驱动器本身的另一个文件夹中.演示中可能有1张以上的幻灯片.

I am generating a google slide dynamically using a template ppt present in my drive. I need to convert this newly created ppt as an image and want to store this in another folder in the drive itself. I may have more than 1 slide in the presentation.

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

推荐答案

最简单的实现功能的方法是使用

The probably easiest way to implement your funcitonality is with presentations.pages.getThumbnail

Use this method by specifying the

  • presentationId
  • 您要导出的幻灯片的
  • slidesId
  • mimeType用于导出-例如PNG
  • thumbnailSize-具有图像所需质量的功能
  • presentationId
  • slidesId of the slide you want to export
  • mimeType for export - e.g. PNG
  • thumbnailSize - in function of the desired quality for the image

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

The request will return you the contentURL of type https://lh3.googleusercontent.com/XXXXXXXXXXXXXXXXXX=s1600" which you can use to either view the slide as an image in the browser or to create a file in your Drive from it.

边注:

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

If you want to export the whole presentaiton rather than one slide, use Files: export instead. However, this method only allows you to export the presentation only to PDF, not to PNG (at least not directly).

这篇关于通过Google Slides Api或Drive api将Google幻灯片导出为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆