File size: 3,969 Bytes
210cae4
e7797e7
6621159
210cae4
8a53b2d
 
e7797e7
8a53b2d
210cae4
e32e31b
 
 
 
 
 
 
 
98ebf2e
210cae4
e32e31b
210cae4
 
1d5d40c
210cae4
f3b72a6
210cae4
 
e32e31b
 
 
 
 
210cae4
f3b72a6
 
 
e32e31b
f3b72a6
 
1d5d40c
f3b72a6
 
8a53b2d
f3b72a6
 
8a53b2d
210cae4
 
 
cb1f896
210cae4
c1f7d32
 
 
 
210cae4
e32e31b
210cae4
e7797e7
 
f3b72a6
e7797e7
 
 
 
 
 
210cae4
e7797e7
210cae4
 
 
 
 
f3b72a6
210cae4
 
c1f7d32
210cae4
 
c1f7d32
210cae4
 
cb1f896
c1f7d32
 
 
 
210cae4
 
 
cb1f896
210cae4
 
 
 
 
c1f7d32
 
210cae4
 
c1f7d32
210cae4
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import pandas as pd
from openpyxl import Workbook
import gradio as gr
import os
import warnings

# Suppress warnings
warnings.filterwarnings("ignore", category=UserWarning, module="openpyxl")

# Hardcoded mapping logic (from reference file)
MAPPING = [
    {"Sheet Name": "Sheet1", "Input Column": "A", "PO Output Column": "Order Number", "Start Row": 2},
    {"Sheet Name": "Sheet1", "Input Column": "B", "PO Output Column": "Customer Name", "Start Row": 2},
    {"Sheet Name": "Sheet2", "Input Column": "C", "PO Output Column": "Product Code", "Start Row": 2},
    {"Sheet Name": "Sheet2", "Input Column": "D", "PO Output Column": "Quantity", "Start Row": 2},
    # Add more mappings as needed
]

# Function to extract and map data from the input workbook
def transform_data(input_path):
    # Load the input workbook
    input_workbook = pd.ExcelFile(input_path)

    # Initialize a dictionary to store data for output
    output_data = {}

    # Iterate through each mapping rule
    for mapping in MAPPING:
        output_column = mapping["PO Output Column"]
        input_sheet = mapping["Sheet Name"]
        input_column = mapping["Input Column"]
        start_row = mapping.get("Start Row", 2)  # Default start row is 2 if not specified

        # Extract data from the specified sheet and column
        if input_sheet in input_workbook.sheet_names:
            sheet_data = pd.read_excel(input_path, sheet_name=input_sheet, usecols=[input_column], skiprows=start_row - 1)
            output_data[output_column] = sheet_data.iloc[:, 0].tolist()
        else:
            output_data[output_column] = []  # If sheet is missing, add empty column

    # Ensure all columns have the same number of rows by filling with blanks
    max_rows = max(len(col_data) for col_data in output_data.values())
    for key in output_data:
        while len(output_data[key]) < max_rows:
            output_data[key].append("")

    return pd.DataFrame(output_data)

# Main processing function
def process_files(input_workbook):
    try:
        # Validate input file
        if not os.path.exists(input_workbook):
            return None, "Input workbook file does not exist."

        # Transform the data
        transformed_data = transform_data(input_workbook)

        # Create a new workbook for the output
        output_workbook = Workbook()
        output_sheet = output_workbook.active
        output_sheet.title = "Generated Output"

        # Write headers
        headers = transformed_data.columns.tolist()
        for col_idx, header in enumerate(headers, start=1):
            output_sheet.cell(row=1, column=col_idx, value=header)

        # Write data rows
        for row_idx, row_data in enumerate(transformed_data.itertuples(index=False), start=2):
            for col_idx, value in enumerate(row_data, start=1):
                output_sheet.cell(row=row_idx, column=col_idx, value=value)

        # Save the generated output file
        output_file_path = "Generated_Output_Final.xlsx"
        output_workbook.save(output_file_path)

        return output_file_path, None

    except Exception as e:
        return None, f"An error occurred during file generation: {e}"

# Define the Gradio interface
def generate_excel(input_workbook):
    file_path, error = process_files(input_workbook.name)
    if error:
        return None, error
    return file_path, "File generated successfully."

with gr.Blocks() as app:
    gr.Markdown("# Excel Sheet Generator")
    gr.Markdown("Upload the input workbook to generate the final Excel file.")

    with gr.Row():
        input_workbook = gr.File(label="Input Workbook", file_types=[".xlsx"])

    output_file = gr.File(label="Generated Excel File")
    status_message = gr.Textbox(label="Status", interactive=False)

    generate_button = gr.Button("Generate Excel File")

    generate_button.click(generate_excel, inputs=[input_workbook], outputs=[output_file, status_message])

# Launch the app
app.launch()