Update app.py
Browse files9 unmodified lines1011121314151617181920212223242526272829303132333435363738394041429 unmodified linesdf = all_sheets[week].rename(columns=lambda x: str(x).strip())name_col = next((c for c in df.columns if "name" in str(c).lower()), None)shift_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" in str(c).lower()), None)if not name_col or not shift_col:continuevalid = df[name_col].notnull() & df[shift_col].notnull() & (df[shift_col] > 0)for _, row in df.loc[valid, [name_col, shift_col]].iterrows():emp = str(row[name_col]).strip()num_shifts = float(row[shift_col])record = {"Week": week, "Employee": emp, "No_of_Shift": num_shifts}if hours_col:hrs = row[hours_col]record["Hours"] = hrs if pd.notnull(hrs) else ""if emp and not emp.lower().startswith("unnamed"):records.append(record)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)# Pivot so that shifts (and optionally hours) show for each employee in each weekif "Hours" in df_all.columns:book2 = df_all.pivot_table(index="Week", columns="Employee", values=["No_of_Shift", "Hours"], fill_value="").reset_index()else:book2 = df_all.pivot_table(index="Week", columns="Employee", values="No_of_Shift", 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)}"iface = gr.Interface(fn=process_excel,9 unmodified lines1011121314151617181920212223242526272829303132333435363738399 unmodified linesdf = all_sheets[week].rename(columns=lambda x: str(x).strip())name_col = next((c for c in df.columns if "name" in str(c).lower()), None)shift_col = next((c for c in df.columns if "shift" in str(c).lower()), None)if not name_col or not shift_col:continuevalid = df[name_col].notnull() & df[shift_col].notnull() & (df[shift_col] > 0)for _, row in df.loc[valid, [name_col, shift_col]].iterrows():emp = str(row[name_col]).strip()num_shifts = float(row[shift_col])if emp and not emp.lower().startswith("unnamed"):records.append({"Week": week, "Employee": emp, "No_of_Shift": num_shifts})output_file = "Filtered_Excel.xlsx"if not records:error_file = "Error_No_Data_Found.txt"with open(error_file, "w") as f:f.write("No employee/shift data found.")return error_filedf_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", fill_value="").reset_index()book2.to_excel(output_file, index=False)return output_fileexcept Exception as e:error_file = "Error.txt"with open(error_file, "w") as f:f.write(str(e))return error_fileiface = gr.Interface(fn=process_excel,
app.py+11-14