ホーム 暮らし Extracting tables from PDFs with tabula.read_pdf

Extracting tables from PDFs with tabula.read_pdf

三浦 拓海 三浦 拓海 • 更新: • 公開: • 読了約 7 分
目次
n16c8f2e748c6

I had a hard time extracting tables from PDFs using tabula.read_pdf, so I am summarizing it here while I remember.

What is tabula.read_pdf?

It is a Python module that allows you to extract tables from PDF files. While there are other modules for reading PDFs, tabula seems to be specialized for table extraction (it extracts them in Dataframe format). However, since it is developed in Java, you need to have Java installed.

Environment

OS: Windows 10

How to use

Install tabula
Open the command prompt and install tabula.

$ pip install tabula-py

Install Java
Next, install Java from the official website.

https://www.oracle.com/java/technologies/downloads/#java11-windows

There are several types, but if you are using Windows, please download the "x64 MSI Installer" at the bottom. The difference from the "x64 Installer" is just the file extension.

.msi uses the Windows Installer, so if you are not on Windows, please download the "x64 MSI Installer". (Windows Installer is processed automatically, so Windows users do not need to worry about anything.) msi stands for "Microsoft Windows Installer".

Next, launch the installer and install it. Please double-click an icon that looks like this.

The necessary items are checked by default, so please just click "Next" to complete the installation.

Setting environment variables

You cannot use tabula just by installing Java. You need to set the environment variables. Open "Edit the system environment variables" from Settings.

Next, click "Environment Variables"

New

User variable settings

The variable name is "JAVA_HOME". The variable value, if using the default installation location, is the path "C:\Program Files\Java\jdk-21" (the 21 part varies depending on the version).

Edit Path

Go to Path, Edit, New, and enter "%JAVA_HOME%\bin". Close everything with OK.

Restart your PC. Java will then become available. I forgot to restart my PC and wasted time.

How to use tabula.read_pdf
import pandas as pd
import tabula

data = tabula.read_pdf("PDFのパス", lattice=True, pages='all', pandas_options='header':None)
print(data)

Load a PDF using tabula.read_pdf("PDF path"). I have added a few options, but please remove them if they are unnecessary.

If there are multiple tables

If there are multiple tables in the PDF, it will extract them as a list. For example, like this.

List[0] -> First table (dataframe type)
List[1] -> Second table (dataframe type)

Even PDFs that are difficult to manage as data can have their tables extracted this way. After that, you can do whatever you like with them.


同じカテゴリの記事

三浦 拓海
コンテンツディレクター 三浦 拓海
プロフィールを見る →

映画と連続ドラマの公開情報を追う担当。配給会社のプレスシートと公式サイトの発表日を確認したうえで執筆します。

おすすめの関連記事