Update splitter.py
Browse files- splitter.py +58 -48
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 file</h3>
|
54 |
</div>
|
55 |
</body>
|
56 |
</html>
|
@@ -58,47 +58,56 @@ html_subject = """
|
|
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", "xlsx"])
|
63 |
|
64 |
if uploaded_file is not None:
|
65 |
try:
|
66 |
-
#
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
-
if
|
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 |
<style>
|
103 |
div.stDownloadButton > button {
|
104 |
color: #ffffff; /* Text color */
|
@@ -119,18 +128,19 @@ if uploaded_file is not None:
|
|
119 |
}
|
120 |
</style>
|
121 |
"""
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
|
|
130 |
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
|
|
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 or XLSX file</h3>
|
54 |
</div>
|
55 |
</body>
|
56 |
</html>
|
|
|
58 |
|
59 |
st.markdown(html_subject, unsafe_allow_html=True)
|
60 |
|
61 |
+
# Step 1: Upload CSV or XLSX file
|
62 |
uploaded_file = st.file_uploader("", type=["csv", "xlsx"])
|
63 |
|
64 |
if uploaded_file is not None:
|
65 |
try:
|
66 |
+
# Check file type and read accordingly
|
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 df is not None:
|
81 |
+
if 'Invoice_No' not in df.columns:
|
82 |
+
st.error("The uploaded file does not contain the 'Invoice_No' column.")
|
83 |
+
else:
|
84 |
+
# Step 3: Split the DataFrame based on unique Invoice_No values
|
85 |
+
invoice_groups = df.groupby('Invoice_No')
|
86 |
+
|
87 |
+
# Step 4: Generate CSV files for each Invoice_No and add them to a ZIP archive
|
88 |
+
zip_buffer_csv = BytesIO()
|
89 |
+
with ZipFile(zip_buffer_csv, "a") as zip_file:
|
90 |
+
for invoice_no, group in invoice_groups:
|
91 |
+
csv_buffer = BytesIO()
|
92 |
+
group.to_csv(csv_buffer, index=False)
|
93 |
+
zip_file.writestr(f"{invoice_no}.csv", csv_buffer.getvalue())
|
94 |
|
95 |
+
zip_buffer_csv.seek(0) # Move to the beginning of the buffer
|
96 |
+
|
97 |
+
# Step 5: Generate Excel files for each Invoice_No and add them to a ZIP archive
|
98 |
+
zip_buffer_xlsx = BytesIO()
|
99 |
+
with ZipFile(zip_buffer_xlsx, "a") as zip_file:
|
100 |
+
for invoice_no, group in invoice_groups:
|
101 |
+
excel_buffer = BytesIO()
|
102 |
+
with pd.ExcelWriter(excel_buffer, engine='xlsxwriter') as writer:
|
103 |
+
group.to_excel(writer, index=False, sheet_name=str(invoice_no))
|
104 |
+
excel_buffer.seek(0)
|
105 |
+
zip_file.writestr(f"{invoice_no}.xlsx", excel_buffer.getvalue())
|
106 |
|
107 |
+
zip_buffer_xlsx.seek(0) # Move to the beginning of the buffer
|
108 |
|
109 |
+
# Step 6: Provide download buttons for both ZIP files (CSV and Excel)
|
110 |
+
button_styles = """
|
111 |
<style>
|
112 |
div.stDownloadButton > button {
|
113 |
color: #ffffff; /* Text color */
|
|
|
128 |
}
|
129 |
</style>
|
130 |
"""
|
131 |
+
st.markdown(button_styles, unsafe_allow_html=True)
|
132 |
+
|
133 |
+
# Download buttons
|
134 |
+
st.download_button(
|
135 |
+
label="Download Invoice ZIP (CSV)",
|
136 |
+
data=zip_buffer_csv,
|
137 |
+
file_name="invoices_csv.zip",
|
138 |
+
mime="application/zip"
|
139 |
+
)
|
140 |
|
141 |
+
st.download_button(
|
142 |
+
label="Download Invoice ZIP (Excel)",
|
143 |
+
data=zip_buffer_xlsx,
|
144 |
+
file_name="invoices_xlsx.zip",
|
145 |
+
mime="application/zip"
|
146 |
+
)
|