import streamlit as st import pandas as pd import os from io import BytesIO from zipfile import ZipFile html_subject = """

Upload a CSV file

""" st.markdown(html_subject, unsafe_allow_html=True) # Step 1: Upload CSV file uploaded_file = st.file_uploader("", type=["csv"]) if uploaded_file is not None: # Step 2: Read the CSV file into a DataFrame df = pd.read_csv(uploaded_file) if 'Invoice_No' not in df.columns: st.error("The uploaded CSV does not contain the 'Invoice_No' column.") else: # Step 3: Split the DataFrame based on unique Invoice_No values invoice_groups = df.groupby('Invoice_No') # Step 4: Generate CSV files for each Invoice_No and add them to a ZIP archive zip_buffer_csv = BytesIO() with ZipFile(zip_buffer_csv, "a") as zip_file: for invoice_no, group in invoice_groups: csv_buffer = BytesIO() group.to_csv(csv_buffer, index=False) zip_file.writestr(f"{invoice_no}.csv", csv_buffer.getvalue()) zip_buffer_csv.seek(0) # Move to the beginning of the buffer # Step 5: Generate Excel files for each Invoice_No and add them to a ZIP archive zip_buffer_xlsx = BytesIO() with ZipFile(zip_buffer_xlsx, "a") as zip_file: for invoice_no, group in invoice_groups: excel_buffer = BytesIO() with pd.ExcelWriter(excel_buffer, engine='xlsxwriter') as writer: group.to_excel(writer, index=False, sheet_name=str(invoice_no)) excel_buffer.seek(0) zip_file.writestr(f"{invoice_no}.xlsx", excel_buffer.getvalue()) zip_buffer_xlsx.seek(0) # Move to the beginning of the buffer # Step 6: Provide download buttons for both ZIP files (CSV and Excel) button_styles = """ """ st.markdown(button_styles, unsafe_allow_html=True) # Download buttons st.download_button( label="Download Invoice ZIP (CSV)", data=zip_buffer_csv, file_name="invoices_csv.zip", mime="application/zip" ) st.download_button( label="Download Invoice ZIP (Excel)", data=zip_buffer_xlsx, file_name="invoices_xlsx.zip", mime="application/zip" )