Error using xlwings package with streamlit

i have a streamlit app that allow user to read excel file and try to split each sheet into a different workbook for this i am using the xlwings package when i try to debug the code it crash and display the below error :

could not load source '<attrs generated init streamlit.state.session_state.Value>': Source unavailable

what this error mean ? and how to fix it ?

code:


import streamlit as st 
import pandas as pd
import numpy as np

from pathlib import Path
import xlwings as xw  

sheet5_path = "C:/Users/gm/Documents"

st.markdown("""

<style>
.header_title {
        font-size:30px !important;
        color:black;
        text-align :center
    }
.big-font {
        font-size:50px !important;
        color:Red
</style>
""", unsafe_allow_html=True)

st.title("Excel Split & Send")
try:
    data = st.sidebar.file_uploader("Upload Dataset",type=["csv","xlsx","xls"])
    
except Exception as e:
    st.write("error is: {}".format(e))
    
if data is not None:
    sheet1,sheet2,sheet3,sheet4,sheet5 = None,None,None,None,None
    with pd.ExcelFile(data,) as reader:
        sheet1 = pd.read_excel(reader,sheet_name="sheet1")
        sheet2 = pd.read_excel(reader,sheet_name="sheet2")
        sheet3 = pd.read_excel(reader,sheet_name="sheet3")
        sheet4 = pd.read_excel(reader,sheet_name="sheet4")
        sheet5 = pd.read_excel(reader,sheet_name="sheet5")

        st.write(sheet1.finished[0])
        st.write(sheet1.unfinished[0])
        st.markdown('<p class="header_title">sheet1|{}</p>'.format(str(sheet1.shape[0])), unsafe_allow_html=True)
        sheet1 = pd.read_excel(reader,sheet_name="sheet1",usecols=[0,1,2,3,4])
        st.dataframe(sheet1)
    
        st.write(sheet2.finished[0])
        st.write(sheet2.unfinished[0])
        st.markdown('<p class="header_title">sheet2|{}</p>'.format(str(sheet2.shape[0])), unsafe_allow_html=True)
        sheet2 = pd.read_excel(reader,sheet_name="sheet2",usecols=[0,1,2,3,4])   
        st.dataframe(sheet2)

        st.write(sheet3.finished[0])
        st.write(sheet3.unfinished[0])  
        st.markdown('<p class="header_title">sheet3|{}</p>'.format(str(sheet3.shape[0])), unsafe_allow_html=True)
        sheet3 = pd.read_excel(reader,sheet_name="sheet3",usecols=[0,1,2,3,4])
        st.dataframe(sheet3)
        
        st.write(sheet4.finished[0])
        st.write(sheet4.unfinished[0])
        st.markdown('<p class="header_title">sheet4|{} </p>'.format(str(sheet4.shape[0])), unsafe_allow_html=True)
        sheet4 = pd.read_excel(reader,sheet_name="sheet4",usecols=[0,1,2,3,4])
        st.dataframe(sheet4)

        st.write(sheet5.finished[0])
        st.write(sheet5.unfinished[0])
        st.markdown('<p class="header_title">sheet5|{} </p>'.format(str(sheet5.shape[0])), unsafe_allow_html=True)
        sheet5 = pd.read_excel(reader,sheet_name="sheet5",usecols=[0,1,2,3,4])  
        st.dataframe(sheet5)
    
    if st.sidebar.button("Split & Send"):
        #create  xlwings object
        app = xw.App(visible = False)

        # create workbook
        wb = xw.Book(data)
        # iterate over the sheets of the created workbook
    # if sheet name = total  delete it (do not copy it)
        for  sheet in wb.sheets:
            if "total" in sheet.name:
                sheet.delete()
            
            # create new work book copy the first sheet  
            else:
                wb_new = app.books.add()
                sheet.copy(after = wb_new.sheets[0])
                wb_new.sheets[0].delete()
                
                #delete the columns G:H 
                wb_new.sheets[0].range('G:H').delete()
                
                # save the new excel files  as name of each sheet  
                if "sheet5"  in sheet.name:
                    wb_new.save(f"{sheet5_path}/{sheet.name}.xlsx")
                else:
                    wb_new.save(f"{sheet.name}.xlsx")
                # close the create workbook
                wb_new.close()

        
else:
    st.markdown('<p class="big-font">You Need to Upload a FILE !!</p>', unsafe_allow_html=True)

@leb_dev - just wondering if you managed to fix this?

Yes it was fixed once i run the app using **streamlit run ** out of the debugging mode than i try it in the debugging mode it seems to work. :thinking:

1 Like