Google 套件 - Apps 脚本 - 通过 API 将幻灯片下载为 PNG 文件

Google Suite - Apps Script - Download Slides as PNG files via API

大家早上好。我写了一个简短的脚本,它根据电子表格中的行批量创建 [单页] Google 幻灯片。在每次创建的循环中,我想在 Google 驱动器(或在用户桌面上下载)中创建幻灯片的 PNG。这些图片的规格应该与用户单击“文件”>“下载”>“PNG”时的规格相同 - 沉重的小文本需要完整的投影仪高清 - 所以我不相信我可以使用显示为 1600 像素的 "Thumbnail" 功能。

我下面的代码生成错误 "Converting from text/html to image/png is not supported" - 所以我不确定这是 API 的限制还是我的编码问题。提前谢谢你。

  var options =
     {
       "contentType" : "image/PNG"
     };
  var url = 'https://docs.google.com/presentation/d/' + presentationCopyId + '/export/PNG';
  var response = UrlFetchApp.fetch(url, options);
  var image = response.getAs(MimeType.PNG);
  image.setName(SlideName);
  DriveApp.createFile(image);  

此答案已过时,留作文档用途,但请参阅


答案:

遗憾的是,无法使用 Slides 或 Drive API 将幻灯片导出为 PNG 文件。

更多信息:

According to the documentation,只有四种可用于导出演示文稿文件的 MimeType:

  • application/vnd.openxmlformats-officedocument.presentationml.presentation
  • application/vnd.oasis.opendocument.presentation
  • application/pdf
  • text/plain

尝试导出到 image/png MIME 类型会导致以下错误:

Converting from text/html to image/png is not supported

出于测试目的,我尝试使用 /export/pdf 端点并从那里进行第二次转换为 PNG,如下所示:

function slidesAsPngAttempt() {
  var presentationCopyId = "1Loa...pQs";  
  var options =
      {
        "contentType" : "application/pdf"
      };
  // for exporting to pdf the /export/pdf needs to be all lower case to avoid 404
  var url = 'https://docs.google.com/presentation/d/' + presentationCopyId + '/export/pdf';  
  var response = UrlFetchApp.fetch(url, options);
  var pdfAsblob = response.getBlob();
 
  var image = pdfAsblob.getAs('image/png');
  
  image.setName(DriveApp.getFileById(presentationCopyId).getName());
  DriveApp.createFile(image);  
}

不幸的是,当运行 var image = pdfAsblob.getAs('image/png'):

时会出现类似的错误

Converting from application/pdf to image/png is not supported.

来自相同的导出 MIME 类型参考文档,可用于 PDF 文件的唯一导出类型是:

  • text/csv
  • text/tab-separated-values
  • application/zip

参考文献:

是的,有可能。

您可以使用 Google 幻灯片 API 并为 Google 幻灯片的每一页制作一个 PNG 文件。

这是代码, 但首先您必须启用 API,如下所示

  1. 打开脚本编辑器
  2. 点击资源->高级Google服务
  3. 启用 Google 幻灯片 API 和驱动器 API 。
  4. 点击确定

现在复制并粘贴此代码, 并在 presentationID 中写入您的幻灯片 ID。

function generateScreenshots() {
  
  var presentationId = "***ADD YOUR Google Slide ID Only***";
  var presentation = SlidesApp.openById(presentationId);
  var baseUrl =
    "https://slides.googleapis.com/v1/presentations/{presentationId}/pages/{pageObjectId}/thumbnail";
  var parameters = {
    method: "GET",
    headers: { Authorization: "Bearer " + ScriptApp.getOAuthToken() },
    contentType: "application/json",
    muteHttpExceptions: true
  };

  // Log URL of the main thumbnail of the deck
  Logger.log(Drive.Files.get(presentationId).thumbnailLink);

  // For storing the screenshot image URLs
  var screenshots = [];

  var slides = presentation.getSlides().forEach(function(slide, index) {
    var url = baseUrl
      .replace("{presentationId}", presentationId)
      .replace("{pageObjectId}", slide.getObjectId());
    var response = JSON.parse(UrlFetchApp.fetch(url, parameters));

    // Upload Googel Slide image to Google Drive
    var blob = UrlFetchApp.fetch(response.contentUrl).getBlob();
    DriveApp.createFile(blob).setName("Image " + (index + 1) + ".png");

    screenshots.push(response.contentUrl);
  });

  return screenshots;
}