Update app.py
Browse files6 unmodified lines78910111225 unmodified lines383940414243448 unmodified lines5354555657585960616217 unmodified lines80818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531546 unmodified linesfrom datetime import datetimefrom dateutil.parser import parseimport gradio as grdef find_best_match(target, columns):str_columns = [str(c) for c in columns]25 unmodified linesdef extract_pay_period_from_top_rows(file_path, sheet_name, rows_to_check=3):df_raw = pd.read_excel(file_path, sheet_name=sheet_name, header=None)dates_found = []for row_idx in range(rows_to_check):for val in df_raw.iloc[row_idx].values:if pd.isna(val):8 unmodified linesdates_found.append(dt)except Exception:continueif not dates_found:return "Pay Period Unknown"min_date = min(dates_found)max_date = max(dates_found)fmt = "%d %b"17 unmodified linessheet_name = pd.ExcelFile(file_path).sheet_names[0]df = pd.read_excel(file_path, sheet_name=sheet_name, header=1)columns = list(df.columns)name_col = find_best_match("DA'S NAME", columns) or find_best_match("Name", columns)regular_hours_col = find_best_match("REGULAR HOURS", columns)extra_shift_col = find_best_match("EXTRA SHIFT", columns)bonus_col = find_best_match("BONUS", columns)deduct_col = find_best_match("DEDUCTION", columns)total_col = find_best_match("TOTAL PAY", columns)pay_period = extract_pay_period_from_top_rows(file_path, sheet_name)if not all([name_col, regular_hours_col, total_col]):return f"Error: Required columns missing. Detected columns: {columns}"labels_per_page = 30cols = 3rows = 10doc = Document()for i in range(0, len(df), labels_per_page):chunk = df.iloc[i:i + labels_per_page]table = doc.add_table(rows=rows, cols=cols)table.autofit = Falsefor c_idx in range(cols):for r_idx in range(rows):cell = table.cell(r_idx, c_idx)cell.width = Pt(180)for idx, (_, row) in enumerate(chunk.iterrows()):r = idx // colsc = idx % colscell = table.cell(r, c)name = str(row.get(name_col, "")).strip()regular_hours = row.get(regular_hours_col, 0)extra_shift = row.get(extra_shift_col, 0) if extra_shift_col else 0bonus = row.get(bonus_col, 0) if bonus_col else 0deduction = row.get(deduct_col, 0) if deduct_col else 0total_pay = row.get(total_col, 0)if not name or name.lower() in ("nan", "none"):continuetotal_hours = 0if pd.notna(regular_hours):total_hours += float(regular_hours)if pd.notna(extra_shift):total_hours += float(extra_shift)lines = [f"Name: {name}",f"Total Hours: {int(total_hours)}" + (f" Bonus: {format_money(bonus)}" if format_money(bonus) else "") + (f" Deduction: {format_money(deduction)}" if format_money(deduction) else ""),f"Total Pay: {format_money(total_pay) or '$0'}",f"Pay Period: {pay_period}"]write_label_to_cell(cell, lines)if i + labels_per_page < len(df):doc.add_page_break()output_file = "Generated_Labels.docx"doc.save(output_file)return output_fileexcept Exception as e:import tracebackreturn f"❌ Error: {e}\n{traceback.format_exc()}"demo = gr.Interface(fn=generate_labels,6 unmodified lines7891011121325 unmodified lines3940414243448 unmodified lines535455565758596017 unmodified lines787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371386 unmodified linesfrom datetime import datetimefrom dateutil.parser import parseimport gradio as grimport osdef find_best_match(target, columns):str_columns = [str(c) for c in columns]25 unmodified linesdef extract_pay_period_from_top_rows(file_path, sheet_name, rows_to_check=3):df_raw = pd.read_excel(file_path, sheet_name=sheet_name, header=None)dates_found = []for row_idx in range(rows_to_check):for val in df_raw.iloc[row_idx].values:if pd.isna(val):8 unmodified linesdates_found.append(dt)except Exception:continueif not dates_found:return "Pay Period Unknown"min_date = min(dates_found)max_date = max(dates_found)fmt = "%d %b"17 unmodified linessheet_name = pd.ExcelFile(file_path).sheet_names[0]df = pd.read_excel(file_path, sheet_name=sheet_name, header=1)columns = list(df.columns)name_col = find_best_match("DA'S NAME", columns) or find_best_match("Name", columns)regular_hours_col = find_best_match("REGULAR HOURS", columns)extra_shift_col = find_best_match("EXTRA SHIFT", columns)bonus_col = find_best_match("BONUS", columns)deduct_col = find_best_match("DEDUCTION", columns)total_col = find_best_match("TOTAL PAY", columns)pay_period = extract_pay_period_from_top_rows(file_path, sheet_name)if not all([name_col, regular_hours_col, total_col]):return f"Error: Required columns missing. Detected columns: {columns}"labels_per_page = 30cols = 3rows = 10doc = Document()for i in range(0, len(df), labels_per_page):chunk = df.iloc[i:i + labels_per_page]table = doc.add_table(rows=rows, cols=cols)table.autofit = Falsefor c_idx in range(cols):for r_idx in range(rows):cell = table.cell(r_idx, c_idx)cell.width = Pt(180)for idx, (_, row) in enumerate(chunk.iterrows()):r = idx // colsc = idx % colscell = table.cell(r, c)name = str(row.get(name_col, "")).strip()regular_hours = row.get(regular_hours_col, 0)extra_shift = row.get(extra_shift_col, 0) if extra_shift_col else 0bonus = row.get(bonus_col, 0) if bonus_col else 0deduction = row.get(deduct_col, 0) if deduct_col else 0total_pay = row.get(total_col, 0)if not name or name.lower() in ("nan", "none"):continuetotal_hours = 0if pd.notna(regular_hours):total_hours += float(regular_hours)if pd.notna(extra_shift):total_hours += float(extra_shift)lines = [f"Name: {name}",f"Total Hours: {int(total_hours)}" + (f" Bonus: {format_money(bonus)}" if format_money(bonus) else "") + (f" Deduction: {format_money(deduction)}" if format_money(deduction) else ""),f"Total Pay: {format_money(total_pay) or '$0'}",f"Pay Period: {pay_period}"]write_label_to_cell(cell, lines)if i + labels_per_page < len(df):doc.add_page_break()output_file = "Generated_Labels.docx"doc.save(output_file)return output_fileexcept Exception as e:import tracebackreturn f"Error: {str(e)}\n{traceback.format_exc()}"import gradio as grdemo = gr.Interface(fn=generate_labels,
app.py+4-20