Search code examples
python-3.xpandasxlsxwriterpandas.excelwriter

Set a name to Excel Table with Python


I'am trying to set a name to Excel table (that i created with the code below) in order to replace the default one

    with pd.ExcelWriter('pandas_table.xlsx', engine='xlsxwriter') as writer:
        df.to_excel(writer, sheet_name='StgFull', startrow=1, header=False, index=False)
        workbook = writer.book
        worksheet = writer.sheets['StgFull']
        (max_row, max_col) = df.shape
        tabname = 'abcd'
        column_settings = [{'header': column} for column in df.columns]
        worksheet.add_table(0, 0, max_row, max_col - 1
                            , {'columns': column_settings}
                            )
        worksheet.set_column(0, max_col - 1, 12)
    writer.save()

enter image description here


Solution

  • Well, i had to do this :

    worksheet.add_table(0, 0, max_row, max_col - 1
                                , {'columns': column_settings
                                ,"name": "TabName"}
                                )