Update app.py
Browse files6 unmodified lines7891011121325 unmodified lines394041414243448 unmodified lines5354555656575958596017 unmodified lines7879808381828384858690879288899590919299931019495969710698991001011111021031041051161061071081091101111231121131261141151161171181321191201211221231241391251411261271441281291301481311321511331341351361371386 unmodified linesfrom datetime import datetimefrom dateutil.parser import parseimport gradio as grimport os
def 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 lines dates_found.append(dt) except Exception: continue
if not dates_found: return "Pay Period Unknown"
min_date = min(dates_found) max_date = max(dates_found) fmt = "%d %b"17 unmodified lines sheet_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 = 30 cols = 3 rows = 10
doc = 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 = False
for 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 // cols c = idx % cols cell = 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 0 bonus = row.get(bonus_col, 0) if bonus_col else 0 deduction = row.get(deduct_col, 0) if deduct_col else 0 total_pay = row.get(total_col, 0)
if not name or name.lower() in ("nan", "none"): continue
total_hours = 0 if 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_file
except Exception as e: import traceback return f"❌ Error: {e}\n{traceback.format_exc()}" return f"Error: {str(e)}\n{traceback.format_exc()}"
import gradio as gr
demo = gr.Interface( fn=generate_labels,app.py+4-20