File size: 3,814 Bytes
434ad17
 
 
b6f51d6
434ad17
b6f51d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9422418
 
b6f51d6
 
f906580
b6f51d6
 
 
 
 
 
f906580
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6f51d6
 
 
 
 
 
 
 
 
 
 
 
 
 
d2205dc
 
 
 
 
 
 
 
 
 
b6f51d6
 
 
 
 
 
 
 
d2205dc
4b5e2c2
d2205dc
 
 
 
b6f51d6
4b5e2c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import streamlit as st
import pandas as pd
from io import BytesIO
import base64

# Define the button and title styles
styles = """
<style>
/* Button styles */
div.stButton > button {
    color: #ffffff; /* Text color */
    font-size: 20px;
    background-image: linear-gradient(0deg, #a2c2e1 0%, #003b5c 100%); /* Light blue to deep blue gradient */
    border: none;
    padding: 10px 20px;
    cursor: pointer;
    border-radius: 15px;
    display: inline-block;
    width: 100%; /* Make button fit column width */
}
div.stButton > button:hover {
    background-color: #00ff00; /* Hover background color */
    color: #ff0000; /* Hover text color */
}

/* Title styles */
h1 {
    background-image: linear-gradient(0deg, #a2c2e1 0%, #003b5c 100%); /* Light blue to deep blue gradient */
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    font-size: 36px;
}
</style>
"""

# Set up the Streamlit app
st.markdown(styles, unsafe_allow_html=True)

# Custom gradient title
st.markdown("<h1>CSV/Excel merger</h1>", unsafe_allow_html=True)
st.write("A website that is used for merging multiple csv/excel files and converting them to excel/csv format")

# File uploader for multiple CSV files
uploaded_files = st.file_uploader("Upload CSV files", type=["csv", "xlsx"], accept_multiple_files=True)

if uploaded_files:
    # Initialize an empty list to store DataFrames
    df_list = []

    for i, uploaded_file in enumerate(uploaded_files):
        if uploaded_file.type == "text/csv":
            if i == 0:
                # For the first file, include the header
                df = pd.read_csv(uploaded_file)
                # Store the columns from the first file
                headers = df.columns
            else:
                # For subsequent files, set the header using the stored headers
                df = pd.read_csv(uploaded_file, names=headers, header=0)
        elif uploaded_file.type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
            if i == 0:
                # For the first file, include the header
                df = pd.read_excel(uploaded_file)
                # Store the columns from the first file
                headers = df.columns
            else:
                # For subsequent files, set the header using the stored headers
                df = pd.read_excel(uploaded_file, names=headers, header=0)
        df_list.append(df)

    # Concatenate all DataFrames, ignoring index to avoid duplication
    merged_df = pd.concat(df_list, ignore_index=True)

    # Display the merged DataFrame
    st.markdown("<h1>Appended data</h1>", unsafe_allow_html=True)
    st.dataframe(merged_df)

    # Create a layout with 3 columns for buttons
    col1, col2, col3 = st.columns(3)

    with col1:
        # Provide a download button for the merged CSV file
        if st.button("Convert to CSV"):
            csv_output = BytesIO()
            merged_df.to_csv(csv_output, index=False)
            csv_output.seek(0)
            st.download_button(
                label="Download Merged CSV File",
                data=csv_output,
                file_name="merged_data.csv",
                mime="text/csv"
            )

    with col2:
        # Provide an option to convert to Excel
        if st.button("Convert to Excel"):
            excel_output = BytesIO()
            with pd.ExcelWriter(excel_output, engine='xlsxwriter') as writer:
                merged_df.to_excel(writer, index=False)
            excel_output.seek(0)
            st.download_button(
                label="Download Merged Excel File",
                data=excel_output,
                file_name="merged_data.xlsx",
                mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            )
else:
    st.write("")