Upload 2 files
Browse files- merger.py +85 -0
- requirements.txt +66 -0
merger.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from typing import List
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
from tempfile import NamedTemporaryFile
|
| 6 |
+
|
| 7 |
+
# Function to append files
|
| 8 |
+
def append_files(files: List[BytesIO]):
|
| 9 |
+
dfs = []
|
| 10 |
+
file_dimensions = []
|
| 11 |
+
file_names = []
|
| 12 |
+
|
| 13 |
+
for file in files:
|
| 14 |
+
try:
|
| 15 |
+
if file.name.endswith('.xls') or file.name.endswith('.xlsx'):
|
| 16 |
+
df = pd.read_excel(file)
|
| 17 |
+
elif file.name.endswith('.csv'):
|
| 18 |
+
df = pd.read_csv(file)
|
| 19 |
+
|
| 20 |
+
# Store dimensions and filename of each file
|
| 21 |
+
file_dimensions.append(df.shape)
|
| 22 |
+
file_names.append(file.name)
|
| 23 |
+
dfs.append(df)
|
| 24 |
+
|
| 25 |
+
except Exception as e:
|
| 26 |
+
st.error(f"Error reading file {file.name}: {e}")
|
| 27 |
+
return None, [], []
|
| 28 |
+
|
| 29 |
+
if len(dfs) == 0:
|
| 30 |
+
st.warning("No valid files found to append.")
|
| 31 |
+
return None, [], []
|
| 32 |
+
|
| 33 |
+
try:
|
| 34 |
+
appended_df = pd.concat(dfs, ignore_index=True)
|
| 35 |
+
appended_dimensions = appended_df.shape
|
| 36 |
+
except Exception as e:
|
| 37 |
+
st.error(f"Error appending files: {e}")
|
| 38 |
+
return None, [], []
|
| 39 |
+
|
| 40 |
+
return appended_df, file_names, file_dimensions, appended_dimensions
|
| 41 |
+
|
| 42 |
+
# Streamlit UI
|
| 43 |
+
def main():
|
| 44 |
+
st.title(':blue[Append Excel/CSV Files]')
|
| 45 |
+
st.write("")
|
| 46 |
+
uploaded_files = st.file_uploader("Upload your files", accept_multiple_files=True)
|
| 47 |
+
|
| 48 |
+
if st.button("Append Files") and uploaded_files:
|
| 49 |
+
st.write("")
|
| 50 |
+
files_data = [file for file in uploaded_files]
|
| 51 |
+
appended_data, file_names, file_dimensions, appended_dimensions = append_files(files_data)
|
| 52 |
+
|
| 53 |
+
if appended_data is not None:
|
| 54 |
+
st.subheader(":blue[Dimensions of Uploaded Files:]")
|
| 55 |
+
for i, (filename, dim) in enumerate(zip(file_names, file_dimensions), start=1):
|
| 56 |
+
st.write(f"{filename}: {dim[0]} rows, {dim[1]} columns")
|
| 57 |
+
|
| 58 |
+
st.subheader("\n:blue[Dimensions of Appended Data:]")
|
| 59 |
+
st.write(f"Appended Data: {appended_dimensions[0]} rows, {appended_dimensions[1]} columns")
|
| 60 |
+
st.write("")
|
| 61 |
+
|
| 62 |
+
st.subheader("\n:blue[Appended Data:]")
|
| 63 |
+
st.write(appended_data)
|
| 64 |
+
|
| 65 |
+
# Allow user to download the appended file as CSV
|
| 66 |
+
|
| 67 |
+
col1, col2 = st.columns([0.5, 1.2])
|
| 68 |
+
|
| 69 |
+
with col1:
|
| 70 |
+
|
| 71 |
+
with NamedTemporaryFile(delete=False, suffix='.csv') as temp_csv:
|
| 72 |
+
appended_data.to_csv(temp_csv.name, index=False)
|
| 73 |
+
st.download_button(label="Download Appended CSV", data=temp_csv.name, file_name='appended_data.csv', mime='text/csv')
|
| 74 |
+
|
| 75 |
+
with col2:
|
| 76 |
+
# Allow user to download the appended file as Excel (xlsx)
|
| 77 |
+
with NamedTemporaryFile(delete=False, suffix='.xlsx') as temp_xlsx:
|
| 78 |
+
appended_data.to_excel(temp_xlsx.name, index=False)
|
| 79 |
+
st.download_button(label="Download Appended Excel", data=temp_xlsx.name, file_name='appended_data.xlsx', mime='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|
| 80 |
+
|
| 81 |
+
else:
|
| 82 |
+
st.warning("Unable to append files. Please check your files and try again.")
|
| 83 |
+
|
| 84 |
+
if __name__ == '__main__':
|
| 85 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
altair==4.2.2
|
| 2 |
+
attrs==23.2.0
|
| 3 |
+
blinker==1.8.2
|
| 4 |
+
cachetools==5.3.3
|
| 5 |
+
certifi==2024.6.2
|
| 6 |
+
charset-normalizer==3.3.2
|
| 7 |
+
click==8.1.7
|
| 8 |
+
entrypoints==0.4
|
| 9 |
+
et-xmlfile==1.1.0
|
| 10 |
+
gitdb==4.0.11
|
| 11 |
+
GitPython==3.1.43
|
| 12 |
+
google-api-core==2.19.0
|
| 13 |
+
google-auth==2.30.0
|
| 14 |
+
google-auth-oauthlib==1.2.0
|
| 15 |
+
google-cloud-bigquery==3.24.0
|
| 16 |
+
google-cloud-core==2.4.1
|
| 17 |
+
google-crc32c==1.5.0
|
| 18 |
+
google-resumable-media==2.7.1
|
| 19 |
+
googleapis-common-protos==1.63.1
|
| 20 |
+
grpcio==1.64.1
|
| 21 |
+
grpcio-status==1.62.2
|
| 22 |
+
gspread==6.1.2
|
| 23 |
+
httplib2==0.22.0
|
| 24 |
+
idna==3.7
|
| 25 |
+
Jinja2==3.1.4
|
| 26 |
+
jsonschema==4.22.0
|
| 27 |
+
jsonschema-specifications==2023.12.1
|
| 28 |
+
markdown-it-py==3.0.0
|
| 29 |
+
MarkupSafe==2.1.5
|
| 30 |
+
mdurl==0.1.2
|
| 31 |
+
numpy==1.26.4
|
| 32 |
+
oauth2client==4.1.3
|
| 33 |
+
oauthlib==3.2.2
|
| 34 |
+
openpyxl==3.1.4
|
| 35 |
+
packaging==24.1
|
| 36 |
+
pandas==2.2.2
|
| 37 |
+
pillow==10.3.0
|
| 38 |
+
proto-plus==1.24.0
|
| 39 |
+
protobuf==4.25.3
|
| 40 |
+
pyarrow==16.1.0
|
| 41 |
+
pyasn1==0.6.0
|
| 42 |
+
pyasn1_modules==0.4.0
|
| 43 |
+
pydeck==0.9.1
|
| 44 |
+
Pygments==2.18.0
|
| 45 |
+
pyparsing==3.1.2
|
| 46 |
+
python-dateutil==2.9.0.post0
|
| 47 |
+
python-decouple==3.8
|
| 48 |
+
pytz==2024.1
|
| 49 |
+
referencing==0.35.1
|
| 50 |
+
requests==2.32.3
|
| 51 |
+
requests-oauthlib==2.0.0
|
| 52 |
+
rich==13.7.1
|
| 53 |
+
rpds-py==0.18.1
|
| 54 |
+
rsa==4.9
|
| 55 |
+
six==1.16.0
|
| 56 |
+
smmap==5.0.1
|
| 57 |
+
streamlit==1.35.0
|
| 58 |
+
streamlit-aggrid==1.0.5
|
| 59 |
+
tenacity==8.3.0
|
| 60 |
+
toml==0.10.2
|
| 61 |
+
toolz==0.12.1
|
| 62 |
+
tornado==6.4.1
|
| 63 |
+
typing_extensions==4.12.2
|
| 64 |
+
tzdata==2024.1
|
| 65 |
+
urllib3==2.2.1
|
| 66 |
+
XlsxWriter==3.2.0
|