Spaces:
Sleeping

File size: 2,151 Bytes
5c9efba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import pandas as pd
import gradio as gr

def process_excel(uploaded_file):
    try:
        all_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 hours
            df["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_file

    except Exception as e:
        return f"Error: {str(e)}"

iface = gr.Interface(
    fn=process_excel,
    inputs=gr.File(label="Upload Excel File"),
    outputs=gr.File(label="Download Filtered Excel"),
    title="Excel Employee Hours Filter"
)

iface.launch()