【问题标题】:Batch convert jpg to Google Docs将 jpg 批量转换为 Google Docs
【发布时间】:2018-12-08 22:09:26
【问题描述】:

我在 Google 云端硬盘中有一个 jpg 文件夹,我想将其转换为 Google 文档。现在我可以手动选择每一个,并在上下文菜单“在 Google Docs 中打开”这将创建一个新文档,其中图像位于页面顶部,OCR 文本位于下方。我只想对我所有的图像执行此操作。

有一个脚本here 将 gdoc 转换为 docx,我应该能够适应我的情况,但我似乎无法让它工作。

这是我改编的脚本:

function convertJPGtoGoogleDocs() {
  var srcfolderId = "~~~~~~~~~Sv4qZuPdJgvEq1A~~~~~~~~~"; // <--- Please input folder ID.
  var dstfolderId = srcfolderId; // <--- If you want to change the destination folder, please modify this.
  var files = DriveApp.getFolderById(srcfolderId).getFilesByType(MimeType.JPG);
  while (files.hasNext()) {
    var file = files.next();
    DriveApp.getFolderById(dstfolderId).createFile(
      UrlFetchApp.fetch(
        "https://docs.google.com/document/d/" + file.getId() + "/export?format=gdoc",
        {
          "headers" : {Authorization: 'Bearer ' + ScriptApp.getOAuthToken()},
          "muteHttpExceptions" : true
        }
      ).getBlob().setName(file.getName() + ".docx")
    );
  }
}

有人可以帮忙吗?

谢谢。

【问题讨论】:

  • 请不要删除这个!!我没有足够的声誉来添加评论,所以我必须在这里发布。感谢您的好评。但是6分钟限制呢?你能为此做点什么吗?或者至少以某种方式使其在第二次执行中不会再次重复转换后的照片,而只是继续它离开的地方。

标签: google-apps-script google-docs


【解决方案1】:
  • 您想将文件夹中的 Jpeg 文件转换为 Google 文档。
  • 当 Jpeg 文件转换为 Google 文档时,您想使用 OCR。

如果我的理解是正确的,那么这个修改呢?

修改点:

  • 在您修改的脚本中,MimeType.JPG 返回undefined。所以while 中的脚本没有运行。
    • 请使用MimeType.JPEG
  • this answer 的脚本用于将 Google 文档导出为 Microsoft Word。遗憾的是,该脚本不能直接用于将 Jpeg 文件转换为 Google 文档。

如果要修改this answer的脚本,修改如下如何?

当您使用此脚本时,please enable Drive API at Advanced Google Services。这样,API 会在 API 控制台自动启用。 The specification of Google Apps Script Project was Changed at April 8, 2019.

修改脚本:

function convertJPGtoGoogleDocs() {
  var srcfolderId = "~~~~~~~~~Sv4qZuPdJgvEq1A~~~~~~~~~"; // <--- Please input folder ID.
  var dstfolderId = srcfolderId; // <--- If you want to change the destination folder, please modify this.
  var files = DriveApp.getFolderById(srcfolderId).getFilesByType(MimeType.JPEG); // Modified
  while (files.hasNext()) {
    var file = files.next();
    Drive.Files.insert({title: file.getName(), parents: [{id: dstfolderId}]}, file.getBlob(), {ocr: true}); // Modified
  }
}

注意:

参考资料:

如果我误解了你的问题,请告诉我。我想修改它。

【讨论】:

  • @Packwood 欢迎。我很高兴你的问题得到了解决。也谢谢你。
  • 注意 - 您需要通过 Apps 脚本启用 Drive API,而不是通过 GCP。使用脚本编辑器的“资源/高级 Google 服务”菜单条目(位于 script.google.coom)
  • @Avi 感谢您的评论。是的。你是对的。 The specification of Google Apps Script Project was Changed at April 8, 2019。我忘了修改我的答案。我对此深表歉意。我刚刚修改了。
  • 无需道歉。我必须感谢你。您的回答对我帮助很大!
  • @exsonic01 感谢您的评论。我为我糟糕的英语水平深表歉意。从您的错误消息来看,我认为您可能在未在高级 Google 服务中启用 Drive API 的情况下运行了该脚本。那么在 Google 高级服务中启用 Drive API 怎么样? Ref如果这不是直接的解决方案,我很抱歉。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多