Search code examples
google-apps-scriptgoogle-slides-apigoogle-slides

Downloading a Google Slides presentation as PowerPoint doc using Google Apps Script?


The GUI of Google Slides offers to download a GSlides presentation as a Powerpoint (myFile.pptx). I could not find the equivalent in the Google Apps Script documentation - any pointer?

EDIT

Thanks to comments and answers, I tried this snippet:

function testFileOps() {
  // Converts the file named 'Synthese' (which happens to be a Google Slide doc) into a pptx
  var files = DriveApp.getFilesByName('Synthese');
  var rootFolder = DriveApp.getRootFolder();
  while (files.hasNext()) {
    var file = files.next();
    var blobPptx = file.getBlob().getAs('application/vnd.openxmlformats-officedocument.presentationml.presentation');
    var result = rootFolder.createFile(blobPptx);
  }
}

It returns an error:

Converting from application/vnd.google-apps.presentation to application/vnd.openxmlformats-officedocument.presentationml.presentation is not supported. (line 7, file "Code")

SECOND EDIT

As per another suggestion in comments, I tried to make an http call from Google App Script, that would directly convert the gslides into pptx, without size limit. It produces a file on G Drive, but this file is corrupted / unreadable. The GAS script:

function convertFileToPptx() {
  // Converts a public Google Slide file into a pptx
 var rootFolder = DriveApp.getRootFolder();
 var response = UrlFetchApp.fetch('https://docs.google.com/presentation/d/1Zc4-yFoUYONXSLleV_IaFRlNk6flRKUuAw8M36VZe-4/export/pptx');
 var blobPptx = response.getContent();
 var result = rootFolder.createFile('test2.pptx',blobPptx,MimeType.MICROSOFT_POWERPOINT);
}

Notes:

  • I got the mime type for pptx here
  • using the mime type 'pptx' returns the same error message

Solution

  • How about this modification?

    Modification point:

    • response.getContent() returns byte array. So please use response.getBlob().

    Modified script:

    function convertFileToPptx() {
      var fileId = "1Zc4-yFoUYONXSLleV_IaFRlNk6flRKUuAw8M36VZe-4";
      var outputFileName = "test2.pptx";
    
      var url = 'https://docs.google.com/presentation/d/' + fileId + '/export/pptx';
      var rootFolder = DriveApp.getRootFolder();
      var response = UrlFetchApp.fetch(url);
      var blobPptx = response.getBlob();
      var result = rootFolder.createFile(blobPptx.setName(outputFileName));
    }
    

    Note:

    • If you want to convert Google Slides, which are not published, in your Google Drive, please use access token. At that time please modify url as follows.
      • var url = 'https://docs.google.com/presentation/d/' + fileId + '/export/pptx?access_token=' + ScriptApp.getOAuthToken();
    • DriveApp.createFile() creates a file on root folder as the default.

    References: