patrickligardes commited on
Commit
f3b72a6
·
verified ·
1 Parent(s): 8a53b2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -26
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import pandas as pd
2
  from openpyxl import load_workbook
 
3
  import gradio as gr
4
  import os
5
  import warnings
@@ -9,11 +10,8 @@ warnings.filterwarnings("ignore", category=UserWarning, module="openpyxl")
9
 
10
  # Load the constant mapping file (embedded in the app)
11
  def load_mapping():
12
- mapping_data = {
13
- "PO Output Column": ["Column1", "Column2", "Column3"], # Replace with actual columns
14
- "UVM MMB POLY STICKER Column": ["Sheet1.ColumnA", "Sheet1.ColumnB", "Fixed-Value"] # Replace with mapping logic
15
- }
16
- return pd.DataFrame(mapping_data)
17
 
18
  # Function to extract and map data from the input workbook
19
  def transform_data(input_path, mapping_df):
@@ -21,33 +19,30 @@ def transform_data(input_path, mapping_df):
21
  input_workbook = pd.ExcelFile(input_path)
22
 
23
  # Initialize a dictionary to store data for output
24
- output_data = {col: [] for col in mapping_df["PO Output Column"] if not pd.isna(col)}
25
 
26
  # Iterate through each mapping rule
27
  for _, row in mapping_df.iterrows():
28
  output_column = row["PO Output Column"]
29
- input_rule = row["UVM MMB POLY STICKER Column"]
 
 
30
 
31
- if pd.isna(output_column) or pd.isna(input_rule):
32
  continue
33
 
34
- # Handle fixed values
35
- if "Fixed" in input_rule:
36
- fixed_value = input_rule.split("-")[0].strip()
37
- output_data[output_column] = [fixed_value] * 10 # Placeholder for rows
 
 
38
 
39
- # Handle column mapping from input workbook
40
- elif "." in input_rule:
41
- sheet_name, column_name = input_rule.split(".")
42
- if sheet_name in input_workbook.sheet_names:
43
- sheet_data = pd.read_excel(input_path, sheet_name=sheet_name)
44
- if column_name in sheet_data.columns:
45
- output_data[output_column] = sheet_data[column_name].tolist()
46
-
47
- # Fill missing columns with empty lists
48
  for key in output_data:
49
- if not output_data[key]:
50
- output_data[key] = ["" for _ in range(10)]
51
 
52
  return pd.DataFrame(output_data)
53
 
@@ -61,12 +56,12 @@ def process_files(input_workbook):
61
  transformed_data = transform_data(input_workbook, mapping_df)
62
 
63
  # Load the output template (embedded in the app)
64
- output_template_path = "output_template.xlsx" # Replace with the actual template file path
65
  if not os.path.exists(output_template_path):
66
  return "Output template file is missing."
67
 
68
  output_workbook = load_workbook(output_template_path)
69
- output_sheet = output_workbook["363040"]
70
 
71
  # Write transformed data to the output sheet
72
  for row_idx, row_data in enumerate(transformed_data.itertuples(index=False), start=2):
@@ -74,7 +69,7 @@ def process_files(input_workbook):
74
  output_sheet.cell(row=row_idx, column=col_idx, value=value)
75
 
76
  # Save the generated output file
77
- output_file_path = "Generated_Output.xlsx"
78
  output_workbook.save(output_file_path)
79
 
80
  return output_file_path
 
1
  import pandas as pd
2
  from openpyxl import load_workbook
3
+ from openpyxl.utils import get_column_letter
4
  import gradio as gr
5
  import os
6
  import warnings
 
10
 
11
  # Load the constant mapping file (embedded in the app)
12
  def load_mapping():
13
+ mapping_path = "Levi's Data Mapping.xlsx"
14
+ return pd.read_excel(mapping_path)
 
 
 
15
 
16
  # Function to extract and map data from the input workbook
17
  def transform_data(input_path, mapping_df):
 
19
  input_workbook = pd.ExcelFile(input_path)
20
 
21
  # Initialize a dictionary to store data for output
22
+ output_data = {}
23
 
24
  # Iterate through each mapping rule
25
  for _, row in mapping_df.iterrows():
26
  output_column = row["PO Output Column"]
27
+ input_sheet = row["Sheet Name"]
28
+ input_column = row["Input Column"]
29
+ start_row = row.get("Start Row", 2) # Default start row is 2 if not specified
30
 
31
+ if pd.isna(output_column) or pd.isna(input_sheet) or pd.isna(input_column):
32
  continue
33
 
34
+ # Extract data from the specified sheet and column
35
+ if input_sheet in input_workbook.sheet_names:
36
+ sheet_data = pd.read_excel(input_path, sheet_name=input_sheet, usecols=[input_column], skiprows=start_row - 1)
37
+ output_data[output_column] = sheet_data[input_column].tolist()
38
+ else:
39
+ output_data[output_column] = [] # If sheet is missing, add empty column
40
 
41
+ # Ensure all columns have the same number of rows by filling with blanks
42
+ max_rows = max(len(col_data) for col_data in output_data.values())
 
 
 
 
 
 
 
43
  for key in output_data:
44
+ while len(output_data[key]) < max_rows:
45
+ output_data[key].append("")
46
 
47
  return pd.DataFrame(output_data)
48
 
 
56
  transformed_data = transform_data(input_workbook, mapping_df)
57
 
58
  # Load the output template (embedded in the app)
59
+ output_template_path = "Generated_Output.xlsx"
60
  if not os.path.exists(output_template_path):
61
  return "Output template file is missing."
62
 
63
  output_workbook = load_workbook(output_template_path)
64
+ output_sheet = output_workbook.active
65
 
66
  # Write transformed data to the output sheet
67
  for row_idx, row_data in enumerate(transformed_data.itertuples(index=False), start=2):
 
69
  output_sheet.cell(row=row_idx, column=col_idx, value=value)
70
 
71
  # Save the generated output file
72
+ output_file_path = "Generated_Output_Final.xlsx"
73
  output_workbook.save(output_file_path)
74
 
75
  return output_file_path