如何使用 Apps 脚本从 docx 文件中提取文本?

Eru*_*Eru 5 docx google-docs google-apps-script google-drive-api

文件保存在 Drive 文件夹中。我需要将所有 .docx 文件的文本内容作为 API 负载发送。我尝试过使用Blob但无济于事。有办法完成这件事吗?

Iam*_*hus 7

如果我理解正确的话,您想要发送云端硬盘中的 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;
}
Run Code Online (Sandbox Code Playgroud)

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

请记住,每次运行时都会创建一个新文件。使用Files.delete可以避免这种情况。

我希望这有帮助。