Google Drive内の画像ファイルをGoogleドキュメントで開くと、画像内のテキストを抽出することができます。PDF や写真のファイルをテキストに変換する
それをスクリプトで実現する方法を調べて試したので書き残しておきます。
事前準備
コード.gs
function get_ocr() {
var id = "画像ファイルのID";
var file = DriveApp.getFileById(id);
var blob = file.getBlob();
var name = file.getName();
var file = {
title: name,
mimeType: 'image/png'
};
var option = {
ocr: true
}
file = Drive.Files.insert(file, blob, option);
var doc = DocumentApp.openByUrl(file.embedLink);
var text = doc.getBody().getText();
var docUrl = "https://docs.google.com/document/d/" + doc.getId() + "/edit";
Logger.log(docUrl);
Logger.log(text);
}
|
試してみる
スクリプトエディタのスクリーンショットを撮ってDriveにアップロードして、そのファイルを対象にやってみます。
Logger.log(docUrl)に出力したURLにアクセスすると
以下のようにテキストに変換されたドキュメントが開きます
function get_ocr() {
var id = " 771)LOID"; var file = DriveApp.getFileById(id); var blob = file.getBlob();
var file = {
title: "file_name", mimeType: 'image/png
var option = {
ocr: true
file = Drive. Files.insert(file, blob, option); var doc - DocumentApp.openByUrl(file.embedlink); var body = doc.getBody().getText():
var docUrl = "https://docs.google.com/document/d/" + doc.getId() + "/edit"; Logger.log(docUrl);
テキストは
var text = doc.getBody().getText()
で取得できます
参考
Files: insert
https://developers.google.com/drive/api/v2/reference/files/insert
https://stackoverflow.com/questions/50840148/ocr-images-from-google-drive-using-google-app-script
https://productforums.google.com/forum/#!topic/docs/wMlGv2l3urs
PDF や写真のファイルをテキストに変換する
https://support.google.com/drive/answer/176692?co=GENIE.Platform%3DDesktop&hl=ja
var text = doc.getBody().getText()
で取得できます
参考
Files: insert
https://developers.google.com/drive/api/v2/reference/files/insert
https://stackoverflow.com/questions/50840148/ocr-images-from-google-drive-using-google-app-script
https://productforums.google.com/forum/#!topic/docs/wMlGv2l3urs
PDF や写真のファイルをテキストに変換する
https://support.google.com/drive/answer/176692?co=GENIE.Platform%3DDesktop&hl=ja