Update app.py
Browse files5 unmodified lines678910111213141516171819202122232425262728293031323334353637383940414243441 unmodified line46474849505152535 unmodified linesall_sheets = pd.read_excel(uploaded_file.name, sheet_name=None, engine="openpyxl")ordered_sheetnames = [k for k in all_sheets.keys() if k.strip().upper() != "TEMPLATE" and not all_sheets[k].empty]records = []for week in ordered_sheetnames:df = all_sheets[week].rename(columns=lambda x: x.strip() if isinstance(x, str) else x)name_col = next((c for c in df.columns if "name" in str(c).lower()), None)hours_cols = []if "REGULAR HOURS" in df.columns:hours_cols.append("REGULAR HOURS")if "OVER TIME" in df.columns:hours_cols.append("OVER TIME")if not name_col or not hours_cols:continue# Convert hours columns to numeric and sum them to get total hoursdf["Total_Hours"] = df[hours_cols].apply(pd.to_numeric, errors='coerce').sum(axis=1)valid = df[name_col].notnull() & df["Total_Hours"].notnull() & (df["Total_Hours"] > 0)for _, row in df.loc[valid, [name_col, "Total_Hours"]].iterrows():emp = str(row[name_col]).strip()hrs = float(row["Total_Hours"])if emp and not emp.lower().startswith("unnamed"):records.append({"Week": week, "Employee": emp, "Hours": hrs})if not records:return "No employee/hour data found."df_all = pd.DataFrame(records)df_all['Week'] = pd.Categorical(df_all['Week'], categories=ordered_sheetnames, ordered=True)book2 = df_all.pivot_table(index="Week", columns="Employee", values="Hours", fill_value="").reset_index()output_file = "Filtered_Excel.xlsx"book2.to_excel(output_file, index=False)return output_fileexcept Exception as e:return f"Error: {str(e)}"1 unmodified linefn=process_excel,inputs=gr.File(label="Upload Excel File"),outputs=gr.File(label="Download Filtered Excel"),title="Excel Employee Hours Filter")iface.launch()5 unmodified lines6789101112131415161718192021222324252627282930313233341 unmodified line3637383940415 unmodified linesall_sheets = pd.read_excel(uploaded_file.name, sheet_name=None, engine="openpyxl")ordered_sheetnames = [k for k in all_sheets.keys() if k.strip().upper() != "TEMPLATE" and not all_sheets[k].empty]records = []for week in ordered_sheetnames:df = all_sheets[week].rename(columns=lambda x: x.strip() if isinstance(x, str) else x)name_col = next((c for c in df.columns if "name" in str(c).lower()), None)shifts_col = next((c for c in df.columns if "shift" in str(c).lower()), None)hours_col = next((c for c in df.columns if "hour" == str(c).strip().lower()), None)if not name_col or not shifts_col or not hours_col:continue# Count valid shifts and take value from Hours columnvalid = df[name_col].notnull() & df[shifts_col].notnull() & df[hours_col].notnull()for _, row in df.loc[valid, [name_col, shifts_col, hours_col]].iterrows():emp = str(row[name_col]).strip()num_shift = int(row[shifts_col])hrs = float(row[hours_col])if emp and not emp.lower().startswith("unnamed"):records.append({"Week": week, "Employee": emp, "No_of_Shift": num_shift, "Hours": hrs})if not records:return "No employee/shift data found."df_all = pd.DataFrame(records)df_all['Week'] = pd.Categorical(df_all['Week'], categories=ordered_sheetnames, ordered=True)book2 = df_all.pivot_table(index="Week", columns="Employee", values=["No_of_Shift", "Hours"], fill_value="").reset_index()output_file = "Filtered_Excel.xlsx"book2.to_excel(output_file, index=False)return output_fileexcept Exception as e:return f"Error: {str(e)}"1 unmodified linefn=process_excel,inputs=gr.File(label="Upload Excel File"),outputs=gr.File(label="Download Filtered Excel"),title="Excel Employee Shifts Filter")iface.launch()
app.py+12-24