Ninad077 commited on
Commit
47cb35c
·
verified ·
1 Parent(s): 5548ea9

Upload splitter.py

Browse files
Files changed (1) hide show
  1. splitter.py +27 -7
splitter.py CHANGED
@@ -72,16 +72,28 @@ if uploaded_file is not None:
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 = BytesIO()
76
- with ZipFile(zip_buffer, "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.seek(0) # Move to the beginning of the buffer
83
 
84
- # Step 5: Provide a download button for the ZIP file
 
 
 
 
 
 
 
 
 
 
 
 
85
  button_styles = """
86
  <style>
87
  div.stDownloadButton > button {
@@ -104,9 +116,17 @@ if uploaded_file is not None:
104
  </style>
105
  """
106
  st.markdown(button_styles, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
107
  st.download_button(
108
- label="Download Invoices ZIP",
109
- data=zip_buffer,
110
- file_name="invoices.zip",
111
  mime="application/zip"
112
  )
 
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 {
 
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
  )