Spaces:
Sleeping
Sleeping
File size: 7,756 Bytes
25b1716 00afa2e 25b1716 00afa2e 25b1716 00afa2e 25b1716 00afa2e 25b1716 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | import gradio as gr
import pandas as pd
import openpyxl
import os
# -------------------------
# GLOBAL VARIABLES
# -------------------------
merged_data = pd.DataFrame()
total_ads_cost = 0.0
# ============================================================
# STEP 1 β USER UPLOADS EXCEL FILES β GENERATE SHEET2
# ============================================================
def step1_generate_sheet2(files):
global merged_data, total_ads_cost
if not files:
return None, "β Please upload at least one Excel file."
merged_data = pd.DataFrame()
total_ads_cost = 0.0
for f in files:
try:
# Gradio 6.x gives file path directly
filepath = str(f)
df = pd.read_excel(
filepath,
sheet_name="Order Payments",
header=1,
skiprows=[2]
)
df["Source_File"] = os.path.basename(filepath)
merged_data = pd.concat(
[merged_data, df],
ignore_index=True
)
# -------------------------
# Ads Cost (3rd Sheet)
# -------------------------
ads_df = pd.read_excel(
filepath,
sheet_name=2,
skiprows=2
)
ads_total = pd.to_numeric(
ads_df.iloc[:, 7],
errors="coerce"
).sum()
total_ads_cost += ads_total
except Exception as e:
print(f"Skipping {filepath}: {e}")
# -------------------------
# Filter Required Statuses
# -------------------------
# Auto detect status column
status_column = None
for col in merged_data.columns:
if "status" in str(col).lower():
status_column = col
break
if status_column is None:
return None, f"β Status column not found.\nColumns: {list(merged_data.columns)}"
# Auto detect SKU column
sku_column = None
for col in merged_data.columns:
if "sku" in str(col).lower():
sku_column = col
break
if sku_column is None:
return None, f"β SKU column not found.\nColumns: {list(merged_data.columns)}"
status_keep = [
"Delivered",
"Return",
"Exchange",
"Shipped"
]
merged_data = merged_data[
merged_data[status_column].astype(str).isin(status_keep)
]
merged_data = merged_data.sort_values(by=sku_column)
# -------------------------
# Create Sheet2
# -------------------------
sheet2 = pd.DataFrame()
sheet2["Supplier SKU"] = (
merged_data[sku_column]
.dropna()
.unique()
)
sheet2["Cost"] = ""
sheet2["Final Settlement Amount"] = ""
sheet2["Final Purchase"] = ""
sheet2["Total Sale"] = ""
filename = "Sheet2_Fill_Cost.xlsx"
sheet2.to_excel(
filename,
index=False
)
return (
filename,
"β
Sheet2 created! Download and fill COST column."
)
# ============================================================
# STEP 2 β USER UPLOADS FILLED SHEET2 β FINAL REPORT
# ============================================================
def step2_generate_final(sheet2_file):
global merged_data, total_ads_cost
if merged_data.empty:
return None, "β You must complete Step 1 first."
if sheet2_file is None:
return None, "β Upload the filled Sheet2 Excel file."
filepath = str(sheet2_file)
sheet2_filled = pd.read_excel(filepath)
# -------------------------
# Merge Cost
# -------------------------
final_sheet1 = merged_data.merge(
sheet2_filled[
["Supplier SKU", "Cost"]
],
on="Supplier SKU",
how="left"
)
final_sheet1["Cost"] = pd.to_numeric(
final_sheet1["Cost"],
errors="coerce"
).fillna(0)
final_sheet1["Quantity"] = pd.to_numeric(
final_sheet1["Quantity"],
errors="coerce"
).fillna(0)
# Purchase
final_sheet1["Purchase"] = (
final_sheet1["Cost"]
* final_sheet1["Quantity"]
)
# -------------------------
# Summary Sheet
# -------------------------
sheet2_final = (
final_sheet1.groupby("Supplier SKU")
.agg({
"Final Settlement Amount": "sum",
"Purchase": "sum"
})
.reset_index()
)
sheet2_final["Total Sale"] = (
sheet2_final["Final Settlement Amount"]
- sheet2_final["Purchase"]
)
sheet2_final = sheet2_final.sort_values(
by="Total Sale"
)
# -------------------------
# Ads Cost
# -------------------------
sheet2_final["Ads Cost"] = 0.0
if not sheet2_final.empty:
sheet2_final.loc[
sheet2_final.index[0],
"Ads Cost"
] = round(total_ads_cost, 2)
# Rename Columns
sheet2_final.columns = [
"Supplier SKU",
"Final Settlement Amount",
"Final Purchase",
"Total Sale",
"Ads Cost"
]
sheet2_final.insert(
1,
"Cost",
sheet2_filled["Cost"]
)
# -------------------------
# Save Output
# -------------------------
output_file = "Final_Meesho_Report.xlsx"
with pd.ExcelWriter(output_file) as writer:
final_sheet1.to_excel(
writer,
sheet_name="Sheet1",
index=False
)
sheet2_final.to_excel(
writer,
sheet_name="Sheet2",
index=False
)
# -------------------------
# Excel Formula
# -------------------------
wb = openpyxl.load_workbook(output_file)
ws = wb["Sheet1"]
ws["M1"] = "Purchase"
last_row = ws.max_row
for row in range(2, last_row + 1):
ws[f"M{row}"] = (
f'=VLOOKUP(E{row},Sheet2!$A:$B,2,FALSE)*I{row}'
)
wb.save(output_file)
return (
output_file,
"π Final Profit Report generated successfully!"
)
# ============================================================
# UI
# ============================================================
with gr.Blocks(title="MeeProfit Analyzer") as app:
gr.Markdown(
"# π MeeProfit Analyzer\n"
"Smart Excel Profit Automation for Meesho Sellers"
)
# STEP 1
with gr.Tab("β¨ Step 1 β Create Cost Sheet"):
gr.Markdown(
"""
Upload Meesho Order Payments files.
Download generated Sheet2 and fill Cost column.
"""
)
files_step1 = gr.File(
file_count="multiple",
label="π Upload Excel Files"
)
btn1 = gr.Button(
"π Generate Sheet2"
)
sheet2_download = gr.File(
label="β¬οΈ Download Sheet2"
)
msg1 = gr.Markdown()
btn1.click(
step1_generate_sheet2,
inputs=files_step1,
outputs=[
sheet2_download,
msg1
]
)
# STEP 2
with gr.Tab("π Step 2 β Generate Final Report"):
gr.Markdown(
"""
Upload filled Sheet2 file.
"""
)
file_step2 = gr.File(
label="π Upload Filled Sheet2"
)
btn2 = gr.Button(
"π Generate Final Report"
)
final_download = gr.File(
label="β¬οΈ Download Final Report"
)
msg2 = gr.Markdown()
btn2.click(
step2_generate_final,
inputs=file_step2,
outputs=[
final_download,
msg2
]
)
app.launch() |