Last active
March 2, 2016 08:06
-
-
Save clausd/7e6e0136fd51058bb4a7 to your computer and use it in GitHub Desktop.
Excel to Mysql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import pandas as pd | |
import mysql.connector | |
from sqlalchemy import create_engine | |
from string import Template | |
import os | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-u", "--user", help="mysql user", nargs='?', const='') | |
parser.add_argument("-p", "--password", help="mysql password", nargs='?', const='') | |
parser.add_argument("-e", "--excel", help="Excel file to table-ize.\nIf the file does not exist try to create from table") | |
parser.add_argument("database", help="database to use") | |
parser.add_argument("table", help="table to create/export") | |
args = parser.parse_args() | |
excel_file = os.path.abspath(args.excel) | |
connection_template = Template('mysql+mysqlconnector://$user:$password@localhost/$database') | |
connection_string = connection_template.substitute(user=args.user, password=args.password, database=args.database) | |
print "Connecting to " + connection_string | |
engine = create_engine(connection_string, echo=False) | |
if os.path.exists(excel_file): | |
print "populating " + args.table | |
records = pd.read_excel(excel_file) | |
records.to_sql(name = args.table, con=engine, if_exists='append', index=False, chunksize=2) | |
else: | |
print "exporting " + args.table + " to " + excel_file | |
records = pd.read_sql_table(args.table, engine) | |
records.to_excel(excel_file, sheet_name=args.table) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment