我使用Google云端硬盘及其使用Google Docs工具将其转换为OCR word文件(.docx)。因为word文件也保留了格式。我有很多图片并将其上传到云端硬盘并将其逐个转换为可编辑,因为PDF转换不起作用。
在这段时间里,我想耐心等待完成一个转换过程。之后我开始下一次转换,这很费时间。
我使用了Google OCR API。但它不保留格式,如粗体,对齐等。
那么,有没有办法使用REST API自动化这个过程?
UPDATE
我在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,
};
这个修改怎么样?
从您的示例脚本中,发现您使用的是googleapis
。所以在这个修改中,我也使用了googleapis
。 Drive中的图像文件通过Drive API中的files.copy
方法转换为带有OCR的Google文档。以下修改假设以下几点。
googleapis
。drive
也可以用于files.copy
方法。在运行脚本之前,请确认以下几点。
files.copy
方法,请将https://www.googleapis.com/auth/drive
包含在if
的list.js
语句中。在这个修改中,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);
});
});
}
image/png
,image/jpeg
和image/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);
});
});
});
}