Willem Hoek

How-to Import an Excel file into SQLite and back to Excel again

Sep 17, 2024

YouTube demo:

YouTube

Introduction

What we want to do

Why?

Skillset required

Tools used

Code

Load Excel file in SQLite


import pandas as pd
import sqlite3

# if using Windows
db = r'C:\data\sap\sqlite.db'                   # SQLite database file
file_in = r'C:\data\sap\mara.xlsx'              # Excel data file

# if using Linux or macOS
db = r'/home/willem/data/sap/sqlite.db'         # SQLite database file
file_in = r'/home/willem/data/sap/mara.xlsx'    # Excel data file

# read Excel file into a dataframe
df = pd.read_excel(file_in, na_filter=False)

# create database table from dataframe
table = "mara"
conn = sqlite3.connect(db)
df.to_sql(table, conn, if_exists='replace', index=False)
conn.close()

Export SQLite data to Excel file


import pandas as pd
import sqlite3

# if using Windows
db = r'C:\data\sap\sqlite.db'                       # SQLite database file
file_in = r'C:\data\sap\mara_out.xlsx'              # Excel data file

# if using Linux or macOS
db = r'/home/willem/data/sap/sqlite.db'             # SQLite database file
file_out = r'/home/willem/data/sap/mara_out.xlsx'   # Excel data file

sql = "select *, length(maktx) as maktx_len from mara"

# get data from SQLite in Pandas dataframe
conn = sqlite3.connect(db)
df = pd.read_sql_query(sql, conn)
conn.close()

# export dataframe to Excel spreadsheet
df.to_excel(file_out, index=False)

Export SQLite data to Excel file - CUSTOMIZE the Excel file

If you require more complex Excel files to be created, you may want to look at XlsxWriter. XlsxWriter is a Python module that can be used to write text, numbers, formulas and hyperlinks to multiple worksheets in an Excel 2007+ XLSX file.

You may need to install the XlsxWriter library first with pip install xlsxwriter.

import pandas as pd
import sqlite3
import xlsxwriter

db = r'/home/willem/data/sap/sqlite.db'
file_out = r'/home/willem/data/sap/mara-snazzy.xlsx'
sql = "select matnr, maktx from mara "

# Get data from SQLite in Pandas dataframe
con = sqlite3.connect(db)
df = pd.read_sql_query(sql, con)
con.close()

# Create Excel file
row = 1
col = 0
workbook = xlsxwriter.Workbook(file_out)
header = workbook.add_format({'bold': True, 'bg_color': '#D8E4BC'})
worksheet = workbook.add_worksheet('Summary')
worksheet.write(row, col, 'Material', header)
worksheet.write(row, col + 1, 'Description', header)
row += 1
for index, dat in df.iterrows():
    worksheet.write(row + index, col, dat['matnr'])
    worksheet.write(row + index, col + 1, dat['maktx'])
worksheet.set_column('B:B', 50)
worksheet.set_zoom(200)
workbook.close()

References and further reading

[1] Why Business Analysts should know and use SQL
https://sap123.com/a/759

[2] SQLite
https://www.sqlite.org/

[3] Python Anaconda distribution
https://www.anaconda.com/download

[4] Python pandas library
https://pandas.pydata.org/

[5] XlsxWriter - Creating Excel files with Python and XlsxWriter
https://xlsxwriter.readthedocs.io/

Similar Posts on this website

Solving the Jane Street puzzle of December 2022

Why I created Scrumdog - a program to download Jira Issues to a local database

Jane Street puzzle Feb 2021 SOLVED! OCaml to the rescue

Solving the Jane Street puzzle of Dec 2020 - Backtracking with OCaml

Automate your Jira reporting with Python and Excel