Update app.py
Browse files
app.py
CHANGED
|
@@ -2,40 +2,35 @@ import gradio as gr
|
|
| 2 |
import pandas as pd
|
| 3 |
|
| 4 |
def process_file(file_path):
|
| 5 |
-
try:
|
| 6 |
-
# read excel
|
| 7 |
-
df = pd.read_excel(file_path, header=None)
|
| 8 |
|
| 9 |
-
|
| 10 |
-
df = df.iloc[4:].reset_index(drop=True)
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
df[df.shape[1]] = ""
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
df
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
df.to_csv(output_file, index=False, header=False)
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
return str(e)
|
| 31 |
|
| 32 |
|
| 33 |
demo = gr.Interface(
|
| 34 |
fn=process_file,
|
| 35 |
inputs=gr.File(type="filepath", label="Upload Excel File"),
|
| 36 |
outputs=gr.File(label="Download CSV"),
|
| 37 |
-
title="Excel to CSV Tool"
|
| 38 |
-
description="Upload Excel → remove first 4 rows → H = I - J → download CSV"
|
| 39 |
)
|
| 40 |
|
| 41 |
demo.launch()
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
|
| 4 |
def process_file(file_path):
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
df = pd.read_excel(file_path, header=None)
|
|
|
|
| 7 |
|
| 8 |
+
# delete first 4 rows
|
| 9 |
+
df = df.iloc[4:].reset_index(drop=True)
|
|
|
|
| 10 |
|
| 11 |
+
# ensure at least 10 columns
|
| 12 |
+
while df.shape[1] < 10:
|
| 13 |
+
df[df.shape[1]] = ""
|
| 14 |
|
| 15 |
+
# convert column I and J to numbers safely
|
| 16 |
+
df.iloc[:,8] = pd.to_numeric(df.iloc[:,8], errors="coerce").fillna(0)
|
| 17 |
+
df.iloc[:,9] = pd.to_numeric(df.iloc[:,9], errors="coerce").fillna(0)
|
| 18 |
|
| 19 |
+
# H = I - J
|
| 20 |
+
df.iloc[:,7] = df.iloc[:,8] - df.iloc[:,9]
|
|
|
|
| 21 |
|
| 22 |
+
# save CSV
|
| 23 |
+
output_file = "/tmp/output.csv"
|
| 24 |
+
df.to_csv(output_file, index=False, header=False)
|
| 25 |
|
| 26 |
+
return output_file
|
|
|
|
| 27 |
|
| 28 |
|
| 29 |
demo = gr.Interface(
|
| 30 |
fn=process_file,
|
| 31 |
inputs=gr.File(type="filepath", label="Upload Excel File"),
|
| 32 |
outputs=gr.File(label="Download CSV"),
|
| 33 |
+
title="Excel to CSV Tool"
|
|
|
|
| 34 |
)
|
| 35 |
|
| 36 |
demo.launch()
|