Can I get speaker notes from Google Presentations 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: presentation.pages.get

What would be a good way to do this?

+4
source share
2 answers

In the absence of an API, I would not say that this is a good way to do this. This is actually terrible . But there it is. If you absolutely need it. Most likely, this is also a bit strange.

Steps:

  • Export a presentation via the Drive API as a PowerPoint.pptx file
  • - zip , XML.
  • (, XML ..).

? Script:

  • API script ( > Google).

    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');
    }
    
+1

Source: https://habr.com/ru/post/1661402/


All Articles