如何自动化 Google Drive Docs OCR 设施?

How to automate Google Drive Docs OCR facility?

我已经使用 Google Drive 及其使用 Google Docs 打开工具将它们转换为 OCR word 文件 (.docx)。因为 word 文件也保留了格式。我有很多图片上传到Drive,一张一张转换成可编辑的,因为PDF转换不行。

这段时间我想耐心等待完成一个转换过程。之后我开始下一个转换,这很耗时。

我使用了 Google OCR API。但不保留粗体、对齐等格式

那么,有什么方法可以使用 REST API 来自动化这个过程吗?

更新

  1. 已将图像上传到 Google 驱动器

  2. Google 驱动器

  3. 中图像的右键单击上下文菜单
  4. Google "Open with" 上下文菜单中的文档

  5. 转换过程后 OCR(检测到自动语言)

  6. 最后 Google 文档和图像

我尝试了 googleapis on GitHub and I selected the drive sample list.js 代码。

我的代码

'use strict';

const {google} = require('googleapis');
const sampleClient = require('../sampleclient');

const drive = google.drive({
  version: 'v3',
  auth: sampleClient.oAuth2Client,
});

async function runSample(query) {
  const params = {pageSize: 3};
  params.q = query;
  const res = await drive.files.list(params);
  console.log(res.data);
  return res.data;
}

if (module === require.main) {
  const scopes = ['https://www.googleapis.com/auth/drive.metadata.readonly'];
  sampleClient
    .authenticate(scopes)
    .then(runSample)
    .catch(console.error);
}

module.exports = {
  runSample,
  client: sampleClient.oAuth2Client,
};

这个修改怎么样?

从您的示例脚本中,我们发现您正在使用 googleapis。所以在这次修改中,我也使用了googleapis。 Drive 中的图像文件在Drive API 中通过files.copy 方法转换为Google Document with OCR。下面的修改假设有以下几点。

  1. 您正在 Node.js 中使用 googleapis
  2. 当您运行您的脚本时,您已经通过驱动器API检索了文件列表。
    • 这表明您的脚本中的 drive 也可用于 files.copy 方法。

备注:

  • 如果您还没有使用过 Drive API,请查看 the quickstart。 (版本 3)。

确认点:

在你运行脚本之前,请确认以下几点。

  • 为了使用files.copy方法,请在list.js.
  • 中的if语句的范围内包含https://www.googleapis.com/auth/drive

修改后的脚本 1(通过提供 files() id 来转换 Google 具有 OCR 的文档:

本次修改中,修改了runSample()

function runSample()
{
    // Please set the file(s) IDs of sample images in Google Drive.
    const files = [
        "### fileId1 ###",
        "### fileId2 ###",
        "### fileId3 ###", , ,
    ];

    // takes each file and convert them to Google Docs format
    files.forEach((id) =>
    {
        const params = {
            fileId: id,
            resource:
            {
                mimeType: 'application/vnd.google-apps.document',
                parents: ['### folderId ###'], // If you want to put the converted files in a specific folder, please use this.
            },
            fields: 'id',
        };

        // Convert after processes here
        // Here we copy the IDs 
        drive.files.copy(params, (err, res) =>
        {
            if (err)
            {
                console.error(err);
                return;
            }
            console.log(res.data.id);
        });
    });
}

注:

  • 你的文件(图片)被上面的脚本转换为Google文档,结果(Google文档)似乎与你的样本(在你的问题中)相同。但我不确定这是否是您想要的质量,请见谅。

参考文献:

修改脚本 2(通过单个文件夹转换 Google 具有 OCR 的文档并仅选择图像:

  • 您想通过从特定文件夹中检索文件(图像)将其转换为 Google 文档。
  • 您想检索 image/pngimage/jpegimage/tiff 的文件。

示例代码语法:

const folderId = "### folderId ###"; // Please set the folder ID including the images.
drive.files.list(
{
    pageSize: 1000,
    q: `'${folderId}' in parents and (mimeType='image/png' or mimeType='image/jpeg' or mimeType='image/tiff')`,
    fields: 'files(id)',
}, (err, res) =>
{
    if (err)
    {
        console.error(err);
        return;
    }
    const files = res.data.files;
    files.forEach((file) =>
    {
        console.log(file.id);

        // Please put above script of the files.forEach method by modifying ``id`` to ``file.id``.

    });
});

在接下来的修改中,修改了整个 runSample()

function runSample()
{
    // Put the folder ID including files you want to convert.
    const folderId = "### folderId ###";

    // Retrieve file list.
    drive.files.list(
    {
        pageSize: 1000,
        q: `'${folderId}' in parents and (mimeType='image/png' or mimeType='image/jpeg' or mimeType='image/tiff')`,
        fields: 'files(id)',
    }, (err, res) =>
    {
        if (err)
        {
            console.error(err);
            return;
        }
        const files = res.data.files;

        // Retrieve each file from the retrieved file list.
        files.forEach((file) =>
        {
            const params = {
                fileId: file.id,
                resource:
                {
                    mimeType: 'application/vnd.google-apps.document',
                    parents: ['### folderId ###'],
                },
                fields: 'id',
            };

            // Convert a file
            drive.files.copy(params, (err, res) =>
            {
                if (err)
                {
                    console.error(err);
                    return;
                }
                console.log(res.data.id);
            });
        });
    });
}

参考文献: