Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,13 +8,17 @@ import warnings
|
|
| 8 |
# Suppress openpyxl warnings
|
| 9 |
warnings.filterwarnings("ignore", category=UserWarning, module="openpyxl")
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Function to extract and map data from the input workbook
|
| 17 |
-
def transform_data(input_path
|
| 18 |
# Load the input workbook
|
| 19 |
input_workbook = pd.ExcelFile(input_path)
|
| 20 |
|
|
@@ -22,19 +26,16 @@ def transform_data(input_path, mapping_df):
|
|
| 22 |
output_data = {}
|
| 23 |
|
| 24 |
# Iterate through each mapping rule
|
| 25 |
-
for
|
| 26 |
-
output_column =
|
| 27 |
-
input_sheet =
|
| 28 |
-
input_column =
|
| 29 |
-
start_row =
|
| 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[
|
| 38 |
else:
|
| 39 |
output_data[output_column] = [] # If sheet is missing, add empty column
|
| 40 |
|
|
@@ -53,11 +54,8 @@ def process_files(input_workbook):
|
|
| 53 |
if not os.path.exists(input_workbook):
|
| 54 |
return None, "Input workbook file does not exist."
|
| 55 |
|
| 56 |
-
# Load the constant mapping data
|
| 57 |
-
mapping_df = load_mapping()
|
| 58 |
-
|
| 59 |
# Transform the data
|
| 60 |
-
transformed_data = transform_data(input_workbook
|
| 61 |
|
| 62 |
# Load the output template (embedded in the app)
|
| 63 |
output_template_path = "Generated_Output.xlsx"
|
|
|
|
| 8 |
# Suppress openpyxl warnings
|
| 9 |
warnings.filterwarnings("ignore", category=UserWarning, module="openpyxl")
|
| 10 |
|
| 11 |
+
# Hardcoded mapping logic (from reference file)
|
| 12 |
+
MAPPING = [
|
| 13 |
+
{"Sheet Name": "Sheet1", "Input Column": "A", "PO Output Column": "Order Number", "Start Row": 2},
|
| 14 |
+
{"Sheet Name": "Sheet1", "Input Column": "B", "PO Output Column": "Customer Name", "Start Row": 2},
|
| 15 |
+
{"Sheet Name": "Sheet2", "Input Column": "C", "PO Output Column": "Product Code", "Start Row": 2},
|
| 16 |
+
{"Sheet Name": "Sheet2", "Input Column": "D", "PO Output Column": "Quantity", "Start Row": 2},
|
| 17 |
+
# Add more mappings as needed
|
| 18 |
+
]
|
| 19 |
|
| 20 |
# Function to extract and map data from the input workbook
|
| 21 |
+
def transform_data(input_path):
|
| 22 |
# Load the input workbook
|
| 23 |
input_workbook = pd.ExcelFile(input_path)
|
| 24 |
|
|
|
|
| 26 |
output_data = {}
|
| 27 |
|
| 28 |
# Iterate through each mapping rule
|
| 29 |
+
for mapping in MAPPING:
|
| 30 |
+
output_column = mapping["PO Output Column"]
|
| 31 |
+
input_sheet = mapping["Sheet Name"]
|
| 32 |
+
input_column = mapping["Input Column"]
|
| 33 |
+
start_row = mapping.get("Start Row", 2) # Default start row is 2 if not specified
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# Extract data from the specified sheet and column
|
| 36 |
if input_sheet in input_workbook.sheet_names:
|
| 37 |
sheet_data = pd.read_excel(input_path, sheet_name=input_sheet, usecols=[input_column], skiprows=start_row - 1)
|
| 38 |
+
output_data[output_column] = sheet_data.iloc[:, 0].tolist()
|
| 39 |
else:
|
| 40 |
output_data[output_column] = [] # If sheet is missing, add empty column
|
| 41 |
|
|
|
|
| 54 |
if not os.path.exists(input_workbook):
|
| 55 |
return None, "Input workbook file does not exist."
|
| 56 |
|
|
|
|
|
|
|
|
|
|
| 57 |
# Transform the data
|
| 58 |
+
transformed_data = transform_data(input_workbook)
|
| 59 |
|
| 60 |
# Load the output template (embedded in the app)
|
| 61 |
output_template_path = "Generated_Output.xlsx"
|