'problem with writing dataframe to excel with xlwings
I have a working script for some financial modeling . The script reads inputs from couple of sheets from the excel and returns a dataframe. When I run the script and print, the dataframe looks fine. But when I try to write the dataframe to the excel file, it throws an error.
I have tried the same chunk of code (that writes to excel file) separately and it works flawlessly. Not sure what I am doing wrong.
the actual code is quite long, so here is the final snippet: NOTE: the values in the dictionary are the lists that are defined inside the code, so i hope that is not what creating the issue because without the commented part(writing to excel), the code runs perfectly.
def main_function(filename):
output_df = pd.DataFrame({
'Stage': n_stageName,
'Year After Origination': n_yearAfterOrigination,
'Management Overlay': n_mgmt_overlay,
'PD curve ID': n_pd_curveID,
'LGD': n_LGD,
'ECL': n_ECL,
'FLI Adj ECL': n_FLI_adj_ECL,
'EIR': n_EIR,
'ECL (JMD)': n_ECL_JMD,
'FLI Adj ECL (JMD)': n_FLI_adj_ECL_JMD,
'Exposure': n_Exposure})
#ws = xw.Book(filename).sheets['test']
#ws["A1"].options(pd.DataFrame, header=False, index=False, expand='table').value = output_df
return output_df
main_loop('sample2.xlsx')
however, when i run the same chunk of code for writing into excel on a separate file (where I created a dataframe manually), it works fine
def to_excel(filename):
output_df = pd.DataFrame({
'Stage': [1,2,3],
'Year After Origination': [4,5,6],
'Management Overlay': [7,8,9],
})
ws = xw.Book(filename).sheets["test"]
ws["A1"].options(pd.DataFrame, header=True, index=False, expand='table').value = output_df
return output_df
to_excel('sample2.xlsx')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
