Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active October 7, 2023 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/422c6125781e045c73dab3d5373d8836 to your computer and use it in GitHub Desktop.
Save code-boxx/422c6125781e045c73dab3d5373d8836 to your computer and use it in GitHub Desktop.
Python Import Excel Into Database

PYTHON IMPORT EXCEL TO DATABASE

https://code-boxx.com/import-excel-database-python/

NOTES

  1. GIST does not allow Excel files... Convert S2_users.csv to S2_users.xlsx on your own.
  2. Run unpack.bat (Windows) unpack.sh (Linux/Mac). This will automatically:
    • Create a virtual environment - virtualenv venv.
    • Activate the virtual environment - venv\scripts\activate (Windows) venv/bin/activate (Mac/Linux)
    • Install required modules - pip install openpyxl
    • Create the dummy database - python S1B_create.py
    • Run the import example - python S3_import.py

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

-- (A) USERS TABLE
CREATE TABLE users (
uid INTEGER,
name TEXT NOT NULL,
email TEXT NOT NULL,
tel TEXT NOT NULL,
PRIMARY KEY("uid" AUTOINCREMENT)
);
# (A) LOAD PACKAGES
import sqlite3, os
from sqlite3 import Error
# (B) DATABASE + SQL FILE
DBFILE = "users.db"
SQLFILE = "S1A_users.sql"
# (C) DELETE OLD DATABASE IF EXIST
if os.path.exists(DBFILE):
os.remove(DBFILE)
# (D) IMPORT SQL
conn = sqlite3.connect(DBFILE)
with open(SQLFILE) as f:
conn.executescript(f.read())
conn.commit()
conn.close()
print("Database created!")
Jo Doe jo@doe.com 465785
Joa Doe joa@doe.com 123456
Job Doe job@doe.com 234567
Joe Doe joe@doe.com 345678
Jog Doe jog@doe.com 578456
Joh Doe joh@doe.com 378945
Joi Doe joi@doe.com 456789
Jon Doe jon@doe.com 987654
Jor Doe jor@doe.com 754642
Joy Doe joy@doe.com 124578
# (A) INIT
# (A1) LOAD MODULES
import sqlite3, os
from sqlite3 import Error
from openpyxl import load_workbook
# (A2) SETTINGS
DBFILE = "users.db"
# (B) OPEN DATABASE & EXCEL FILE
conn = sqlite3.connect(DBFILE)
cursor = conn.cursor()
book = load_workbook("S2_users.xlsx")
sheet = book.active
# (C) IMPORT ROWS & COLUMNS
for row in range(1, sheet.max_row + 1):
sql = "INSERT INTO `users` (`name`, `email`, `tel`) VALUES "
sql += f"('{sheet.cell(row, 1).value}', '{sheet.cell(row, 2).value}', '{sheet.cell(row, 3).value}')"
cursor.execute(sql)
print(sql)
conn.commit()
conn.close()
virtualenv venv
call venv\Scripts\activate
pip install openpyxl
python S1B_create.py
python S3_import.py
virtualenv venv
source "venv/bin/activate"
pip install openpyxl
python S1B_create.py
python S3_import.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment