-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite2excel.py
More file actions
51 lines (38 loc) · 1.63 KB
/
Copy pathsqlite2excel.py
File metadata and controls
51 lines (38 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import sqlite3
import pandas as pd
import argparse
import os
def sqlite_to_excel(sqlite_file, excel_file):
# Connect to the SQLite database
conn = sqlite3.connect(sqlite_file)
cursor = conn.cursor()
# Get a list of all tables in the database
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = cursor.fetchall()
if not tables:
print("No tables found in the database.")
return
# Open an Excel writer
with pd.ExcelWriter(excel_file, engine='openpyxl') as writer:
for (table_name,) in tables:
print(f"Exporting table: {table_name}")
df = pd.read_sql_query(f"SELECT * FROM `{table_name}`", conn)
df.to_excel(writer, sheet_name=table_name[:31], index=False) # Excel sheet names max 31 chars
conn.close()
print(f"Exported {len(tables)} tables to {excel_file}")
def main():
parser = argparse.ArgumentParser(description="Convert a SQLite database to an Excel file with one sheet per table.")
parser.add_argument('--input', '-i', required=True, help='Path to the input SQLite database file')
parser.add_argument('--output', '-o', required=True, help='Path to the output Excel file (.xlsx)')
args = parser.parse_args()
# Validate input file
if not os.path.isfile(args.input):
print(f"Error: Input file '{args.input}' does not exist.")
return
# Ensure output has .xlsx extension
if not args.output.lower().endswith(".xlsx"):
print("Error: Output file must have a .xlsx extension.")
return
sqlite_to_excel(args.input, args.output)
if __name__ == "__main__":
main()