Update splitter.py
Browse files- splitter.py +46 -60
splitter.py
CHANGED
@@ -50,7 +50,7 @@ html_subject = """
|
|
50 |
-webkit-text-fill-color: transparent;
|
51 |
margin: 0;
|
52 |
text-shadow: 0 2px 5px rgba(0, 0, 0, 0.4);
|
53 |
-
">Upload a CSV
|
54 |
</div>
|
55 |
</body>
|
56 |
</html>
|
@@ -58,56 +58,43 @@ html_subject = """
|
|
58 |
|
59 |
st.markdown(html_subject, unsafe_allow_html=True)
|
60 |
|
61 |
-
# Step 1: Upload CSV
|
62 |
-
uploaded_file = st.file_uploader("", type=["csv"
|
63 |
|
64 |
if uploaded_file is not None:
|
65 |
-
|
66 |
-
|
67 |
-
if uploaded_file.name.endswith('.csv'):
|
68 |
-
# Try reading the CSV file with utf-8 encoding first
|
69 |
-
df = pd.read_csv(uploaded_file, encoding='utf-8')
|
70 |
-
elif uploaded_file.name.endswith('.xlsx'):
|
71 |
-
# Read the Excel file
|
72 |
-
df = pd.read_excel(uploaded_file, engine='openpyxl')
|
73 |
-
else:
|
74 |
-
st.error("Unsupported file format. Please upload a CSV or XLSX file.")
|
75 |
-
df = None
|
76 |
-
except Exception as e:
|
77 |
-
st.error(f"An error occurred while reading the file: {e}")
|
78 |
-
df = None
|
79 |
|
80 |
-
if
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
zip_file.writestr(f"{invoice_no}.csv", csv_buffer.getvalue())
|
94 |
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
|
107 |
-
|
108 |
|
109 |
-
|
110 |
-
|
111 |
<style>
|
112 |
div.stDownloadButton > button {
|
113 |
color: #ffffff; /* Text color */
|
@@ -128,19 +115,18 @@ if uploaded_file is not None:
|
|
128 |
}
|
129 |
</style>
|
130 |
"""
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
)
|
140 |
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
|
|
50 |
-webkit-text-fill-color: transparent;
|
51 |
margin: 0;
|
52 |
text-shadow: 0 2px 5px rgba(0, 0, 0, 0.4);
|
53 |
+
">Upload a CSV file</h3>
|
54 |
</div>
|
55 |
</body>
|
56 |
</html>
|
|
|
58 |
|
59 |
st.markdown(html_subject, unsafe_allow_html=True)
|
60 |
|
61 |
+
# Step 1: Upload CSV file
|
62 |
+
uploaded_file = st.file_uploader("", type=["csv"])
|
63 |
|
64 |
if uploaded_file is not None:
|
65 |
+
# Step 2: Read the CSV file into a DataFrame
|
66 |
+
df = pd.read_csv(uploaded_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
if 'Invoice_No' not in df.columns:
|
69 |
+
st.error("The uploaded CSV does not contain the 'Invoice_No' column.")
|
70 |
+
else:
|
71 |
+
# Step 3: Split the DataFrame based on unique Invoice_No values
|
72 |
+
invoice_groups = df.groupby('Invoice_No')
|
73 |
+
|
74 |
+
# Step 4: Generate CSV files for each Invoice_No and add them to a ZIP archive
|
75 |
+
zip_buffer_csv = BytesIO()
|
76 |
+
with ZipFile(zip_buffer_csv, "a") as zip_file:
|
77 |
+
for invoice_no, group in invoice_groups:
|
78 |
+
csv_buffer = BytesIO()
|
79 |
+
group.to_csv(csv_buffer, index=False)
|
80 |
+
zip_file.writestr(f"{invoice_no}.csv", csv_buffer.getvalue())
|
|
|
81 |
|
82 |
+
zip_buffer_csv.seek(0) # Move to the beginning of the buffer
|
83 |
+
|
84 |
+
# Step 5: Generate Excel files for each Invoice_No and add them to a ZIP archive
|
85 |
+
zip_buffer_xlsx = BytesIO()
|
86 |
+
with ZipFile(zip_buffer_xlsx, "a") as zip_file:
|
87 |
+
for invoice_no, group in invoice_groups:
|
88 |
+
excel_buffer = BytesIO()
|
89 |
+
with pd.ExcelWriter(excel_buffer, engine='xlsxwriter') as writer:
|
90 |
+
group.to_excel(writer, index=False, sheet_name=str(invoice_no))
|
91 |
+
excel_buffer.seek(0)
|
92 |
+
zip_file.writestr(f"{invoice_no}.xlsx", excel_buffer.getvalue())
|
93 |
|
94 |
+
zip_buffer_xlsx.seek(0) # Move to the beginning of the buffer
|
95 |
|
96 |
+
# Step 6: Provide download buttons for both ZIP files (CSV and Excel)
|
97 |
+
button_styles = """
|
98 |
<style>
|
99 |
div.stDownloadButton > button {
|
100 |
color: #ffffff; /* Text color */
|
|
|
115 |
}
|
116 |
</style>
|
117 |
"""
|
118 |
+
st.markdown(button_styles, unsafe_allow_html=True)
|
119 |
+
# Download buttons
|
120 |
+
st.download_button(
|
121 |
+
label="Download Invoice ZIP (CSV)",
|
122 |
+
data=zip_buffer_csv,
|
123 |
+
file_name="invoices_csv.zip",
|
124 |
+
mime="application/zip"
|
125 |
+
)
|
|
|
126 |
|
127 |
+
st.download_button(
|
128 |
+
label="Download Invoice ZIP (Excel)",
|
129 |
+
data=zip_buffer_xlsx,
|
130 |
+
file_name="invoices_xlsx.zip",
|
131 |
+
mime="application/zip"
|
132 |
+
)
|