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

How do I extract the text from a docx file using Apps Script?

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

如果我没有理解错的话,您想发送您在云端硬盘中的一个 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 to create a Docs file out of the blob you get from the docx, via Drive.Files.insert. Then, you can easily access this newly created file via DocumentApp and use getText.

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

希望对您有所帮助。