Skip to content

Instantly share code, notes, and snippets.

@lxndr-rl
Created September 14, 2021 02:38
Show Gist options
  • Save lxndr-rl/d01cd17784409ce4ddde1fbb8b2535e8 to your computer and use it in GitHub Desktop.
Save lxndr-rl/d01cd17784409ce4ddde1fbb8b2535e8 to your computer and use it in GitHub Desktop.
Excel table to Sql insert
import pandas as pd
import sys
if(len(sys.argv[1:]) < 2):
print("Uso: py script.py _NOMBRETABLA_ --uppercase|--lowercase(opcional)")
exit()
try:
argumento = sys.argv[3]
except:
argumento = "invalid"
df = pd.read_excel(sys.argv[2], sheet_name='Sheet1')
if(argumento != '--uppercase' and argumento != '--lowercase'):
print("Opcion invalida. Se usará lowercase\n")
argumento = '--lowercase'
stringini = f"insert into {sys.argv[1]} VALUES("
stringValues = ""
first = True
for i in df.values:
for j in i:
if first:
if(argumento == '--uppercase'):
stringValues = f"{stringini.upper()}{j}"
else:
stringValues = f"{stringini.lower()}{j}"
first = False
else:
if(type(j) == int or type(j) == float):
stringValues = f"{stringValues}, {j}"
else:
if(argumento == '--uppercase'):
stringValues = f"{stringValues}, '{j.upper()}'"
else:
stringValues = f"{stringValues}, '{j}'"
print(f"{stringValues})")
first = True
stringValues = ''
@lxndr-rl
Copy link
Author

Convert
image

To
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment