Can I get speaker notes from Google Slides by API?

[亡魂溺海] 提交于 2020-07-05 08:05:57

问题


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?


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/40713058/can-i-get-speaker-notes-from-google-slides-by-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!