extract text from pdf
TRANSCRIPT
![Page 1: Extract text from PDF](https://reader034.vdocuments.net/reader034/viewer/2022051318/58f0dba11a28abc4048b45b3/html5/thumbnails/1.jpg)
AppleScriptでPDFからテキストを抽出する
Piyomaru Software⻑野⾕ 隆昌
http://piyocast.com/as/
2016.9.26 Cocoa勉強会 Ikebukuro発表内容
![Page 2: Extract text from PDF](https://reader034.vdocuments.net/reader034/viewer/2022051318/58f0dba11a28abc4048b45b3/html5/thumbnails/2.jpg)
現在、AppleScriptはCocoa-bridgedな⾔語• OS X 10.10以降、AppleScriptのすべてのランタイム環境で
Cocoa機能の呼び出しがサポートされるようになったMy brand-new AS Books are now on sale!
「AppleScript最新リファレンス」https://piyomarusoft.booth.pm/items/307646
「最新事情がわかるAppleScript 10⼤最新技術」https://piyomarusoft.booth.pm/items/301502
![Page 3: Extract text from PDF](https://reader034.vdocuments.net/reader034/viewer/2022051318/58f0dba11a28abc4048b45b3/html5/thumbnails/3.jpg)
Cocoa-bridgedだが、制約もけっこーあるBlocks構文、protocolは未サポート→ 呼べないFrameworkがたくさんある(T_T)
詳細は、AppleScriptObjC Release Notes、Wikipedia「AppleScript」の項⽬(⾃分が書きました)参照のこと
![Page 4: Extract text from PDF](https://reader034.vdocuments.net/reader034/viewer/2022051318/58f0dba11a28abc4048b45b3/html5/thumbnails/4.jpg)
数千本のAppleScriptのストックから
![Page 5: Extract text from PDF](https://reader034.vdocuments.net/reader034/viewer/2022051318/58f0dba11a28abc4048b45b3/html5/thumbnails/5.jpg)
サードパーティのエディタでオープンASObjC Explorer 4
Cocoaメソッドなどの⼊⼒補完
Cocoaオブジェクトやイベントのログ表⽰できることが特徴
Apple純正のスクリプトエディタでは、これらの機能をサポートしていない
![Page 6: Extract text from PDF](https://reader034.vdocuments.net/reader034/viewer/2022051318/58f0dba11a28abc4048b45b3/html5/thumbnails/6.jpg)
Code:-- Created 2015-08-12 by Shane Stanley, Christopher Stoneuse AppleScript version "2.4"use scripting additionsuse framework "Foundation"use framework "Quartz"
set aFile to POSIX path of (choose file of type {"com.adobe.pdf"})set aTEXT to pdf2Text(aFile)
--PDFの全ページからテキストを抽出するon pdf2Text(thePath)
set theText to current application's NSMutableString's |string|()set anNSURL to current application's |NSURL|'s fileURLWithPath:thePathset theDoc to current application's PDFDocument's alloc()'s initWithURL:anNSURLset theCount to theDoc's pageCount() as integer
repeat with i from 1 to theCountset thePage to (theDoc's pageAtIndex:(i - 1))(theText's appendString:(thePage's |string|()))
end repeat
return theText as text
end pdf2Text
![Page 7: Extract text from PDF](https://reader034.vdocuments.net/reader034/viewer/2022051318/58f0dba11a28abc4048b45b3/html5/thumbnails/7.jpg)
実際にPDFテキスト抽出①PDFサンプル1:⾃分で書いた本
Markdownエディタ「MacDown」やPagesから出⼒したPDF
すべて横書き
複雑なレイアウトはしていない(書籍なので)
→ 問題なくテキスト抽出できた
![Page 8: Extract text from PDF](https://reader034.vdocuments.net/reader034/viewer/2022051318/58f0dba11a28abc4048b45b3/html5/thumbnails/8.jpg)
実際にPDFテキスト抽出②PDFサンプル2:吉川さんがInDesignで作った縦書き⽂章のPDF
Adobe InDesignから出⼒したPDF
すべて縦書き
複雑なレイアウトはしていない
→ 問題なくテキスト抽出できた
![Page 9: Extract text from PDF](https://reader034.vdocuments.net/reader034/viewer/2022051318/58f0dba11a28abc4048b45b3/html5/thumbnails/9.jpg)
PDFDocumentからのテキスト抽出凝ったレイアウトを⾏なっていない「素朴なPDF」からのテキスト抽出は問題ない?
ただし、「本当にすべて抽出できているのか」を確認する必要はある
これまでに、納品書のPDF(国際宅急便業者)からのデータ抽出を⾏い、⾚ペン⽚⼿にデータをチェックしてみたが、部分的に⽋落するケースがあった
→ 最終的に、Adobe IllustratorでPDFをオープンしてテキスト抽出を実施(対象PDFデータが英語のみだったので。なぜかAdobe Illustrator Mac版ではPDFから⽇本語テキストを抽出できない)
過信は禁物。ひととおり既存のデータでチェックして、テキスト抽出もれについて調査しておかなくてはならない