Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import pandas as pdfrom docx import Documentimport gradio as grimport osdef generate_labels(excel_file, template_file):# Load Exceldf = pd.read_excel(excel_file.name)# Load Word templatedoc = Document(template_file.name)# Clear template paragraphsfor p in doc.paragraphs:p.text = ""# Iterate over rowsfor _, row in df.iterrows():name = row.get("DA'S NAME", "")hours = row.get("Total Hours", "")bonus = row.get("Bonus", "")deduction = row.get("Deduction", "")total_pay = row.get("Total Pay", "")pay_period = row.get("Pay Period", "")text = f"""Name: {name}Total Hours: {hours} {'Bonus: $' + str(bonus) if pd.notna(bonus) and bonus > 0 else ''}{'Deduction: $' + str(deduction) if pd.notna(deduction) and deduction > 0 else ''}Total Pay: ${total_pay}Pay Period: {pay_period}"""doc.add_paragraph(text.strip())doc.add_paragraph()# Save outputoutput_file = "Generated_Labels.docx"doc.save(output_file)return output_filewith gr.Blocks() as demo:gr.Markdown("## Payroll Label Generator")gr.Markdown("Upload your **Excel payroll file** and the **Word label template**, get a ready-made Word document.")with gr.Row():excel_file = gr.File(label="Upload Excel File (.xlsx)", file_types=[".xlsx"])template_file = gr.File(label="Upload Word Template (.doc or .docx)", file_types=[".doc", ".docx"])output = gr.File(label="Download Generated Labels")btn = gr.Button("Generate Labels")btn.click(fn=generate_labels, inputs=[excel_file, template_file], outputs=output)demo.launch()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253import pandas as pdfrom docx import Documentdef generate_labels(excel_file):# Load Excel (read all sheets, but assume only 1 sheet per period)xls = pd.ExcelFile(excel_file)sheet_name = xls.sheet_names[0] # first sheet name = pay perioddf = pd.read_excel(excel_file, sheet_name=sheet_name)# Create new Word documentdoc = Document()for _, row in df.iterrows():name = str(row.get("DA'S NAME", "")).strip()hours = row.get("Total Hours", "")bonus = row.get("Bonus", "")deduction = row.get("Deduction", "")total_pay = row.get("Total Pay", "")# Build label text blocklabel_lines = []# Line 1: Namelabel_lines.append(f" Name: {name}")# Line 2: Hours + Bonus + Deduction (only if present)line2 = f"Total Hours: {hours}"if pd.notna(bonus) and bonus != 0:line2 += f" Bonus: ${int(bonus)}"if pd.notna(deduction) and deduction != 0:line2 += f" Deduction: ${int(deduction)}"label_lines.append(line2)# Line 3: Total Paylabel_lines.append(f"Total Pay: ${int(total_pay)}")# Line 4: Pay Period (from sheet name)label_lines.append(f"Pay Period: {sheet_name}")# Add to Worddoc.add_paragraph("\n".join(label_lines))doc.add_paragraph() # blank line between labels# Saveoutput_file = "Generated_Labels.docx"doc.save(output_file)return output_file# Example run (local testing)if __name__ == "__main__":output = generate_labels("Main File.xlsx")print(f"Labels saved to {output}")
app.py+53-56