LoginSignup
1
0

More than 3 years have passed since last update.

GoogleのOCRを使って書籍をセルフ電子化する①

Last updated at Posted at 2020-12-15

こんにちは。みなさん、英語の書籍を読むことはありますか。

フランス語の読解を先輩とやっていて、日本国内ではそんなに出回っていないテキストを二人で読もう、ということになりました。
著作権的に、いいのかかなり怪しいところだけれど、僕がもっているテキストを二人で読むために、Google Driveで共有することにしました。

画像でもいいんだけれど、なんかコメントとかできた方が便利かなと思って、テキストにしたい。と思ったときに、GoogleのOCRは便利やで〜という記事を見たので、使ってみるとあら便利。画像で保存したファイルを、右クリックして「Documentで開く」とするだけで、OCR(光学文字認識)してくれて文字にしてくれる。

しかも精度もほとんど間違いなし。頑張って手直しできる範囲です。

最初は一つ一つ画像を手でDocumentで開いていましたが、フォントを直したり、一枚のファイルにまとめたりするのに、やはりGASを使った方がいいのでは…?ということになり頑張って作ってみました。

コード.gs
function main() {
  getFolder('myFolder'); // フォルダ名を入れます
}

function getFolder(name) {
  const folder = DriveApp.getFoldersByName(name).next().getFoldersByName('Images').next();
  const images = folder.getFiles();

  while(images.hasNext()) {
    const image = images.next();
    const resource = { title: image.getName(), mimeType: 'image/jpeg' };
    const blob = image.getBlob();

    // 画像ファイルをDocumentで開く
    const doc = Drive.Files.insert(resource, blob, { ocr: true });

    const destinationFolder = DriveApp.getFolderById("********************");
    DriveApp.getFileById(doc.id).moveTo(destinationFolder);
  }
}

function deleteImages() {
  const files = DriveApp.getFolderById("************").getFiles();
  while(files.hasNext()) {
    const file = files.next();
    const id = file.getId();
    const body = DocumentApp.openById(id).getBody();

    // イメージを削除
    // body.getImages()[0].removeFromParent();
  }
}

/* Sets Font size and Font family */
function setTextStyle() {
  const files = DriveApp.getFolderById("************").getFiles();
  while(files.hasNext()) {
    const file = files.next();
    const id = file.getId();
    if(id == "************") {continue;}
    const body = DocumentApp.openById(id).getBody();

    const attributes = {};
    const attr = DocumentApp.Attribute;
    attributes[attr.FONT_FAMILY] = 'Calibri';
    attributes[attr.FONT_SIZE] = 13;
    body.setAttributes(attributes);
  }
}

function collectTextIntoOneFile() {
  const files = DriveApp.getFolderById("************").getFiles();
  const docs = [];
  while(files.hasNext()) {
    let file = files.next();
    let title = file.getName();
    let id = file.getId();
    if (title != "whole_text") { docs.push({ title: title, id: id }); }
  }

  // メモリ上のファイルを名前順に整理
  docs.sort(function(a, b) {
    let titleA = a.title.toUpperCase();
    let titleB = b.title.toUpperCase();

    if( titleA < titleB ) { return -1; }
    if( titleA > titleB ) { return  1; }

    return 0;
  })

  // 実行
  const destination = DocumentApp.openById('************');
  docs.forEach(function(doc) {
    const text = DocumentApp.openById(doc.id).getBody().getText();
    destination.getBody().appendParagraph(text);
  })
}

function props() {
  console.log(PropertiesService.getScriptProperties().getProperty('*****'))
}

Step2. フォントサイズの確認

Step3. Kindleに送信

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0