如何在 Google Slides API 中将幻灯片导出为 HTML/PDF/DOC

San*_*tap 6 google-slides google-slides-api

我正在使用谷歌幻灯片 API 来创建 powerpoint 演示文稿。其他所需功能包括 - 导出为 PDF 导出为 HTML 导出为文档/图像格式。

Slides API有没有给出的方法?

Mau*_*dik 5

Slides API 不直接支持导出,但可以从 Drive API 获得该功能:

https://developers.google.com/drive/v3/reference/files/export

只需使用幻灯片presentationId作为驱动器即可fileId

  • 如果我在网站中单击“导出为 pdf”,我希望将 .pptx 转换并下载为 .pdf 文件。根据上面的答案,我只收到带有编码数据的响应 (2认同)

Sim*_*mas 5

@桑格拉姆

假设您已经创建了幻灯片,将 Google 幻灯片转换为 PDF 的过程如下:

import apiclient.http as client_methods

# exporting the slide deck and specifying the desired final file type
data = drive_client.files().export(fileId=slide_id, mimeType='application/pdf').execute()

# request body to be send together the upload method
 body = {'name':file_name, 'mimeType':'application/pdf'}

# wrapping the binary (data) file with BytesIO class 
fh = client_methods.BytesIO(data)

# creating the Media Io upload class for the file (note that our original slide data is of binary type)
media_body = client_methods.MediaIoBaseUpload(fh, 
                                 mimetype='application/pdf')

# drive API v3 - .create | drive API v2 - .insert
pdf_file_id = drive_client.files().create(body=body, media_body=media_body).execute()['id']

# extra step: moving to desirable folder destination with the function method

def move_files(drive_client, id_, folder_id):
    file_ = drive_client.files().get(fileId=id_, fields='parents').execute()
    drive_client.files().update(fileId=id_, addParents=folder_id,
                           removeParents=file_['parents'][0],
                           fields='id,parents').execute()

# calling the move_files function.
# drive_client is authorized client, folder_id = desired destination directory (from url).

move_files(drive_client, pdf_file_id, folder_id)

Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。

资源:

Drive API v3 导出方法

Drive API v3创建方法

Drive API v3 在文件夹之间移动文件