Skip to content

Instantly share code, notes, and snippets.

Created March 14, 2016 11:54
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Convert complete excel workbook to SQLite database (All Sheets)
This code uses the openpyxl package for playing around with excel using Python code
to convert complete excel workbook (all sheets) to an SQLite database
The code assumes that the first row of every sheet is the column name
Every sheet is stored in a separate table
The sheet name is assigned as the table name for every sheet
import sqlite3
import openpyxl
from openpyxl import load_workbook
import re
def slugify(text, lower=1):
if lower == 1:
text = text.strip().lower()
text = re.sub(r'[^\w _-]+', '', text)
text = re.sub(r'[- ]+', '_', text)
return text
#Replace with a database name
con = sqlite3.connect('test.db')
#replace with the complete path to youe excel workbook
wb = load_workbook(filename=r'abc.xlsx')
sheets = wb.get_sheet_names()
for sheet in sheets:
ws = wb[sheet]
columns= []
query = 'CREATE TABLE ' + str(slugify(sheet)) + '(ID INTEGER PRIMARY KEY AUTOINCREMENT'
for row in ws.rows[0]:
query += ', ' + slugify(row.value) + ' TEXT'
query += ');'
tup = []
for i, rows in enumerate(ws):
tuprow = []
if i == 0:
for row in rows:
tuprow.append(unicode(row.value).strip()) if unicode(row.value).strip() != 'None' else tuprow.append('')
insQuery1 = 'INSERT INTO ' + str(slugify(sheet)) + '('
insQuery2 = ''
for col in columns:
insQuery1 += col + ', '
insQuery2 += '?, '
insQuery1 = insQuery1[:-2] + ') VALUES('
insQuery2 = insQuery2[:-2] + ')'
insQuery = insQuery1 + insQuery2
con.executemany(insQuery, tup)
Copy link

For python3 you will need to replace unicode with str, and the line
for row in ws.rows[0]:
for row in next(ws.rows):

Copy link

Thanks for the code. It was very instructive in creating a larger project.

Copy link

Mglt-b commented Jun 23, 2020

Hello, thank for code

How can get only 5 first columns ?

Or columns with an exactly name ?

And if columns name are in line 2 ?


Copy link

fweller commented Sep 8, 2021

This code is extremely useful. Thank you for sharing it.

Copy link

ekjr commented Sep 20, 2021

for python3 change unicode to str.

Copy link


Copy link

Great project , and thanks
what if we need it dynamic by uploading excel and the code convert it database
Best regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment