Ninad077 commited on
Commit
4699bb6
·
verified ·
1 Parent(s): 36eaceb

Update splitter.py

Browse files
Files changed (1) hide show
  1. 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 or XLSX file</h3>
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 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,19 +115,18 @@ if uploaded_file is not None:
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
- )
 
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
+ )