【问题标题】:How to export slides to HTML/PDF/DOC in google slides API如何在谷歌幻灯片 API 中将幻灯片导出为 HTML/PDF/DOC
【发布时间】:2017-02-14 09:31:13
【问题描述】:

我正在使用 google slides API 来创建 powerpoint 演示文稿。 其他所需的功能是 - 导出为 PDF 导出为 HTML 导出为 Doc/Image 格式。

Slides API 有没有给出方法?

【问题讨论】:

  • 你有没有把这个排序?

标签: google-slides google-slides-api


【解决方案1】:

Slides API 不支持直接导出,但 Drive API 提供了该功能:

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

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

【讨论】:

  • 如果我在我的网站上单击“导出为 pdf”,我希望将 .pptx 转换并下载为 .pdf 文件。按照上面的答案,我只收到了带有编码数据的响应
【解决方案2】:

@桑格拉姆

假设您已经创建了幻灯片,将 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)

希望这会有所帮助。

资源:

Drive API v3 export method

Drive API v3 create method

Drive API v3 move files between folders

【讨论】:

    【解决方案3】:

    正如@Codik 所说,Google Slides API 不支持直接导出,但您可以使用 Google Drive API 进行导出。

    这是我的解决方案。在导出之前,我还要替换侧面的一些文本。

    $substitutions = [
            'text_block_1' => "My First Text",
            'text_block_2' => "My Second Text"
        ];
    
    // You can fond the google slide Id in the URL
    $templateId = "Here comes the Google Slide Id";
    
    $client = $certificationService->getGoogleClient();
    
    $driveService = new \Google_Service_Drive($client);
    $slidesService = new \Google_Service_Slides($client);
    
    
    $copy = new \Google_Service_Drive_DriveFile(array(
        'name' => 'Pdf Name' . ' presentation'
    ));
    
    $driveResponse = $driveService->files->copy($templateId, $copy);
    $fileId = $driveResponse->id;
    
    // replaceable text should be covered with {{}} 
    // example = {{text_block_1}}
    $requests = [];
        foreach ($substitutions as $key => $value) {
            $requests[] = new \Google_Service_Slides_Request(
                [
                    'replaceAllText' => [
                        'containsText' => [
                            'text' => '{{'.$key.'}}',
                            'matchCase' => true
                        ],
                        'replaceText' => $value
                    ]
                ]
            );
        }
    
    $batchUpdateRequest = new \Google_Service_Slides_BatchUpdatePresentationRequest(array(
            'requests' => $requests
        ));
    
    
    $updateResponse = $slidesService->presentations->batchUpdate($fileId, $batchUpdateRequest);
    
    
    // here you can specify the file type you want to export as
    $response = $driveService->files->export($fileId, 'application/pdf', array('alt' => 'media'));
    
    
    $content = $response->getBody()->getContents();
    
    // delete the exported file from the Google Drive
    $driveService->files->delete($fileId);
    

    注意:- 为了使此代码正常工作,谷歌幻灯片应与 Google 服务帐户电子邮件共享。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-03
      • 1970-01-01
      相关资源
      最近更新 更多