Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active October 2, 2023 07:06
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/338a79fac955a06f99257b9d6b66f76a to your computer and use it in GitHub Desktop.
Save code-boxx/338a79fac955a06f99257b9d6b66f76a to your computer and use it in GitHub Desktop.
Python Export Database To Excel

PYTHON EXPORT DATABASE TO EXCEL

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

NOTES

  1. 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 PYExcel pip install openpyxl
    • Create the database python S1B_create.py
    • Run the demo python S2_export.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)
);
-- (B) DUMMY DATA
INSERT INTO "users" VALUES
(1,'Jo Doe','jo@doe.com','465785'),
(2,'Joa Doe','joa@doe.com','123456'),
(3,'Job Doe','job@doe.com','234567'),
(4,'Joe Doe','joe@doe.com','345678'),
(5,'Jog Doe','jog@doe.com','578456'),
(6,'Joh Doe','joh@doe.com','378945'),
(7,'Joi Doe','joi@doe.com','456789'),
(8,'Jon Doe','jon@doe.com','987654'),
(9,'Jor Doe','jor@doe.com','754642'),
(10,'Joy Doe','joy@doe.com','124578');
# (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!")
# (A) INIT
# (A1) LOAD MODULES
import sqlite3, os, openpyxl
from sqlite3 import Error
# (A2) SETTINGS
DBFILE = "users.db"
# (B) OPEN DATABASE & CREATE EXCEL
conn = sqlite3.connect(DBFILE)
cursor = conn.cursor()
book = openpyxl.Workbook()
sheet = book.active
# (C) EXPORT DATA TO EXCEL
cursor.execute("SELECT * FROM `users`")
results = cursor.fetchall()
i = 0
for row in results:
i += 1
j = 1
for col in row:
cell = sheet.cell(row = i, column = j)
cell.value = col
j += 1
# (D) SAVE EXCEL FILE & CLOSE DB
book.save("demo.xlsx")
conn.close()
virtualenv venv
call venv\Scripts\activate
pip install openpyxl
python S1B_create.py
python S2_export.py
virtualenv venv
source "venv/bin/activate"
pip install openpyxl
python S1B_create.py
python S2_export.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment