Update app.py
Browse files
app.py
CHANGED
@@ -10,7 +10,7 @@ def process_file(file):
|
|
10 |
try:
|
11 |
if file_name.endswith(('.xls', '.xlsx', '.xlsm')):
|
12 |
# Read data from the "YunExpress" sheet
|
13 |
-
df = pd.read_excel(file
|
14 |
else:
|
15 |
return f"Unsupported file format: {file_name}", None
|
16 |
except Exception as e:
|
@@ -61,9 +61,13 @@ def process_file(file):
|
|
61 |
if "Shipping Province" in df.columns:
|
62 |
output_df["Province/State"] = df["Shipping Province"]
|
63 |
|
64 |
-
# 6. ZipCode
|
65 |
if "Shipping ZIP" in df.columns:
|
66 |
-
|
|
|
|
|
|
|
|
|
67 |
|
68 |
# 7. CountryCode
|
69 |
if "Shipping Country Code" in df.columns:
|
@@ -83,30 +87,28 @@ def process_file(file):
|
|
83 |
|
84 |
# 11. Fixed defaults and conditional RoutingCode
|
85 |
mask = output_df["CustomerOrderNo."].notna() & (output_df["CustomerOrderNo."] != "")
|
86 |
-
# Norway code
|
87 |
mask_norway = mask & (output_df["CountryCode"] == "NO")
|
88 |
mask_other = mask & (output_df["CountryCode"] != "NO")
|
89 |
-
|
90 |
output_df.loc[mask_other, "RoutingCode"] = "HKTHZXR"
|
91 |
output_df.loc[mask_norway, "RoutingCode"] = "HK-ASS-PF"
|
92 |
-
|
93 |
output_df.loc[mask, "UnitPrice1"] = 2
|
94 |
output_df.loc[mask, "CurrencyCode"] = "USD"
|
95 |
output_df.loc[mask, "ItemDescription1"] = "Eye Cosmetic Accessories"
|
96 |
output_df.loc[mask, "UnitWeight1"] = 0.02
|
97 |
-
# ForeignItemDescription1
|
98 |
output_df.loc[mask, "ForeignItemDescription1"] = "Eye Cosmetic Accessories"
|
99 |
-
|
100 |
EU_COUNTRIES = {"AT","BE","BG","CY","CZ","DE","DK","EE","ES","FI",
|
101 |
"FR","HR","HU","IE","IT","LT","LU","LV","MT","NL",
|
102 |
"PL","PT","RO","SE","SI","SK","GR"}
|
103 |
mask_eu = mask & output_df["CountryCode"].isin(EU_COUNTRIES)
|
104 |
-
output_df.loc[mask_eu, "AdditionalServices"]
|
105 |
|
106 |
# 12. Remove duplicate rows based on CustomerOrderNo.
|
107 |
output_df = output_df.drop_duplicates(subset=["CustomerOrderNo."], keep="first")
|
108 |
|
109 |
-
# 13. Generate output filename using
|
110 |
hk_tz = pytz.timezone("Asia/Hong_Kong")
|
111 |
today_hk = datetime.now(hk_tz).strftime("%y%m%d")
|
112 |
output_file_name = f"yunexpress {today_hk}.xlsx"
|
@@ -127,7 +129,7 @@ with gr.Blocks(title="Shipping - YunExpress") as demo:
|
|
127 |
output_file_component = gr.File(label="Download Processed File")
|
128 |
process_button.click(fn=process_file, inputs=[file_input], outputs=[output_data, output_file_component])
|
129 |
|
130 |
-
#
|
131 |
gr.HTML(
|
132 |
"""
|
133 |
<div style="text-align: center; font-size: 16px; margin-top: 20px;">
|
@@ -147,4 +149,4 @@ with gr.Blocks(title="Shipping - YunExpress") as demo:
|
|
147 |
"""
|
148 |
)
|
149 |
|
150 |
-
demo.launch()
|
|
|
10 |
try:
|
11 |
if file_name.endswith(('.xls', '.xlsx', '.xlsm')):
|
12 |
# Read data from the "YunExpress" sheet
|
13 |
+
df = pd.read_excel(file, sheet_name="YunExpress")
|
14 |
else:
|
15 |
return f"Unsupported file format: {file_name}", None
|
16 |
except Exception as e:
|
|
|
61 |
if "Shipping Province" in df.columns:
|
62 |
output_df["Province/State"] = df["Shipping Province"]
|
63 |
|
64 |
+
# 6. ZipCode (pad 4-digit US zips with a leading zero)
|
65 |
if "Shipping ZIP" in df.columns:
|
66 |
+
zip_raw = df["Shipping ZIP"].astype(str).str.strip()
|
67 |
+
mask_us = output_df["CountryCode"] == "US"
|
68 |
+
mask_4 = zip_raw.str.len() == 4
|
69 |
+
zip_fixed = zip_raw.where(~(mask_us & mask_4), "0" + zip_raw)
|
70 |
+
output_df["ZipCode"] = zip_fixed
|
71 |
|
72 |
# 7. CountryCode
|
73 |
if "Shipping Country Code" in df.columns:
|
|
|
87 |
|
88 |
# 11. Fixed defaults and conditional RoutingCode
|
89 |
mask = output_df["CustomerOrderNo."].notna() & (output_df["CustomerOrderNo."] != "")
|
|
|
90 |
mask_norway = mask & (output_df["CountryCode"] == "NO")
|
91 |
mask_other = mask & (output_df["CountryCode"] != "NO")
|
92 |
+
|
93 |
output_df.loc[mask_other, "RoutingCode"] = "HKTHZXR"
|
94 |
output_df.loc[mask_norway, "RoutingCode"] = "HK-ASS-PF"
|
95 |
+
|
96 |
output_df.loc[mask, "UnitPrice1"] = 2
|
97 |
output_df.loc[mask, "CurrencyCode"] = "USD"
|
98 |
output_df.loc[mask, "ItemDescription1"] = "Eye Cosmetic Accessories"
|
99 |
output_df.loc[mask, "UnitWeight1"] = 0.02
|
|
|
100 |
output_df.loc[mask, "ForeignItemDescription1"] = "Eye Cosmetic Accessories"
|
101 |
+
|
102 |
EU_COUNTRIES = {"AT","BE","BG","CY","CZ","DE","DK","EE","ES","FI",
|
103 |
"FR","HR","HU","IE","IT","LT","LU","LV","MT","NL",
|
104 |
"PL","PT","RO","SE","SI","SK","GR"}
|
105 |
mask_eu = mask & output_df["CountryCode"].isin(EU_COUNTRIES)
|
106 |
+
output_df.loc[mask_eu, "AdditionalServices"] = "v1"
|
107 |
|
108 |
# 12. Remove duplicate rows based on CustomerOrderNo.
|
109 |
output_df = output_df.drop_duplicates(subset=["CustomerOrderNo."], keep="first")
|
110 |
|
111 |
+
# 13. Generate output filename using Hong Kong date
|
112 |
hk_tz = pytz.timezone("Asia/Hong_Kong")
|
113 |
today_hk = datetime.now(hk_tz).strftime("%y%m%d")
|
114 |
output_file_name = f"yunexpress {today_hk}.xlsx"
|
|
|
129 |
output_file_component = gr.File(label="Download Processed File")
|
130 |
process_button.click(fn=process_file, inputs=[file_input], outputs=[output_data, output_file_component])
|
131 |
|
132 |
+
# Links to other tools
|
133 |
gr.HTML(
|
134 |
"""
|
135 |
<div style="text-align: center; font-size: 16px; margin-top: 20px;">
|
|
|
149 |
"""
|
150 |
)
|
151 |
|
152 |
+
demo.launch()
|