Streamlit: Upload Folder, Read Excel Workbooks, and Create Selection Bar
To upload a folder and read all Excel workbooks in Streamlit, you can use the 'streamlit.file_uploader' function and the 'pandas' library. Here's an example code snippet:
import streamlit as st
import pandas as pd
import os
# Upload folder
uploaded_folder = st.file_uploader('Upload folder', type='zip')
if uploaded_folder:
# Extract files from folder
folder_name = os.path.splitext(uploaded_folder.name)[0]
os.makedirs(folder_name, exist_ok=True)
with open(uploaded_folder.name, 'wb') as f:
f.write(uploaded_folder.getvalue())
import zipfile
with zipfile.ZipFile(uploaded_folder.name, 'r') as zip_ref:
zip_ref.extractall(folder_name)
# Read Excel files
excel_files = []
for file in os.listdir(folder_name):
if file.endswith('.xlsx'):
excel_files.append(file)
if not excel_files:
st.write('No Excel files found in folder.')
else:
# Create selection bar for workbook name
selected_file = st.selectbox('Select workbook:', excel_files)
# Read selected workbook
df = pd.read_excel(os.path.join(folder_name, selected_file))
st.write(df)
This code will allow the user to upload a zip folder containing Excel workbooks, extract the files to a new directory, and read all the Excel files. It then creates a selection bar for the user to choose which workbook to display. The selected workbook is then read using 'pandas' and displayed in the Streamlit app.
Note that this code assumes that all Excel workbooks are in the '.xlsx' format. If you have workbooks in other formats, you will need to modify the code to handle those files appropriately.
原文地址: https://www.cveoy.top/t/topic/n5M9 著作权归作者所有。请勿转载和采集!