import pandas as pd from docx import Document import gradio as gr import os def generate_labels(excel_file, template_file): # Load Excel df = pd.read_excel(excel_file.name) # Load Word template doc = Document(template_file.name) # Clear template paragraphs for p in doc.paragraphs: p.text = "" # Iterate over rows for _, 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 output output_file = "Generated_Labels.docx" doc.save(output_file) return output_file with 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()