|
|
|
@ -1,23 +1,20 @@
|
|
|
|
|
#!/bin/python3
|
|
|
|
|
import pandas as pd
|
|
|
|
|
import numpy as np
|
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
|
|
|
|
|
|
# Wraps strings and timestamps into single quotes
|
|
|
|
|
def wrap_types(val):
|
|
|
|
|
if np.isnan(val):
|
|
|
|
|
print("NaN FOUND")
|
|
|
|
|
val = "NULL"
|
|
|
|
|
if type(val) in [str, pd._libs.tslibs.timestamps.Timestamp]:
|
|
|
|
|
if type(val) in [str, pd._libs.tslibs.timestamps.Timestamp] and val != "NULL":
|
|
|
|
|
return f"'{val}'"
|
|
|
|
|
else:
|
|
|
|
|
return val
|
|
|
|
|
|
|
|
|
|
p = ArgumentParser()
|
|
|
|
|
# action store_true is for flags
|
|
|
|
|
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:
|
|
|
|
@ -28,20 +25,26 @@ if not args.output:
|
|
|
|
|
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)
|
|
|
|
|
print(in_file.parse("Datei", skiprows=1).head())
|
|
|
|
|
|
|
|
|
|
with open(out_file_path, "w") as of:
|
|
|
|
|
for sheet in in_file.sheet_names:
|
|
|
|
|
curr_meta = in_file.parse(sheet, nrows=1, header=None)
|
|
|
|
|
curr_sheet = in_file.parse(sheet, header=1)
|
|
|
|
|
curr_meta = curr_meta[0][0].split("#")
|
|
|
|
|
db = curr_meta[0]
|
|
|
|
|
table_prefix = curr_meta[1]
|
|
|
|
|
table_suffix = sheet
|
|
|
|
|
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
|
|
|
|
@ -49,7 +52,6 @@ with open(out_file_path, "w") as of:
|
|
|
|
|
cols = ""
|
|
|
|
|
vals = ""
|
|
|
|
|
for i in range(len(indices)):
|
|
|
|
|
# print(type(values[i]), ": ", values[i])
|
|
|
|
|
if i != 0:
|
|
|
|
|
cols += f", {indices[i]}"
|
|
|
|
|
vals += f", {wrap_types(values[i])}"
|
|
|
|
@ -57,4 +59,6 @@ with open(out_file_path, "w") as of:
|
|
|
|
|
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_prefix}.{table_suffix}" ({cols}) VALUES ({vals})\n')
|
|
|
|
|
of.write(f'INSERT INTO {db_table} ({cols}) VALUES ({vals})\n')
|
|
|
|
|
count += 1
|
|
|
|
|
print(f'Created {count} INSERT statements for sheet "{sheet}"\n')
|
|
|
|
|