hisayan
hisayan

Reputation: 89

Can I get speaker notes from Google Slides by API?

I want to get speaker notes from Google Slides by API, but I could not find any fields for speaker notes.

For reference: Method: presentations.pages.get

What would be a good way to do this?

Upvotes: 7

Views: 1634

Answers (2)

Bardy
Bardy

Reputation: 2170

In the absence of the API, I wouldn't suggest this is a good way of doing it. In fact it is horrible. But here it is. If you absolutely had to do it. It is likely a bit flakey too.

Steps are:

  1. Export the presentation, via the Drive API, as a PowerPoint .pptx file
  2. Unpack the file - it is a zip file containing a directory structure with XML files in.
  3. Identify the speaker notes files and process them as per your requirement (e.g. extract all text, or work on the XML etc).

Ugly right? Here's an example in Apps Script:

  1. Enable Drive API in Advanced Services within your script (Resources > Advanced Google Services).

    function example() {
      // Print out the speaker notes
      Logger.log(getNotes('123abc......asd'));
    }
    
    // Returns an array of strings, one string per slide
    // representing the speaker notes.
    function getNotes(presentationId) {
      //DriveApp.createFile();
      var notesRegex = /ppt\/notesSlides\/notesSlide\d+\.xml/;
    
      var url = 'https://www.googleapis.com/drive/v2/files/' + presentationId +
        '/export?mimeType=application%2Fvnd.openxmlformats-officedocument.presentationml.presentation';
      var options = {
        headers: {
          Authorization : 'Bearer ' + ScriptApp.getOAuthToken()
        }
      };
      var response = UrlFetchApp.fetch(url, options);
    
      var zipBlob = Utilities.newBlob(response.getContent(), 'application/zip');
      var data = Utilities.unzip(zipBlob);
    
      var notes = [];
      for (var i = 0; i < data.length; i++) {
        if (notesRegex.test(data[i].getName())) {
    
          // Example simply extracts text from speaker notes
          // You could do something more complex.
          notes.push(extractTextFromXml(data[i].getDataAsString()));
        }
      }
      return notes;
    }
    
    function extractTextFromXml(xml) {
      var doc = XmlService.parse(xml);
      var root = doc.getRootElement();
      var ns = root.getNamespace('a');
    
      var text = [];
    
      function walkNode(node) {
        if (node.getText()) {
          text.push(node.getText());
        }
        var children = node.getChildren();
        if (children.length) {
          children.forEach(function(child) {
            walkNode(child);
          });
        }
      }
      walkNode(root);
      return text.join('\n');
    }
    

Upvotes: 2

Maurice Codik
Maurice Codik

Reputation: 598

Support for speaker notes is now available in the Slides API v1. Documentation is here: https://developers.google.com/slides/how-tos/notes

Upvotes: 4

Related Questions