Extracting Dirty Tables from Multi-Page PDFs into Clean Pandas DataFrames

Extracting financial tables from scanned and vector PDFs is notoriously frustrating. Libraries like tabula-py often miss borders or misalign columns. Here is the exact routine I built using pdfplumber and pandas.

Implementation Code

import pdfplumber
import pandas as pd

def extract_clean_tables(pdf_path):
    all_rows = []
    with pdfplumber.open(pdf_path) as pdf:
        for page in pdf.pages:
            table = page.extract_table()
            if table:
                all_rows.extend(table)
                
    df = pd.DataFrame(all_rows[1:], columns=all_rows[0])
    
    # Strip whitespace and drop fully empty rows
    df = df.applymap(lambda s: s.strip() if isinstance(s, str) else s)
    df.dropna(how="all", inplace=True)
    return df

df = extract_clean_tables("quarterly_report.pdf")
df.to_csv("clean_report.csv", index=False)
print("Extracted shape:", df.shape)

This approach reduced manual spreadsheet data entry time from 4 hours every Monday to under 10 seconds.

Leave a Comment