Pavan
Pavan

Reputation: 17

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

Upvotes: 1

Views: 3359

Answers (1)

Ze'ev Ben-Tsvi
Ze'ev Ben-Tsvi

Reputation: 1432

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)

Upvotes: 1

Related Questions