gpt4 book ai didi

google-apps-script - 使用 Google App Script 来自 google drive 的 OCR 图像

转载 作者:行者123 更新时间:2023-12-05 05:16:07 26 4
gpt4 key购买 nike

我已经实现了以下脚本来使用图像 URL 对单个和多个图像进行 OCR。

function doOCRALL() {
var selected = SpreadsheetApp.getActiveSheet().getActiveRange().getValues().length;
for (var i = 0; i < selected; i++) {
var activeCol = SpreadsheetApp.getActiveSheet().getActiveCell().getColumn();
var activeRow = SpreadsheetApp.getActiveSheet().getActiveCell().getRow();
var valueURL = SpreadsheetApp.getActiveSheet().getRange(activeRow + i, activeCol).getValue();

var image = UrlFetchApp.fetch(valueURL).getBlob();

var file = {
title: 'OCR File',
mimeType: 'image/png'
};

// OCR is supported for PDF and image formats
file = Drive.Files.insert(file, image, {ocr: true});
var doc = DocumentApp.openByUrl(file.embedLink);
var body = doc.getBody().getText();
//Get link Doc that Generated
SpreadsheetApp.getActiveSheet().getRange(activeRow + i, activeCol + 2).setValue(file.embedLink);
//Get Content of Doc that Generated
SpreadsheetApp.getActiveSheet().getRange(activeRow + i, activeCol + 1).setValue(body);

}
}


function doOCR() {
//
var activeCol = SpreadsheetApp.getActiveSheet().getActiveCell().getColumn();
var activeRow = SpreadsheetApp.getActiveSheet().getActiveCell().getRow();

var valueURL = SpreadsheetApp.getActiveSheet().getRange(activeRow, activeCol).getValue();

var image = UrlFetchApp.fetch(valueURL).getBlob();

var file = {
title: 'OCR File',
mimeType: 'image/png'
};

// OCR is supported for PDF and image formats
file = Drive.Files.insert(file, image, {ocr: true});
var doc = DocumentApp.openByUrl(file.embedLink);
var body = doc.getBody().getText();


// Print the Google Document URL in the console
Logger.log("body: %s", body);
Logger.log("File URL: %s", file.embedLink);
//Get link Doc that Generated
SpreadsheetApp.getActiveSheet().getRange(activeRow, activeCol + 2).setValue(file.embedLink);
//Get Content of Doc that Generated
SpreadsheetApp.getActiveSheet().getRange(activeRow, activeCol + 1).setValue(body);
}



function onOpen() {
var ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu('OCR Tools')
.addItem('Extract Cell', 'doOCR')
.addItem('Extract All Cell', 'doOCRALL')
.addSeparator()
.addSubMenu(ui.createMenu('About US')
.addItem('Infomation', 'menuItem2'))
.addToUi();
}

function menuItem2() {
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.alert('AIO Team');
}

当我为任何图像提供图像 URL 时,它就起作用了。但是,如果我在我的驱动器上上传相同的图像,然后提供来自驱动器的图像 URL,它只会给我“登录主菜单”。对于其他驱动器图像,它提供相同的文本。提前致谢。

最佳答案

如果内容已在云端硬盘中,您无需获取指向它的链接 - 只需提供文件 ID(您可以从指向它的链接中获取)。

获得文件 ID 后,您可以简单地复制它,并使用最佳参数来激活 OCR。当然,完整的选项列表可在 Drive REST API 页面上找到:https://developers.google.com/drive/api/v2/reference/files/copy#parameters我鼓励您也阅读有关最佳实践的信息,例如 fields 规范(这是最新驱动 API 版本的要求)。

此函数采用您从某处获得的输入驱动器文件 ID 和一个 truth-y 值来设置“使用 OCR”选项。明显的假设是您有权限,id 有效,您已经在云控制台中启用了高级服务和 Drive API 等。

function getIdOfCopyOfDriveFile(fileId, useOcr) {
const options = {
fields: "choose the metadata fields to return in the response e.g. 'id,title,parents'"
};
const existingMetaData = Drive.Files.get(fileId, options);

options.ocr = !!useOcr;
existingMetaData.title += " (copied with" + (options.ocr ? " " : "out ") + "ocr)";
// We could do other modifications of fields we requested before
// copying, like changing the parents array to move the new file.
const newFileMetaData = Drive.Files.copy(existingMetaData, fileId, options);
return newFileMetaData.id;
}

关于google-apps-script - 使用 Google App Script 来自 google drive 的 OCR 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50840148/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com