Standard_Intelligence_Dev / code_df_custom.py
YchKhan's picture
New library that allows excel modification through the execution of python code directly provided by the user
ceb3ae8 verified
raw
history blame
688 Bytes
import pandas as pd
def load_excel(file):
df = pd.read_excel(file)
return file, df
def run_code(file, code):
scope = {'pd': pd}
if file:
print('file ok')
df = pd.read_excel(file)
scope['df'] = df
exec(code, scope, scope)
print(scope.keys())
if not 'new_df' in scope:
print("new_df not defined")
scope['new_df'] = df.copy()
new_df = scope['new_df']
return new_df
else:
print(f"No file provided")
df = pd.DataFrame()
return df
def export_df(df, filename):
filename = filename.replace('.xlsx', '_coded.xlsx')
df.to_excel(filename, index=False)
return filename