【Python】PyPDF2を使ってPDFからテキスト文字を読み取り・抽出する

2020.05.09 /

【Python】PyPDF2を使ってPDFからテキスト文字を読み取り・抽出する

PythonのPDF操作ができるモジュールの1つにPyPDF2というモジュールが存在します。

PyPDF2を使った、PDFからテキスト内容を読み取る方法について解説します。

バイナリファイルのPDFは扱いが難しいのですが、PyPDF2を使えば簡単にテキスト内容を読み取れるんです!!

PyPDF2モジュールのインストール・インポート

PyPDF2モジュールを使うためにはまずモジュールをインストールする必要があります。

下記コマンドをコマンドラインから実行します。

pip install PyPDF2

問題なくインストールができているかインタラクティブシェルに以下を入力してエラーにならないか確認してください。

>>> import PyPDF2

エラーが表示されなければ問題なくインストールができています。

.pyファイルとPDFの準備

PDFからテキストを読み取り・抽出するプログラムを記入するpyファイルを作成します。
ここではpypdf2.pyというファイル名とします。

次に読み取るPDFをpypdf2.pyと同じ階層に保存します。

ここではStatistical Handbook of Japan 2018からダウンロードしたPDFをtest.pdfと名前を変更して使用します。
テキストを読み取るのは4ページ目の以下PDFとします。

テストpdf

PDFからテキストを読み取り・抽出する

以下ソースコードをpypdf2.pyに記入します。

import PyPDF2
file = open('test.pdf', 'rb')
reader = PyPDF2.PdfFileReader(file)
print(reader.numPages)
page = reader.getPage(3)
text = page.extractText()
print(text)

まずPyPDF2モジュールを使用するためにPyPDF2をプログラムの先頭でインポートします。

import PyPDF2

次に対象のPDFをopen()に渡し、バイナリモードで開いてfileに格納します。

file = open('test.pdf', 'rb')

このfileをPdfFileReader()に渡して、PDFを読み込みます。
生成されたPdfFileReaderオブジェクトはreaderに格納します。

reader = PyPDF2.PdfFileReader(file)

読み込んだPDFの総ページ数を知りたい場合は、numPages属性を使用します。

reader.numPages

PDFのページからテキストを抽出するためにはPdfFileReaderオブジェクトからPageオブジェクトを取得する必要があります。

PdfFileReaderからPageオブジェクトを取得するにはgetPageメソッドを使用します。

page = reader.getPage(3)

上記サンプルコードではPDFの4ページ目を指定しています。ページのカウントは0から始まることに注意してください。

そのため先頭ページを指定したい場合はgetPage(0)としてください。

PageオブジェクトからPDFのテキストを抽出するにはextractTextメソッドを使います。

text = page.extractText()

サンプルコードを実行すると以下の結果を得ることができます。

210
 Notes for Users
  1. The present issue
basically
 contains statisti
cs that became available by
May
 31, 2018. 2. Unless otherwise indicated, "year" refers to the calendar year and "fiscal
year"
refers to the 12 months beginning April 1 of the year stated.
 3. Metric units are used in all tables and
figures
 in which the data are measured in
weight, volume, length or area.
Refer to Appendix 2 for conversion factors.
 4. Unless otherwise indicat
ed,
amounts shown are in
 Japanese yen.
Refer to
Appendix 3 for
exchange rates
of J
PY
per
 U.S. dollar.
 5. Statistical figures may not add up to the totals due to rounding.
 6.  The following symbols are used in the tables:
 >…>…>…
 Data not available
 >+ Magnitude
 zero or figures not applicable
 0 or 0.0
  Less than half of unit employed
 >! Marked break in series
 >( Provisional or estimate
 7. Data relating to "China" generally exclude those for Hong Kong SAR, Macao
SAR and Taiwan.
 8. All contents of the present issue, including tables,
figures
, and maps, are also
available on the website
: https://www.stat.go.jp/english/data/handbook/index.htm
l 9. When any contents of the present issue are to be quoted or copied in other media
(print or
 electronic), the title is to be referred to as follows:
     Source: Statistical Handbook of Japan
2018, Statistics Bureau, Ministry of
Internal Af
fairs and Communications, Japan
. 10. "Statistics Bureau, MIC" in the tables and figures is an abbreviation of
 "Statistics
Bureau, Ministry of Internal Affairs and Communications, Japan".

上記結果では、最初にPDFの総ページ数を出力し、その後にPDFから抽出したテキストを表示しています。

いくらか異なっているテキストはありますが、この程度は共用範囲でしょう。

まとめ

「【Python】PyPDF2を使ってPDFからテキスト文字を読み取り・抽出する」はいかがでしたか?

このPyPDF2モジュールを応用することで様々なアプリケーションを作成することができます。

ぜひPyPDF2を使っていろんなアプリを作ってみてください。