Search code examples
pythonpandasxlsxwriter

How to set borders to the data to the excel with Python pandas/XLSX writer


I am trying to format the excel sheet with the borders with python pandas, but no luck, can anyone please assist.

I have data like this:

enter image description here

I want this in this format:

enter image description here


Solution

  • It is not straightforward, You have to iterate through the dataframe and add the format to each cell.

    cell_format = workbook.add_format()
    cell_format.set_border()
    for col_num, col in enumerate(df.columns.values):
        for row_num, value in enumerate(df[col].values):
            worksheet.write(row_num + 1, col_num + 1, value, cell_format)