Data Automation & Cleaning Beginner

Convert Multi-Sheet Excel Workbooks (.xlsx) into SQLite Database in Python

Automatically migrate heavy, slow multi-sheet Excel workbooks into a fast, queryable relational SQLite database using Python Pandas and sqlite3.

#pandas#openpyxl

Problem Statement

Excel freezes when workbooks exceed 100,000 rows across multiple tabs. This script reads every sheet in an `.xlsx` file and writes each tab as an indexed SQL table inside a `.db` file.

Formula / Core Logic

Reads sheet_name=None to load a dict of DataFrames, then iterates with df.to_sql(table_name, conn).

Installation

pip install pandas openpyxl
excel-to-sqlite-database-converter-python-script.py
import sqlite3
import pandas as pd
import re

def excel_to_sqlite(excel_path, db_path='financial_store.db'):
    sheets_dict = pd.read_excel(excel_path, sheet_name=None)
    conn = sqlite3.connect(db_path)
    for sheet_name, df in sheets_dict.items():
        clean_table = re.sub(r'[^a-zA-Z0-9_]', '_', sheet_name).lower()
        df.to_sql(clean_table, conn, if_exists='replace', index=False)
        print(f'Saved sheet [{sheet_name}] -> SQL table [{clean_table}] ({len(df)} rows)')
    conn.close()

Get the Complete 100+ Python Quant & Automation Bundle

Instant lifetime access to all production-ready scripts, valuation models, and trading bots ($19).

Buy Full Bundle on Gumroad ($19) →

Frequently Asked Questions

Q: Do I need to install a separate SQLite server?

No! `sqlite3` is built into Python's standard library and stores the entire database in a single portable file.