'Download an excel file with column formatting in Dash Python

I'm creating a Dash app and I want to allow users to download a pandas Dataframe as an excel file with a click on a button.

I managed to implement it with this callback :

@app.callback(
Output("download_xlsx", "data"),
Input("btn_download_xlsx", "n_clicks"), State("df", "data")
)
def download_xlsx(n_clicks, json_df):
    df =  pd.read_json(json_df)
    return dcc.send_data_frame(df.to_excel, "nom_fic.xlsx", **{'sheet_name': "Feuil1", 'index': False})

It works great but the Excel file is not formatted as I want, especially because the first column is a really big integer and Excel displays it with scientific notation, and I want to force Excel to display the whole int.

I tried to use XlsxWriter formating as such :

@app.callback(
Output("download_xlsx", "data"),
Input("btn_download_xlsx", "n_clicks"), State("df", "data")
)
def download_xlsx(n_clicks, json_df):
    df =  pd.read_json(json_df)

    writer = pd.ExcelWriter("nom_fic.xlsx", engine='xlsxwriter')
    df.to_excel(writer, sheet_name="Feuil1", index=False)

    workbook = writer.book
    worksheet = writer.sheets["Feuil1"]

    format = workbook.add_format({'num_format': '#,##00.0'})
    worksheet.set_column('A:A', None, format)

    writer.save()

    return dcc.send_data_frame(df.to_excel, "nom_fic.xlsx", **{'sheet_name': "Feuil1", 'index': False})

But I get the following callback error when I try to dowload the file :

TypeError: to_excel() got multiple values for argument 'excel_writer'

Can someone see how I can deal with the to_excel() inputs so that I can use the writer with the formatting I want, or has any other idea to solve this problem ?

Thank you :)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source