Roshni Johnson
Roshni Johnson

Reputation: 11

Add border to excel using pandas

I need to add a border around my dataframe which i have created using pandas as shown in the attached pictureExcelTableImage. Below is my code snippet. How can this be done?

[df = pd.DataFrame(data)
df2=pd.DataFrame(exportFields)
df2 = df2.transpose()
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('C:/Users/roshni.j/Desktop/Clarivoy_docs/Testing/pandas_simple.xlsx', engine='xlsxwriter')


# Convert the dataframe to an XlsxWriter Excel object.
df2.to_excel(writer, sheet_name='Sheet1',index=False, header=False,startcol=1, startrow=2)
df.to_excel(writer, sheet_name='Sheet1',index=False, header=False,startcol=1, startrow=3)


# Close the Pandas Excel writer and output the Excel file.
writer.save()

Upvotes: 1

Views: 5579

Answers (2)

Rafael Ribeiro
Rafael Ribeiro

Reputation: 91

Use add_table method from pandas then you can style it. https://xlsxwriter.readthedocs.io/working_with_tables.html

Upvotes: 0

Pavel Kovtun
Pavel Kovtun

Reputation: 367

You need to use something like StyleFrame for this (https://styleframe.readthedocs.io/en/latest/). Pure pandas can't draw any frames in Excel.

Upvotes: 3

Related Questions