Search code examples
pythonpandasformattingxlsxwriter

Using xlsxwriter to Align Left a Row


I am trying to left align a single row using xlsxwriter. I tried the following but I it does not work. How should I do this ?

stats = DataFrame(...)
xl_writer = ExcelWriter(r'U:\temp\test.xlsx')
stats.to_excel(xl_writer, 'Stats')

workbook = xl_writer.book
format_header = workbook.add_format({'align': 'left'})
stats_sheet = xl_writer.sheets['Stats']
stats_sheet.set_row(0, None, format_header)

Solution

  • See the XlsxWriter docs for Formatting of the Dataframe headers:

    Pandas writes the dataframe header with a default cell format. Since it is a cell format it cannot be overridden using set_row(). If you wish to use your own format for the headings then the best approach is to turn off the automatic header from Pandas and write your own. For example...