【问题标题】:How do I extract the text from a docx file using Apps Script?如何使用 Apps 脚本从 docx 文件中提取文本?
【发布时间】:2019-12-18 10:11:35
【问题描述】:

文件保存在云端硬盘文件夹中。我需要将所有 .docx 文件的文本内容作为 API 有效负载发送。我试过使用Blob,但无济于事。有没有办法做到这一点?

【问题讨论】:

  • 您的问题解决了吗?
  • 是的,你的解决方案做到了。

标签: google-apps-script google-drive-api google-docs docx


【解决方案1】:

如果我理解正确,您想发送您在云端硬盘中拥有的 docx 文件的文本内容。如果这是正确的,那么您可以执行以下操作:

function docx() {
  var docxId ="your-docx-id";
  var docx = DriveApp.getFileById(docxId);
  var blob = docx.getBlob();
  var file = Drive.Files.insert({}, blob, {convert:true});
  var id = file["id"];
  var doc = DocumentApp.openById(id);
  var text = doc.getBody().getText();
  return text;
}

此代码使用Advanced Drive Service 从您通过Drive.Files.insert 获得的blob 创建一个Docs 文件。然后,您可以通过 DocumentApp 轻松访问这个新创建的文件并使用getText

请记住,这将在您每次运行时创建一个新文件。使用Files.delete 来避免这种情况。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2014-02-05
    • 2014-10-03
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多