如何自动化Google Drive Docs OCR工具?

问题描述 投票:1回答:1

我使用Google云端硬盘及其使用Google Docs工具将其转换为OCR word文件(.docx)。因为word文件也保留了格式。我有很多图片并将其上传到云端硬盘并将其逐个转换为可编辑,因为PDF转换不起作用。

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

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

那么,有没有办法使用REST API自动化这个过程?

UPDATE

  1. 将图像上传到Google云端硬盘link
  2. Google云端硬盘link中图片的右键单击上下文菜单
  3. Google文档位于“打开方式”link的上下文菜单中
  4. 转换过程后,OCR(检测到自动语言)link
  5. 最后谷歌文件和图像link

我在GitHub上尝试了googleapis,我选择了驱动器样本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,
};
node.js api google-docs-api
1个回答
1
投票

这个修改怎么样?

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

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

Notes :

  • 如果您尚未使用Drive API,请检查the quickstart。 (第3版)。

Confirmation point:

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

  • 为了使用files.copy方法,请将https://www.googleapis.com/auth/drive包含在iflist.js语句中。

Modified script 1 (to convert Google Docs with OCR by giving files() id:

在这个修改中,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);
        });
    });
}

Note:

  • 您的文件(图片)按上述脚本转换为Google文档,结果(Google文档)似乎与您的示例相同(在您的问题中)。但我不确定这是否是你想要的品质,请道歉。

References:

Modified script 2 (to convert Google Docs with OCR by single folder and selects only images:

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

Sample code syntax:

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);
            });
        });
    });
}

References:

© www.soinside.com 2019 - 2024. All rights reserved.