leadingbridge commited on
Commit
fda2f98
·
verified ·
1 Parent(s): 6dea108

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -11
app.py CHANGED
@@ -61,16 +61,10 @@ def process_file(file):
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 + apostrophe for text)
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
- # pad to 5 digits
70
- zip_padded = zip_raw.where(~(mask_us & mask_4), "0" + zip_raw)
71
- # prefix apostrophe so Excel treats it as text and preserves leading zero
72
- zip_final = zip_padded.where(~(mask_us & mask_4), "'" + zip_padded)
73
- output_df["ZipCode"] = zip_final
74
 
75
  # 7. CountryCode
76
  if "Shipping Country Code" in df.columns:
@@ -116,8 +110,18 @@ def process_file(file):
116
  today_hk = datetime.now(hk_tz).strftime("%y%m%d")
117
  output_file_name = f"yunexpress {today_hk}.xlsx"
118
 
119
- # Save to Excel
120
- output_df.to_excel(output_file_name, index=False)
 
 
 
 
 
 
 
 
 
 
121
 
122
  return output_df, output_file_name
123
 
 
61
  if "Shipping Province" in df.columns:
62
  output_df["Province/State"] = df["Shipping Province"]
63
 
64
+ # 6. ZipCode (pad 4-digit US zips, leave others as-is)
65
  if "Shipping ZIP" in df.columns:
66
+ zip_raw = df["Shipping ZIP"].astype(str).str.zfill(5) # zfill makes '1234' → '01234'
67
+ output_df["ZipCode"] = zip_raw
 
 
 
 
 
 
68
 
69
  # 7. CountryCode
70
  if "Shipping Country Code" in df.columns:
 
110
  today_hk = datetime.now(hk_tz).strftime("%y%m%d")
111
  output_file_name = f"yunexpress {today_hk}.xlsx"
112
 
113
+ # 14. Save to Excel with formatting for ZipCode
114
+ with pd.ExcelWriter(output_file_name, engine='xlsxwriter') as writer:
115
+ output_df.to_excel(writer, index=False, sheet_name='YunExpress')
116
+ workbook = writer.book
117
+ worksheet = writer.sheets['YunExpress']
118
+
119
+ # Find the zero-based column index of "ZipCode"
120
+ zip_col_idx = output_df.columns.get_loc("ZipCode")
121
+ # Create a 5-digit text format
122
+ fmt_text = workbook.add_format({'num_format': '00000', 'align': 'left'})
123
+ # Apply to the entire column
124
+ worksheet.set_column(zip_col_idx, zip_col_idx, 8, fmt_text)
125
 
126
  return output_df, output_file_name
127