File size: 1,792 Bytes
eb36e3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
55
56
57
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()