pythonでPDFからテキストを抽出してみる(PyPDF2, pdfminer.six)

2021.12.08

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

pdfのデータからepubを作成することを調べていて、pythonのライブラリで比較的簡単にpdfからテキストは抽出できる とアドバイスをもらったので実際にやってみようと思います。

この2つがメジャーなようです。

PyPDF2は日本語に対応しておらず、pdfminer.sixは日本語もOKなようですね。

PyPDF2

インストール

pipを使ってインストールできます。

pip install PyPDF2

読み込んでみる

2019-11-Chaos_Engineering_Whitepaper のpdfを使用してみます。、

ローカルPCにダウンロードし、以下のコードを実行してみます。

from PyPDF2 import PdfFileReader

with open("2019-11-Chaos_Engineering_Whitepaper.pdf", "rb") as input:
    reader = PdfFileReader(input)

    # pdfの総ページ数は?
    print("2019-11-Chaos_Engineering_Whitepaper.pdf.pdf has %d pages.\n" % reader.getNumPages())

        # 指定のページのデータを読み込む
    page = reader.getPage(0)

    # 読み込んだページのテキストを抽出
    print(page.extractText())

1ページ目:

このページのテキストを読み込むと、

 Chaos Engineering: 
Finding Failures Before 
They Become Outages

と出力されました。

正しく読み込めてますね。

2ページ目:

このページはどうでしょう。

page = reader.getPage(1)

に変更して実行してみると、

Chaos Engineering: Breaking Your Systems for Fun and ProÞt
iane Glazman will never ßy British Airways (BA) 
again. Glazman and her husband were among 
the 75,000 people a
!
ected by the three-day BA 
system failure summer 2017. On their way from San 
Francisco to their sonÕs college graduation in Edinburgh, 
they were stranded in LondonÑwithout their luggageÑat 
the beginning of what was to be the three-week dream 
tour of Scotland. 
"
ÒListening to the excuses was 
frustrating because nothing explained why BA was so 
unprepared for such a catastrophic failure,Ó says 
Glazman. BA lost an estimated $135 million
 due to that outage. 
The culprit turned out to be a faulty uninterruptable 
power supply device (UPS)Ñthe corporate cousin to the 
$10 gadget you can Þnd in your corner Radio Shack. And 
that loss Þgure doesnÕt count the forever-gone trust of 
customers like Glazman, who will look elsewhere for 
transatlantic ßights next time she travels.  
BA of course isnÕt alone for having su
!
ered Þnancially 
for having its systems down. There were also 
United 
Airlines
 (200 ßights delayed for 2.5 hours, thousands of 
passengers stranded or missed connections), 
Starbucks
 (couldnÕt accept any payments but cash in 
a!
ected stores), 
Facebook
 (millions of users o
#ine and 
tens of millions of ads not served during the 2.5 hours of 
downtime), and 
WhatsApp
 (600 million users a
!
ected, 5 
billion messages lost). And when 
Amazon S3 went down
 in March 2017, it collectively 
cost Amazon's customers 
$150 million
.  1D

特殊なレイアウトだとうまく読み込めませんでした。 読み取れない文字もありますね。

Profit が ProÞt と表示された

3ページ目

こちらはどうでしょう。画像付きです。

page = reader.getPage(2)

に変更して実行してみると、

Chaos Engineering: Breaking Your Systems for Fun and ProÞt
In fact, 2017 was a banner year for systems outagesÑ
and for the cost of them. 
The 
2017 ITIC Cost of Downtime survey
 Þnds that 98% 
of organizations say a single hour of downtime costs 
more than $100,000. More than eight in 10 companies 
indicated that 60 minutes of downtime costs their 
business more $300,000. And a record one-third of 
enterprises report that one hour of downtime costs 
their Þrms $1 million to more than $5 million (see Figure 
1). The average cost of a single hour of unplanned 
downtime has risen by 25% to 30% since 2008 when 
ITIC Þrst began tracking these Þgures. 
 2Suffered major outages in 2017

テキストはまずまずの制度で読み込みはされていますが、2ページ目と同じく読み取れない文字もあったようです。

pdfminer.six

インストール

こちらもpipでインストールできます。

pip install pdfminer.six

読み込んでみる

総務省のデジタル・ディバイド解消に向けた技術等研究開発 というファイルを利用させてもらいます。

ローカルPCにダウンロードし、pdfminer.sixに同封されているテキスト抽出のスクリプトを実行します

pdf2txt.py

python pdf2txt.py 000667876.pdf

結果

抽出できませんでしたっ!!!

レイアウトが特殊なのでしょうか。画像をPDFにしただけだからなのでしょうか。

もっと単純なpdfで試してみます。

JITCO日本語教材ひろばから教材サンプルをお借りします。

「みどり」について(日本語版)

python pdf2txt.py aboutmidori-jp.pdf

を実行すると、

今度は抽出できました。

抽出結果の全容はこちらに記載しています。

こちらも精度は高いと思いました。

もちろん、英語のpdfもテキスト抽出が可能なので、PyPDF2で使ったpdfも試してみます。

python pdf2txt.py 2019-11-Chaos_Engineering_Whitepaper.pdf

を実行すると、

こちらも抽出できました。PyPDF2で読み込めなかった文字もこちらは読み込めたっぽいです。

抽出結果の全容はこちらに記載しています。

※ ページ単位で抽出するには -p オプションをつけて実行します。

python pdf2txt.py -p1 2019-11-Chaos_Engineering_Whitepaper.pdf

最後に

PyPDF2はなかなかの精度で英数字のみのpdfのテキストを抽出してくれました。 特殊なレイアウトだと人の手によって抽出されたテキストを校正する必要が出てきますね。 pdfminer.sixもPyPDF2と同様に精度は良く、日本語の抽出も可能という点が大きいです。 特殊なレイアウトだと読み込めませんでしたので、今度は画像として扱って抽出できないか試してみたいと思います。