Update app.py
Browse files
app.py
CHANGED
@@ -2,13 +2,15 @@ import gradio as gr
|
|
2 |
import pandas as pd
|
3 |
from datetime import datetime
|
4 |
import pytz
|
|
|
5 |
|
6 |
# Updated Gradio app for YunExpress:
|
7 |
# - New output headers (as provided)
|
8 |
# - Email -> Email mapping
|
9 |
# - FOBPrice1 = 2 (replaces UnitPrice1)
|
10 |
-
# - ZIP padding fix for US 4-digit ZIPs
|
11 |
-
# - RoutingCode logic: US/NO -> HK-ASS-PF; others -> HKTHZXR
|
|
|
12 |
|
13 |
def process_file(file):
|
14 |
file_name = file.name.lower()
|
@@ -73,7 +75,7 @@ def process_file(file):
|
|
73 |
if "Shipping Country Code" in df.columns:
|
74 |
output_df["CountryCode"] = df["Shipping Country Code"]
|
75 |
|
76 |
-
# 7) ZipCode (
|
77 |
if "Shipping ZIP" in df.columns:
|
78 |
zip_raw = (
|
79 |
df["Shipping ZIP"]
|
@@ -81,10 +83,16 @@ def process_file(file):
|
|
81 |
.str.strip()
|
82 |
.str.replace(r"\.0$", "", regex=True) # clean "1234.0"
|
83 |
)
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
output_df["
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
# 8) phone
|
90 |
if "Shipping Address Phone" in df.columns:
|
@@ -105,10 +113,10 @@ def process_file(file):
|
|
105 |
# Fixed defaults & RoutingCode
|
106 |
mask = output_df["CustomerOrderNo."].astype(str).str.len() > 0
|
107 |
|
108 |
-
# RoutingCode: HK-ASS-PF if US/NO else HKTHZXR
|
109 |
-
|
110 |
-
mask_other = mask & ~output_df["CountryCode"].isin(["US", "NO"])
|
111 |
-
output_df.loc[
|
112 |
output_df.loc[mask_other, "RoutingCode"] = "HKTHZXR"
|
113 |
|
114 |
# Pricing / descriptions / weights
|
@@ -133,8 +141,26 @@ def process_file(file):
|
|
133 |
today_hk = datetime.now(hk_tz).strftime("%y%m%d")
|
134 |
output_file_name = f"yunexpress {today_hk}.xlsx"
|
135 |
|
136 |
-
# Save to Excel
|
137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
|
139 |
return output_df, output_file_name
|
140 |
|
|
|
2 |
import pandas as pd
|
3 |
from datetime import datetime
|
4 |
import pytz
|
5 |
+
import re
|
6 |
|
7 |
# Updated Gradio app for YunExpress:
|
8 |
# - New output headers (as provided)
|
9 |
# - Email -> Email mapping
|
10 |
# - FOBPrice1 = 2 (replaces UnitPrice1)
|
11 |
+
# - ZIP padding fix for US 3- and 4-digit ZIPs (no leading apostrophe; sanitize non [A-Za-z0-9 ] chars)
|
12 |
+
# - RoutingCode logic: US/NO/FR -> HK-ASS-PF; others -> HKTHZXR
|
13 |
+
# - Save Excel with ZipCode column set to Text (@) to preserve leading zeros without apostrophes
|
14 |
|
15 |
def process_file(file):
|
16 |
file_name = file.name.lower()
|
|
|
75 |
if "Shipping Country Code" in df.columns:
|
76 |
output_df["CountryCode"] = df["Shipping Country Code"]
|
77 |
|
78 |
+
# 7) ZipCode (sanitize; US 3/4-digit -> pad to 5; do NOT add apostrophe)
|
79 |
if "Shipping ZIP" in df.columns:
|
80 |
zip_raw = (
|
81 |
df["Shipping ZIP"]
|
|
|
83 |
.str.strip()
|
84 |
.str.replace(r"\.0$", "", regex=True) # clean "1234.0"
|
85 |
)
|
86 |
+
# Keep only letters, digits, spaces (avoid "only letters, numbers and spaces" API error)
|
87 |
+
zip_clean = zip_raw.str.replace(r"[^A-Za-z0-9 ]+", "", regex=True)
|
88 |
+
|
89 |
+
mask_us = output_df["CountryCode"].eq("US")
|
90 |
+
# For US: if 3 or 4 numeric digits -> zero-fill to 5
|
91 |
+
mask_3_4_digits = zip_clean.str.fullmatch(r"\d{3,4}")
|
92 |
+
zip_padded = zip_clean.where(~(mask_us & mask_3_4_digits), zip_clean.str.zfill(5))
|
93 |
+
|
94 |
+
# Do NOT add any leading apostrophe
|
95 |
+
output_df["ZipCode"] = zip_padded
|
96 |
|
97 |
# 8) phone
|
98 |
if "Shipping Address Phone" in df.columns:
|
|
|
113 |
# Fixed defaults & RoutingCode
|
114 |
mask = output_df["CustomerOrderNo."].astype(str).str.len() > 0
|
115 |
|
116 |
+
# RoutingCode: HK-ASS-PF if US/NO/FR else HKTHZXR
|
117 |
+
mask_us_no_fr = mask & output_df["CountryCode"].isin(["US", "NO", "FR"])
|
118 |
+
mask_other = mask & ~output_df["CountryCode"].isin(["US", "NO", "FR"])
|
119 |
+
output_df.loc[mask_us_no_fr, "RoutingCode"] = "HK-ASS-PF"
|
120 |
output_df.loc[mask_other, "RoutingCode"] = "HKTHZXR"
|
121 |
|
122 |
# Pricing / descriptions / weights
|
|
|
141 |
today_hk = datetime.now(hk_tz).strftime("%y%m%d")
|
142 |
output_file_name = f"yunexpress {today_hk}.xlsx"
|
143 |
|
144 |
+
# Save to Excel with ZipCode column forced to Text (@) using xlsxwriter
|
145 |
+
try:
|
146 |
+
import xlsxwriter
|
147 |
+
from xlsxwriter.utility import xl_col_to_name
|
148 |
+
|
149 |
+
with pd.ExcelWriter(output_file_name, engine="xlsxwriter") as writer:
|
150 |
+
output_df.to_excel(writer, index=False, sheet_name="Sheet1")
|
151 |
+
workbook = writer.book
|
152 |
+
worksheet = writer.sheets["Sheet1"]
|
153 |
+
|
154 |
+
# Text format to preserve leading zeros without adding apostrophes
|
155 |
+
text_fmt = workbook.add_format({"num_format": "@"})
|
156 |
+
|
157 |
+
# Locate ZipCode column and set entire column to text
|
158 |
+
zip_col_idx = output_df.columns.get_loc("ZipCode") # 0-based index
|
159 |
+
col_letter = xl_col_to_name(zip_col_idx)
|
160 |
+
worksheet.set_column(f"{col_letter}:{col_letter}", None, text_fmt)
|
161 |
+
except Exception:
|
162 |
+
# Fallback to default writer if xlsxwriter not available
|
163 |
+
output_df.to_excel(output_file_name, index=False)
|
164 |
|
165 |
return output_df, output_file_name
|
166 |
|