how to add border to a range of cells using xlsxwriter?

talktalk picture talktalk · Dec 1, 2016 · Viewed 14.5k times · Source

I want to add border around a range of cells like this image1, but my output file looks like this when I applied "set_column" function image2. instead of "set_column", is there any other functions to use?

writer=pd.ExcelWriter('merge_'+name+'.xlsx',
                      engine='xlsxwriter')
workbook  = writer.book
data.to_excel(writer, sheet_name=SheetName,index=False,na_rep=0)
worksheet = writer.sheets[SheetName]
border_format=workbook.add_format({
                            'border':1,
                            'align':'left',
                            'font_size':10
                           })
worksheet.set_column('A:D',12,border_format)

Thank you in advance,

Answer

Barmaley picture Barmaley · Dec 1, 2016

I don't know how with xlsxwriter you can add format at range with set_column or set_row methods, but you can try do it with conditional formatting like this:

worksheet.conditional_format( 'A1:D12' , { 'type' : 'no_blanks' , 'format' : border_format} )