> ## Content Index
> Fetch the complete content index at: https://datascientyst.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Extract Table from PDF with Python and Pandas
- URL: https://datascientyst.com/extract-table-from-pdf-with-python-pandas/
- Published: 2022-09-30T06:23:11.000Z
- Updated: 2025-02-14T13:10:18.000Z
- Author: John D K
- Tags: Other

In this short tutorial, we'll see how to **extract tables from PDF files with Python and Pandas**.

We will cover two cases of table extraction from PDF:

**(1) Simple table with tabula-py**

```python
from tabula import read_pdf
df_temp = read_pdf('china.pdf')

```

**(2) Table with merged cells**

```python
import pandas as pd
html_tables = pd.read_html(page)

```

Let's cover both examples in more detail as context is important.

Nice video on the topic: [Easily extract tables from websites with pandas and python](https://www.youtube.com/watch?v=OXA%5FZD1gR6A&ref=datascientyst.com)

Notebook: [Scrape wiki tables with pandas and python.ipynb](https://github.com/softhints/python/blob/master/notebooks/Scrape%20wiki%20tables%20with%20pandas%20and%20python.ipynb?ref=datascientyst.com)

## 1: Extract tables from PDF with Python

In this example we will **extract multiple tables from remote PDF file**: [china.pdf](https://github.com/tabulapdf/tabula-java/blob/master/src/test/resources/technology/tabula/china.pdf?ref=datascientyst.com).

We will use library called: [tabula-py](https://pypi.org/project/tabula-py/?ref=datascientyst.com) which can be installed by:

```bash
pip install tabula-py

```

The .pdf file contains 2 table:

- smaller one
- bigger one with merged cells

```python
from tabula import read_pdf

file = 'https://raw.githubusercontent.com/tabulapdf/tabula-java/master/src/test/resources/technology/tabula/china.pdf'

df_temp = read_pdf(file, stream=True)

```

After reading the data we can get a list of DataFrames which contain table data.

Let's check the first one:

|   | FLA Audit Profile    | Unnamed: 0                                        |
| - | -------------------- | ------------------------------------------------- |
| 0 | Country              | China                                             |
| 1 | Factory name         | 01001523B                                         |
| 2 | IEM                  | BVCPS (HK), Shen Zhen Office                      |
| 3 | Date of audit        | May 20-22, 2003                                   |
| 4 | PC(s)                | adidas-Salomon                                    |
| 5 | Number of workers    | 243                                               |
| 6 | Product(s)           | Scarf, cap, gloves, beanies and headbands         |
| 7 | Production processes | Sewing, cutting, packing, embroidery, die-cutting |

Which is the exact match of the first table from the PDF file.

![read-pdf-table-python-tabula](https://datascientyst.com/content/images/2022/09/read-pdf-table-python-tabula.png)

While the second one is a bit weird. The reason is because of the merged cells which are extracted as `NaN` values:

|   | Unnamed: 0                 | Unnamed: 1                    | Unnamed: 2    | Findings           | Unnamed: 3 |
| - | -------------------------- | ----------------------------- | ------------- | ------------------ | ---------- |
| 0 | FLA Code/ Compliance issue | Legal Reference / Country Law | FLA Benchmark | Monitor's Findings | NaN        |
| 1 | 1\. Code Awareness         | NaN                           | NaN           | NaN                | NaN        |
| 2 | 2\. Forced Labor           | NaN                           | NaN           | NaN                | NaN        |
| 3 | 3\. Child Labor            | NaN                           | NaN           | NaN                | NaN        |
| 4 | 4\. Harassment or Abuse    | NaN                           | NaN           | NaN                | NaN        |

![read-pdf-table-python-tabula-merged-cells](https://datascientyst.com/content/images/2022/09/read-pdf-table-python-tabula-merged-cells.png)

How to workaround this problem we will see in the next step.  
Some cells are extracted to multiple rows as we can see from the image:

## 2: Extract tables from PDF - keep format

Often tables in PDF files have:

- strange format
- merged cells
- strange symbols

Most libraries and software are not able to extract them in a reliable way.

To **extract complex table from PDF files with Python and Pandas** we will do:

- download the file (it's possible without download)
- convert the PDF file to HTML
- extract the tables with Pandas

### 2.1 Convert PDF to HTML

First we will download the file from: [china.pdf](https://github.com/tabulapdf/tabula-java/blob/master/src/test/resources/technology/tabula/china.pdf?ref=datascientyst.com).

Then we will convert it to HTML with the library: [pdftotree](https://pypi.org/project/pdftotree/?ref=datascientyst.com).

```python
import pdftotree

page = pdftotree.parse('china.pdf', html_path=None, model_type=None, model_path=None, visualize=False)

```

library can be installed by:

```bash
pip install pdftotree

```

### 2.2 Extract tables with Pandas

Finally we can read all the tables from this page with Pandas:

```python
import pandas as pd
html_tables = pd.read_html(page)
html_tables[1]

```

Which will give us better results in comparison to `tabula-py`

![read-pdf-table-python-pandas-merged-cells](https://datascientyst.com/content/images/2022/09/read-pdf-table-python-pandas-merged-cells.png)

### 2.3 HTMLTableParser

As alternatively to Pandas, we can use the library: [html-table-parser-python3](https://pypi.org/project/html-table-parser-python3/?ref=datascientyst.com) to parse the HTML tables to Python lists.

```python
from html_table_parser.parser import HTMLTableParser

p = HTMLTableParser()
p.feed(page)
print(p.tables[0])

```

it convert the HTML table to Python list:

```
[['', ''], ['Country', 'China'], ['Factory  name', '01001523B'], ['IEM', 'BVCPS  (HK),  Shen  Zhen  Office'], ['Date  of  audit', 'May  20-22,  2003'], ['PC(s)', 'adidas-Salomon'], ['Number  of  workers', '243'], ['Product(s)', 'Scarf,  cap,  gloves,  beanies  and  headbands']]

```

Now we can convert the list to Pandas DataFrame:

```python
import pandas as pd
pd.DataFrame(p.tables[1])

```

To install this library we can do:

```bash
pip install html-table-parser-python3

```

There are two differences to Pandas:

- returns list of values
- instead of NaN values - there are empty strings

## 3\. Python Libraries for extraction from PDF files

Finally let's find a **list of useful Python libraries which can help in PDF parsing and extraction**:

### 3.1 Python PDF parsing

- [tabula-py](https://pypi.org/project/tabula-py/?ref=datascientyst.com) \- Simple wrapper for tabula-java, read tables from PDF into DataFrame  
  - [tabula-py example notebook](https://nbviewer.org/github/chezou/tabula-py/blob/master/examples/tabula%5Fexample.ipynb?ref=datascientyst.com)
- [camelot-py](https://pypi.org/project/camelot-py/?ref=datascientyst.com) \- PDF Table Extraction for Humans
- [pdfminer](https://pypi.org/project/pdfminer/?ref=datascientyst.com) \- PDF parser and analyzer
- [PyPDF2](https://pypi.org/project/PyPDF2/?ref=datascientyst.com) \- A pure-python PDF library capable of splitting, merging, cropping, and transforming PDF files

### 3.2 Parse HTML tables

- [html-table-parser-python3](https://pypi.org/project/html-table-parser-python3/?ref=datascientyst.com) \- parse HTML tables with Python 3 to list of values
- [tablextract](https://pypi.org/project/tablextract/?ref=datascientyst.com) \- extracts the information represented in any HTML table
- [pdftotree](https://pypi.org/project/pdftotree/?ref=datascientyst.com) \- convert PDF into hOCR with text, tables, and figures being recognized and preserved.
- [pandas.read\_html](https://pandas.pydata.org/docs/reference/api/pandas.read%5Fhtml.html?ref=datascientyst.com)
- [html-table-extractor](https://pypi.org/project/html-table-extractor/?ref=datascientyst.com) \- A python library for extracting data from html table
- [py-html-table](https://pypi.org/project/py-html-table/?ref=datascientyst.com) \- Python library to extract data from HTML Tables with rowspan

### 3.3 Example PDF files

Finally you can find example PDF files where you can test table extraction with Python and Pandas:

[tabula test PDF files](https://github.com/tabulapdf/tabula-java/tree/master/src/test/resources/technology/tabula?ref=datascientyst.com)

![](https://datascientyst.com/content/images/2022/09/extract-table-from-pdf-with-python-pandas.png)