You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
2.2 KiB
65 lines
2.2 KiB
#!/bin/python3
|
|
import pandas as pd
|
|
from argparse import ArgumentParser
|
|
|
|
# Wraps strings and timestamps into single quotes
|
|
def wrap_types(val):
|
|
if type(val) in [str, pd._libs.tslibs.timestamps.Timestamp] and val != "NULL":
|
|
return f"'{val}'"
|
|
else:
|
|
return val
|
|
|
|
p = ArgumentParser()
|
|
p.add_argument("-i", "--input", nargs="+", help="Path to input excel file")
|
|
p.add_argument("-o", "--output", nargs="+", help="Path to output sql file")
|
|
args = p.parse_args()
|
|
|
|
# Manual fallback if argument is not specified
|
|
if not args.input:
|
|
in_file_path = input("Please enter path to input excel file: ")
|
|
else:
|
|
in_file_path = args.input[0]
|
|
|
|
if not args.output:
|
|
out_file_path = input("Please enter path to output sql file: ")
|
|
else:
|
|
out_file_path = args.output[0]
|
|
|
|
# Simple input argument checks
|
|
if out_file_path.endswith(("xlsx", "xls")):
|
|
print("Aborting task. Possible overwrite of source file detected.")
|
|
print(f"Specified output file '{out_file_path}' ends with .xls/.xlsx")
|
|
exit(1)
|
|
|
|
if not in_file_path.endswith(("xlsx", "xls")):
|
|
print("Aborting task. No input file in excel format specified.")
|
|
exit(1)
|
|
|
|
print(f'Input: {in_file_path}\nOutput: {out_file_path}')
|
|
|
|
in_file = pd.ExcelFile(in_file_path)
|
|
|
|
with open(out_file_path, "w") as of:
|
|
for sheet in in_file.sheet_names:
|
|
print(f'Processing sheet "{sheet}"...')
|
|
db_table = in_file.parse(sheet, nrows=1, header=None)[0][0]
|
|
curr_sheet = in_file.parse(sheet, header=1).fillna("NULL")
|
|
count = 0
|
|
# One line is one test subject/object
|
|
for line in curr_sheet.iterrows():
|
|
indices = line[1].index
|
|
values = line[1].values
|
|
cols = ""
|
|
vals = ""
|
|
for i in range(len(indices)):
|
|
if i != 0:
|
|
cols += f', "{indices[i]}"'
|
|
vals += f', {wrap_types(values[i])}'
|
|
else:
|
|
cols += f'"{indices[i]}"'
|
|
vals += f'{wrap_types(values[i])}'
|
|
# INSERT INTO $DB.$TABLE ($COL1, $COL2, ...) VALUES ($VAL1, $VAL2, ...)
|
|
of.write(f'INSERT INTO {db_table} ({cols}) VALUES ({vals});\n')
|
|
count += 1
|
|
print(f'Created {count} INSERT statements for sheet "{sheet}"\n')
|