Esben922 commited on
Commit
039d353
·
verified ·
1 Parent(s): a37c10f

Upload 4 files

Browse files
Files changed (4) hide show
  1. BG.py +80 -0
  2. GB-GB.py +65 -0
  3. MultiL.py +45 -0
  4. app.py +24 -65
BG.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ from io import BytesIO
4
+
5
+ def split_excel(file, columns, original_file_name, sheet_name="Linjer", header_row=3):
6
+ try:
7
+ # Load only the "Linjer" sheet
8
+ df = pd.read_excel(file, sheet_name=sheet_name, header=header_row-1) # Adjust for zero-based index
9
+ log_messages = [f"Loaded worksheet '{sheet_name}'. Other worksheets were ignored."]
10
+ except ValueError:
11
+ return {}, [f"Worksheet '{sheet_name}' not found. Please upload a file with the correct structure."]
12
+ except Exception as e:
13
+ return {}, [f"An error occurred while reading the file: {e}"]
14
+
15
+ split_files = {}
16
+ all_unique_values = set()
17
+
18
+ # Validate that both columns exist in the dataframe
19
+ for col in columns:
20
+ if col not in df.columns:
21
+ log_messages.append(f"Column '{col}' not found in the worksheet. Please check the file.")
22
+ return {}, log_messages
23
+
24
+ # Collect all unique values across both columns
25
+ for col in columns:
26
+ unique_values = df[col].dropna().unique()
27
+ log_messages.append(f"Found unique values in column '{col}': {list(unique_values)}")
28
+ all_unique_values.update(unique_values)
29
+
30
+ log_messages.append(f"Total unique values across columns: {list(all_unique_values)}")
31
+
32
+ # Create files for each unique value
33
+ for value in all_unique_values:
34
+ split_df = df[(df[columns[0]] == value) | (df[columns[1]] == value)]
35
+ if split_df.empty:
36
+ log_messages.append(f"No rows found for value '{value}'. Skipping file creation.")
37
+ continue
38
+
39
+ sanitized_value = str(value).replace("/", "-").replace("\\", "-")
40
+ file_name = f"{original_file_name} - {sanitized_value}.xlsx"
41
+
42
+ output = BytesIO()
43
+ with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
44
+ split_df.to_excel(writer, index=False, sheet_name='Sheet1')
45
+ output.seek(0)
46
+ split_files[file_name] = output
47
+ log_messages.append(f"File '{file_name}' created with {len(split_df)} rows.")
48
+
49
+ return split_files, log_messages
50
+
51
+ st.title("BG File Splitter")
52
+ st.write("Upload an Excel file to split it based on unique values in specified columns.")
53
+
54
+ uploaded_file = st.file_uploader("Upload Excel File", type=["xlsx"])
55
+
56
+ if uploaded_file:
57
+ st.write("Processing the uploaded file...")
58
+ original_file_name = uploaded_file.name.rsplit(".", 1)[0] # Extract the file name without extension
59
+ columns_to_split = ['Afsenderkunde', 'Modtagerkunde'] # Column names to check for unique values
60
+
61
+ split_files, logs = split_excel(uploaded_file, columns_to_split, original_file_name)
62
+
63
+ st.write("### Logs")
64
+ if logs:
65
+ for log in logs:
66
+ st.write(log)
67
+ else:
68
+ st.write("No logs available. Something might have gone wrong.")
69
+
70
+ if split_files:
71
+ st.write("### Download Split Files")
72
+ for file_name, file_content in split_files.items():
73
+ st.download_button(
74
+ label=f"Download {file_name}",
75
+ data=file_content,
76
+ file_name=file_name,
77
+ mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
78
+ )
79
+ else:
80
+ st.write("No files were created. Check the logs for details.")
GB-GB.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ from io import BytesIO
4
+
5
+ # Streamlit App
6
+ st.title("GB-GB Split Multiple CSVs by Account Numbers")
7
+
8
+ # Allow multiple files to be uploaded
9
+ uploaded_files = st.file_uploader("Upload your CSV files", type=["csv"], accept_multiple_files=True)
10
+
11
+ if uploaded_files:
12
+ for uploaded_file in uploaded_files:
13
+ # Read each CSV file and get the original file name
14
+ original_file_name = uploaded_file.name.split(".")[0]
15
+ st.write(f"Processing file: {uploaded_file.name}")
16
+ data = pd.read_csv(uploaded_file)
17
+ st.write(f"File '{uploaded_file.name}' loaded successfully.")
18
+
19
+ # Check required columns
20
+ required_columns = ["SHIPPER ACCOUNT NUMBER", "RECEIVER ACCOUNT NUMBER"]
21
+ if not all(column in data.columns for column in required_columns):
22
+ st.error(f"File '{uploaded_file.name}' is missing required columns: {required_columns}")
23
+ continue
24
+
25
+ st.write(f"Columns verified for '{uploaded_file.name}'. Processing the data...")
26
+
27
+ # Extract unique values from the columns
28
+ unique_values = set(data["SHIPPER ACCOUNT NUMBER"].dropna().unique()) | set(
29
+ data["RECEIVER ACCOUNT NUMBER"].dropna().unique()
30
+ )
31
+ st.write(f"Found {len(unique_values)} unique values in '{uploaded_file.name}'.")
32
+
33
+ # Process each unique value
34
+ for unique_value in unique_values:
35
+ # Filter data for the unique value
36
+ filtered_data = data[
37
+ (data["SHIPPER ACCOUNT NUMBER"] == unique_value)
38
+ | (data["RECEIVER ACCOUNT NUMBER"] == unique_value)
39
+ ]
40
+
41
+ # Determine where the unique value was found
42
+ found_in_columns = []
43
+ if (data["SHIPPER ACCOUNT NUMBER"] == unique_value).any():
44
+ found_in_columns.append("SHIPPER ACCOUNT NUMBER")
45
+ if (data["RECEIVER ACCOUNT NUMBER"] == unique_value).any():
46
+ found_in_columns.append("RECEIVER ACCOUNT NUMBER")
47
+
48
+ # Log details
49
+ st.write(
50
+ f"Processed {unique_value}: {len(filtered_data)} rows found in columns {', '.join(found_in_columns)}."
51
+ )
52
+
53
+ # Save the filtered data to an Excel file in memory
54
+ output = BytesIO()
55
+ with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
56
+ filtered_data.to_excel(writer, index=False, sheet_name="Sheet1")
57
+ output.seek(0)
58
+
59
+ # Provide a download link for each Excel file
60
+ st.download_button(
61
+ label=f"Download Excel for {uploaded_file.name} - {unique_value}",
62
+ data=output,
63
+ file_name=f"{original_file_name} - {unique_value}.xlsx",
64
+ mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
65
+ )
MultiL.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+
4
+ st.title("MultiL Split Excel File by Column Value (4-digit formatting)")
5
+
6
+ # File uploader
7
+ uploaded_file = st.file_uploader("Upload an Excel file", type=["xlsx"])
8
+
9
+ if uploaded_file:
10
+ # Load Excel file with the header starting at row 3 (Python's 0-index is row 2)
11
+ df = pd.read_excel(uploaded_file, header=2)
12
+
13
+ # Ensure the column exists
14
+ if "Afsenderkunde løbenumme" in df.columns:
15
+ st.write(f"File uploaded: {uploaded_file.name}")
16
+ unique_values = df["Afsenderkunde løbenumme"].unique()
17
+ st.write(f"Found {len(unique_values)} unique values in 'Afsenderkunde løbenumme'.")
18
+
19
+ # Iterate through unique values to split the file
20
+ for value in unique_values:
21
+ formatted_value = f"{int(value):04}" # Format as 4-digit integer
22
+ split_df = df[df["Afsenderkunde løbenumme"] == value]
23
+ output_filename = f"{uploaded_file.name.split('.')[0]} - {formatted_value}.xlsx"
24
+
25
+ # Convert DataFrame to Excel file for download
26
+ @st.cache_data
27
+ def convert_df_to_excel(dataframe):
28
+ from io import BytesIO
29
+ output = BytesIO()
30
+ with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
31
+ dataframe.to_excel(writer, index=False, sheet_name="Sheet1")
32
+ output.seek(0)
33
+ return output
34
+
35
+ excel_data = convert_df_to_excel(split_df)
36
+
37
+ # Download button for each file
38
+ st.download_button(
39
+ label=f"Download {output_filename}",
40
+ data=excel_data,
41
+ file_name=output_filename,
42
+ mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
43
+ )
44
+ else:
45
+ st.error("The column 'Afsenderkunde løbenumme' was not found in the uploaded file.")
app.py CHANGED
@@ -1,65 +1,24 @@
1
- import streamlit as st
2
- import pandas as pd
3
- from io import BytesIO
4
-
5
- # Streamlit App
6
- st.title("Split Multiple CSVs by Account Numbers")
7
-
8
- # Allow multiple files to be uploaded
9
- uploaded_files = st.file_uploader("Upload your CSV files", type=["csv"], accept_multiple_files=True)
10
-
11
- if uploaded_files:
12
- for uploaded_file in uploaded_files:
13
- # Read each CSV file and get the original file name
14
- original_file_name = uploaded_file.name.split(".")[0]
15
- st.write(f"Processing file: {uploaded_file.name}")
16
- data = pd.read_csv(uploaded_file)
17
- st.write(f"File '{uploaded_file.name}' loaded successfully.")
18
-
19
- # Check required columns
20
- required_columns = ["SHIPPER ACCOUNT NUMBER", "RECEIVER ACCOUNT NUMBER"]
21
- if not all(column in data.columns for column in required_columns):
22
- st.error(f"File '{uploaded_file.name}' is missing required columns: {required_columns}")
23
- continue
24
-
25
- st.write(f"Columns verified for '{uploaded_file.name}'. Processing the data...")
26
-
27
- # Extract unique values from the columns
28
- unique_values = set(data["SHIPPER ACCOUNT NUMBER"].dropna().unique()) | set(
29
- data["RECEIVER ACCOUNT NUMBER"].dropna().unique()
30
- )
31
- st.write(f"Found {len(unique_values)} unique values in '{uploaded_file.name}'.")
32
-
33
- # Process each unique value
34
- for unique_value in unique_values:
35
- # Filter data for the unique value
36
- filtered_data = data[
37
- (data["SHIPPER ACCOUNT NUMBER"] == unique_value)
38
- | (data["RECEIVER ACCOUNT NUMBER"] == unique_value)
39
- ]
40
-
41
- # Determine where the unique value was found
42
- found_in_columns = []
43
- if (data["SHIPPER ACCOUNT NUMBER"] == unique_value).any():
44
- found_in_columns.append("SHIPPER ACCOUNT NUMBER")
45
- if (data["RECEIVER ACCOUNT NUMBER"] == unique_value).any():
46
- found_in_columns.append("RECEIVER ACCOUNT NUMBER")
47
-
48
- # Log details
49
- st.write(
50
- f"Processed {unique_value}: {len(filtered_data)} rows found in columns {', '.join(found_in_columns)}."
51
- )
52
-
53
- # Save the filtered data to an Excel file in memory
54
- output = BytesIO()
55
- with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
56
- filtered_data.to_excel(writer, index=False, sheet_name="Sheet1")
57
- output.seek(0)
58
-
59
- # Provide a download link for each Excel file
60
- st.download_button(
61
- label=f"Download Excel for {uploaded_file.name} - {unique_value}",
62
- data=output,
63
- file_name=f"{original_file_name} - {unique_value}.xlsx",
64
- mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
65
- )
 
1
+ import streamlit as st
2
+
3
+ # Define the menu structure with labels and their associated script names
4
+ menu = {
5
+ "GB-GB": "GB-GB.py",
6
+ "BG": "BG.py",
7
+ "MultiL": "MultiL.py",
8
+ }
9
+
10
+ # Sidebar title
11
+ st.sidebar.title("Navigation")
12
+
13
+ # Sidebar menu implementation
14
+ selected_script = None
15
+ for label, script in menu.items():
16
+ if st.sidebar.button(label):
17
+ selected_script = script
18
+
19
+ # Default script on app load
20
+ if selected_script is None:
21
+ selected_script = "GB-GB.py"
22
+
23
+ # Execute the selected script
24
+ exec(open(selected_script).read())