-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
125 lines (101 loc) · 5.63 KB
/
main.py
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
import streamlit as st
import streamlit_pandas as sp
import os
import base64
import io
pd = sp.pd
# Function to split the data
def split_data(file):
df = pd.read_excel(file)
col = df["itemdescription"]
warehouse_value = df.loc[0, "Warehouse"]
# Replace '.' with '/' to handle both cases
col = col.str.replace(".", "/", regex=False)
if warehouse_value == "TX1":
st.write("Warehouse TX1 Recognized, creating template...")
new_cols = col.str.split("/", expand=True)
new_cols.rename(columns={0: "manufacturer", 1: "model", 2: "model number", 3: "storage capacity", 4: "color", 5: "carrier", 6: "network lock status", 7: "grade"}, inplace=True)
elif warehouse_value == "APPLE AS IS":
st.write("Warehouse APPLE AS IS Recognized, creating template...")
new_cols = col.str.split("/", expand=True)
new_cols.rename(columns={0: "manufacturer", 1: "model", 2: "model number", 3: "storage capacity", 4: "color", 5: "carrier", 6: "grade"}, inplace=True)
new_cols['network lock status'] = new_cols['carrier']
elif warehouse_value == "SAMSUNG":
st.write("Warehouse SAMSUNG Recognized, creating template...")
new_cols = col.str.split("/", expand=True)
new_cols.rename(columns={0: "manufacturer", 1: "model", 2: "model number", 3: "storage capacity", 4: "color", 5: "carrier", 6: "grade"}, inplace=True)
new_cols['network lock status'] = new_cols['carrier']
elif warehouse_value == "TARGET":
st.write("Warehouse TARGET Recognized, creating template...")
new_cols = col.str.split("/", expand=True)
new_cols.rename(columns={0: "manufacturer", 1: "model", 2: "model number", 3: "storage capacity", 4: "color", 5: "grade"}, inplace=True)
new_cols['network lock status'] = " "
new_cols['carrier'] = " "
elif warehouse_value == "W03-ATT":
st.write("Warehouse W03-ATT Recognized, creating template...")
new_cols = col.str.split("/", expand=True)
new_cols.rename(columns={0: "manufacturer", 1: "model", 2: "model number", 3: "storage capacity", 4: "color", 5: "carrier", 6: "grade"}, inplace=True)
new_cols['network lock status'] = new_cols['carrier']
elif warehouse_value == "W07-USCC":
st.write("Warehouse W07-USCC Recognized, creating template...")
new_cols = col.str.split("/", expand=True)
new_cols.rename(columns={0: "manufacturer", 1: "model", 2: "model number", 3: "storage capacity", 4: "color", 5: "carrier", 6: "grade"}, inplace=True)
new_cols['network lock status'] = new_cols['carrier']
elif warehouse_value == "W08-GGL":
st.write("Warehouse W08-GGL Recognized, creating template...")
new_cols = col.str.split("/", expand=True)
new_cols.rename(columns={0: "manufacturer", 1: "model", 2: "model number", 3: "storage capacity", 4: "color", 5: "carrier", 6: "grade"}, inplace=True)
new_cols['network lock status'] = new_cols['carrier']
else:
st.write("Warehouse not recognized, creating template...")
new_cols = col.str.split("/", expand=True)
new_cols.rename(columns={0: "manufacturer", 1: "model", 2: "model number", 3: "storage capacity", 4: "color", 5: "carrier", 6: "network lock status", 7: "grade"}, inplace=True)
# Concatenate the new columns with the original dataframe
df = pd.concat([df, new_cols], axis=1)
# Remove columns with duplicate names
df = df.loc[:, ~df.columns.duplicated()]
# Add new columns with their respective values
df["QUANTITY"] = 1
df["UOM"] = "Units"
df["TAX"] = "0"
# Rearrange columns in the desired order
df = df[["itemdescription", "QUANTITY", "UOM", "itemdescription", "Unit_Selling_Price", "TAX", "manufacturer", "model", "carrier", "type", "trackingnumber", "grade", "lpn", "model number", "storage capacity", "color", "network lock status", "itemnbr", "serialnumber", "Warehouse", "ordernbr"]]
# Rename columns as needed
df.columns = ["PRODUCT", "QUANTITY", "UOM", "DESCRIPTION", "PRICE", "TAX", "manufacturer", "model", "carrier", "type", "trackingnumber", "grade", "lpn", "model number", "storage capacity", "color", "network lock status", "itemnbr", "serialnumber", "Warehouse", "ordernbr"]
replacements = {
"APPL": "Apple",
"SAMS": "Samsung",
"UNL": "Unlocked",
"GGL": "Google",
}
columns_to_replace = [
"PRODUCT",
"DESCRIPTION",
"manufacturer",
"carrier",
"network lock status",
]
for col_name in columns_to_replace:
df[col_name] = df[col_name].replace(replacements, regex=True)
return df
def get_table_download_link(df, filename):
excel_file = io.BytesIO()
writer = pd.ExcelWriter(excel_file, engine="xlsxwriter")
df.to_excel(writer, index=False, sheet_name="Sheet1")
writer.close()
excel_file.seek(0)
b64 = base64.b64encode(excel_file.read()).decode()
return f'<a href="data:application/octet-stream;base64,{b64}" download="{filename}">Download {filename}</a>'
# Streamlit App
st.title("Data Splitter")
uploaded_file = st.file_uploader("Choose a file", type=["xlsx", "xls"])
if uploaded_file is not None:
st.write("Selected file:")
st.write(uploaded_file.name)
output_filename = os.path.splitext(uploaded_file.name)[0] + "(split).xlsx"
if st.button("Split Data"):
df = split_data(uploaded_file)
st.write(df.head())
st.markdown(get_table_download_link(df, output_filename), unsafe_allow_html=True)
else:
st.write("No file selected.")