Spaces:
Sleeping
Sleeping
New library that allows excel modification through the execution of python code directly provided by the user
Browse files- code_df_custom.py +32 -0
code_df_custom.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
|
| 3 |
+
def load_excel(file):
|
| 4 |
+
df = pd.read_excel(file)
|
| 5 |
+
return file, df
|
| 6 |
+
|
| 7 |
+
def run_code(file, code):
|
| 8 |
+
scope = {'pd': pd}
|
| 9 |
+
if file:
|
| 10 |
+
print('file ok')
|
| 11 |
+
df = pd.read_excel(file)
|
| 12 |
+
|
| 13 |
+
scope['df'] = df
|
| 14 |
+
exec(code, scope, scope)
|
| 15 |
+
print(scope.keys())
|
| 16 |
+
if not 'new_df' in scope:
|
| 17 |
+
print("new_df not defined")
|
| 18 |
+
scope['new_df'] = df.copy()
|
| 19 |
+
new_df = scope['new_df']
|
| 20 |
+
|
| 21 |
+
return new_df
|
| 22 |
+
else:
|
| 23 |
+
print(f"No file provided")
|
| 24 |
+
df = pd.DataFrame()
|
| 25 |
+
return df
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def export_df(df, filename):
|
| 29 |
+
filename = filename.replace('.xlsx', '_coded.xlsx')
|
| 30 |
+
df.to_excel(filename, index=False)
|
| 31 |
+
return filename
|
| 32 |
+
|