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

前端 未结 2 1812
情歌与酒
情歌与酒 2021-01-25 22:56

Good Morning All. I have written a short script which batch-creates [single page] Google Slides based on rows from a spreadsheet. While in the loop for each creation, I would

2条回答
  •  情歌与酒
    2021-01-25 23:30

    This answer is outdated, leaving up for documentation purposes but please see other answer.


    Answer:

    Unfortunately it is not possible to export a Slides as a PNG file using the the Slides nor Drive APIs.

    More Information:

    According to the documentation, there are only four available MimeTypes for exporting Presentations files:

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

    Attempting to export to the image/png MIME Type results in the following error:

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

    For testing purposes, I tried using the /export/pdf endpoint and making a second conversion to PNG from there like so:

    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);  
    }
    

    Unfortunately, a similar error occurs when running var image = pdfAsblob.getAs('image/png'):

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

    From the same export MIME types reference documentation, the only export types available for PDF files are:

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

    So unfortunately, this isn't possible. I know this is generally bad news, but I hope this is helpful to you!

    References:

    • G Suite documents and corresponding export MIME types
    • Google Apps Script - method Blob.getAs(contentType)

提交回复
热议问题