【问题标题】:Google Suite - Apps Script - Download Slides as PNG files via APIGoogle Suite - Apps Script - 通过 API 将幻灯片下载为 PNG 文件
【发布时间】:2020-01-21 16:51:00
【问题描述】:

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

我下面的代码生成错误“不支持从 text/html 转换为 image/png” - 所以我不确定这是 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);  

【问题讨论】:

    标签: google-apps-script google-slides-api google-slides


    【解决方案1】:

    是的,有可能。

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

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

    1. 打开脚本编辑器
    2. 点击资源->高级谷歌服务
    3. 启用 Google 幻灯片 API 和 Drive 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;
    }
    

    【讨论】:

    • 非常感谢。是的 - 谷歌似乎已经添加了对 PNG 的支持。对我们所有人来说都是好消息。感谢您在那里潜水。
    • 很好的答案!很高兴再次看到这个。
    【解决方案2】:

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


    答案:

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

    更多信息:

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

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

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

    不支持从 text/html 转换为 image/png

    出于测试目的,我尝试使用/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')时出现类似错误:

    不支持从 application/pdf 转换为 image/png。

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

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

    参考资料:

    【讨论】:

    • 欣赏它,我担心可能是这种情况。 PDF 可能实际上适用于我的流程。但是,修改上面的代码给我留下了“一个帐户。所有谷歌。登录以继续”的 PDF。复制幻灯片的权限设置为查看链接(共享)。我应该调用其他身份验证/设置吗?
    猜你喜欢
    • 1970-01-01
    • 2015-10-18
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多