Ninad077 commited on
Commit
36eaceb
·
verified ·
1 Parent(s): 4f58b36

Update splitter.py

Browse files
Files changed (1) hide show
  1. 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
- # Try reading the CSV file with utf-8 encoding first
67
- df = pd.read_csv(uploaded_file, encoding='utf-8')
68
- except UnicodeDecodeError:
69
- # Fallback to ISO-8859-1 encoding if utf-8 fails
70
- df = pd.read_csv(uploaded_file, encoding='ISO-8859-1')
 
 
 
 
 
 
 
 
71
 
72
- if 'Invoice_No' not in df.columns:
73
- st.error("The uploaded CSV does not contain the 'Invoice_No' column.")
74
- else:
75
- # Step 3: Split the DataFrame based on unique Invoice_No values
76
- invoice_groups = df.groupby('Invoice_No')
77
-
78
- # Step 4: Generate CSV files for each Invoice_No and add them to a ZIP archive
79
- zip_buffer_csv = BytesIO()
80
- with ZipFile(zip_buffer_csv, "a") as zip_file:
81
- for invoice_no, group in invoice_groups:
82
- csv_buffer = BytesIO()
83
- group.to_csv(csv_buffer, index=False)
84
- zip_file.writestr(f"{invoice_no}.csv", csv_buffer.getvalue())
 
85
 
86
- zip_buffer_csv.seek(0) # Move to the beginning of the buffer
87
-
88
- # Step 5: Generate Excel files for each Invoice_No and add them to a ZIP archive
89
- zip_buffer_xlsx = BytesIO()
90
- with ZipFile(zip_buffer_xlsx, "a") as zip_file:
91
- for invoice_no, group in invoice_groups:
92
- excel_buffer = BytesIO()
93
- with pd.ExcelWriter(excel_buffer, engine='xlsxwriter') as writer:
94
- group.to_excel(writer, index=False, sheet_name=str(invoice_no))
95
- excel_buffer.seek(0)
96
- zip_file.writestr(f"{invoice_no}.xlsx", excel_buffer.getvalue())
97
 
98
- zip_buffer_xlsx.seek(0) # Move to the beginning of the buffer
99
 
100
- # Step 6: Provide download buttons for both ZIP files (CSV and Excel)
101
- button_styles = """
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
- st.markdown(button_styles, unsafe_allow_html=True)
123
- # Download buttons
124
- st.download_button(
125
- label="Download Invoice ZIP (CSV)",
126
- data=zip_buffer_csv,
127
- file_name="invoices_csv.zip",
128
- mime="application/zip"
129
- )
 
130
 
131
- st.download_button(
132
- label="Download Invoice ZIP (Excel)",
133
- data=zip_buffer_xlsx,
134
- file_name="invoices_xlsx.zip",
135
- mime="application/zip"
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
+ )