joycecast commited on
Commit
f3f679a
·
verified ·
1 Parent(s): 6445858

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -22
app.py CHANGED
@@ -31,7 +31,6 @@ def convert_ci(by_line_item_file, combined_file, password, max_rows):
31
  description = re.sub(r"\d+", "", description)
32
  return re.sub(r"[^\w\s]", "", description)
33
 
34
- # by_line_item_df["Part"] = by_line_item_df.iloc[:, 0].astype(str)
35
  by_line_item_df["Part"] = ""
36
  by_line_item_df["Tariff_Number"] = by_line_item_df.iloc[:, 1].apply(lambda x: str(x).replace('.0', '') if isinstance(x, float) and str(x).endswith('.0') else str(x))
37
  by_line_item_df["Commercial_Description"] = by_line_item_df.apply(clean_desc, axis=1)
@@ -138,37 +137,35 @@ def convert_ci(by_line_item_file, combined_file, password, max_rows):
138
  'CVD_Case_Number', 'AD_Non_Reimbursement_Statement',
139
  'AD-CVD_Certification_Designation'
140
  ]
141
-
142
- # Update SHEIN Incorrect MID:
143
- MID_mapping_df = pd.read_csv("MID_replace.csv")
144
- mid_map = dict(zip(MID_mapping_df["SHEIN_MID_Code"], MID_mapping_df["Replacement"]))
145
- merged_df["MID_Code"] = merged_df["MID_Code"].map(mid_map).fillna(merged_df["MID_Code"])
146
-
147
  for col in column_order:
148
  if col not in merged_df.columns:
149
  merged_df[col] = ""
150
  merged_df = merged_df[column_order]
151
 
152
- # Save merged output partitioned into 998-row chunks
153
  outputs = []
154
-
155
- for i, start in enumerate(range(0, len(merged_df), max_rows)):
156
- chunk = merged_df.iloc[start : start + max_rows]
157
- suffix = chr(65 + i) if i < 26 else f"part{i + 1}" # A, B, C... then part27+
158
  filename = f"{mawb} T01 Manifest - {suffix}.xlsx"
159
- with pd.ExcelWriter(filename, engine='openpyxl') as writer:
160
  chunk.to_excel(writer, sheet_name="Converted", index=False)
 
161
  worksheet = writer.sheets['Converted']
162
- for column_cells in worksheet.columns:
163
- max_length = max(len(str(cell.value)) if cell.value is not None else 0 for cell in column_cells)
164
- col_letter = column_cells[0].column_letter
165
- worksheet.column_dimensions[col_letter].width = max_length + 2
166
- for cell in column_cells:
167
- cell.number_format = "@"
168
  outputs.append(filename)
169
 
170
- return outputs
 
 
 
 
 
171
 
 
172
  # Gradio Interface
173
  iface = gr.Interface(
174
  fn=convert_ci,
@@ -176,9 +173,9 @@ iface = gr.Interface(
176
  gr.File(label="Upload 'By Line Item' File (.xlsx)", type="filepath"),
177
  gr.File(label="Upload 'Combined' File (.xlsx)", type="filepath"),
178
  gr.Textbox(label="Password for 'By Line Item' File", type="password"),
179
- gr.Number(label="Max Rows per Output File", value=350, precision=0)
180
  ],
181
- outputs=gr.File(label="Download Partitioned Files", file_types=[".xlsx"], file_count="multiple"),
182
  title="SHEIN CI to Magaya Format Conversion Tool",
183
  description="Upload both 'By Line Item' (with password) and 'Combined' Excel files to merge and generate the Manifest in Magaya T01 Format."
184
  )
 
31
  description = re.sub(r"\d+", "", description)
32
  return re.sub(r"[^\w\s]", "", description)
33
 
 
34
  by_line_item_df["Part"] = ""
35
  by_line_item_df["Tariff_Number"] = by_line_item_df.iloc[:, 1].apply(lambda x: str(x).replace('.0', '') if isinstance(x, float) and str(x).endswith('.0') else str(x))
36
  by_line_item_df["Commercial_Description"] = by_line_item_df.apply(clean_desc, axis=1)
 
137
  'CVD_Case_Number', 'AD_Non_Reimbursement_Statement',
138
  'AD-CVD_Certification_Designation'
139
  ]
 
 
 
 
 
 
140
  for col in column_order:
141
  if col not in merged_df.columns:
142
  merged_df[col] = ""
143
  merged_df = merged_df[column_order]
144
 
145
+ # Save merged output partitioned by user-defined max_rows
146
  outputs = []
147
+ for i, start in enumerate(range(0, len(merged_df), int(max_rows))):
148
+ chunk = merged_df.iloc[start : start + int(max_rows)]
149
+ suffix = chr(65 + i) if i < 26 else f"part{i + 1}"
 
150
  filename = f"{mawb} T01 Manifest - {suffix}.xlsx"
151
+ with pd.ExcelWriter(filename, engine='xlsxwriter') as writer:
152
  chunk.to_excel(writer, sheet_name="Converted", index=False)
153
+ workbook = writer.book
154
  worksheet = writer.sheets['Converted']
155
+ # Format all cells as text
156
+ text_format = workbook.add_format({'num_format': '@'})
157
+ worksheet.set_column(0, len(chunk.columns) - 1, None, text_format)
158
+
 
 
159
  outputs.append(filename)
160
 
161
+ import zipfile
162
+ zip_name = f"{mawb} T01 Manifest.zip"
163
+ with zipfile.ZipFile(zip_name, 'w') as zipf:
164
+ for file in outputs:
165
+ zipf.write(file, arcname=file)
166
+ return zip_name
167
 
168
+ # Gradio Interface
169
  # Gradio Interface
170
  iface = gr.Interface(
171
  fn=convert_ci,
 
173
  gr.File(label="Upload 'By Line Item' File (.xlsx)", type="filepath"),
174
  gr.File(label="Upload 'Combined' File (.xlsx)", type="filepath"),
175
  gr.Textbox(label="Password for 'By Line Item' File", type="password"),
176
+ gr.Number(label="Max Rows per Output File", value=700, precision=0)
177
  ],
178
+ outputs=gr.File(label="Download Zipped Excel Output", file_types=[".zip"]),
179
  title="SHEIN CI to Magaya Format Conversion Tool",
180
  description="Upload both 'By Line Item' (with password) and 'Combined' Excel files to merge and generate the Manifest in Magaya T01 Format."
181
  )