I have a dataframe and I want to set outer borders. I tried the below code but it adds border to each and every cell within 'A1:I83' range. I only want to add outer thick border:
border_format=workbook.add_format({
'border':1,
'align':'left',
'font_size':10
})
worksheet_rating_input.conditional_format('A1:I83' , { 'type' : 'no_blanks' , 'format' : border_format})
One way you could do it is to set the format of J1 through J183 with a thick left border and set the format of A184 to I184 to have a thick top border.
I've posted a fully reproducible example of this below. In my example, I make use of df.shape to set the borders dependant on dimensionality of my dataframe.
import xlsxwriter
import pandas as pd
import numpy as np
# Creating a dataframe
df = pd.DataFrame(np.random.randn(182, 9), columns=list('ABCDEFGHI'))
column_list = df.columns
# Create a Pandas Excel writer using XlsxWriter engine.
writer = pd.ExcelWriter("test.xlsx", engine='xlsxwriter')
df.to_excel(writer, index=False)
workbook = writer.book
worksheet = writer.sheets['Sheet1']
leftFormat = workbook.add_format({'left': 5})
topFormat = workbook.add_format({'top': 5})
for row in range(0, df.shape[0] + 1):
worksheet.write(row, df.shape[1], '', leftFormat)
for col in range(0, df.shape[1]):
worksheet.write(df.shape[0] + 1, col, '', topFormat)
worksheet.freeze_panes(1, 0) #freezing top row
writer.save()
With Expected Output:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With