Thick border in xlsxwriter
Asked Answered
S

1

11

Is there any option to add thick border in Excel using xlsxwriter?

I'm writing a border between a range using conditional format.

Add table won't work in my case.enter image description here

Siward answered 4/4, 2018 at 2:12 Comment(0)
T
20

Is there any option to add thick border in Excel using xlsxwriter?

You can use a cell format with a border style 2 or 5 for this. See the weight column in the Format set_border() method:

import xlsxwriter

workbook = xlsxwriter.Workbook('borders.xlsx')
worksheet = workbook.add_worksheet()

format1 = workbook.add_format({'border': 1})
format2 = workbook.add_format({'border': 2})
format3 = workbook.add_format({'border': 5})

worksheet.write('B3', 'Border 1', format1)
worksheet.write('B6', 'Border 2', format2)
worksheet.write('B9', 'Border 5', format3)

workbook.close()

Output:

enter image description here

Note however, as far as I know Excel doesn't support/allow thicker borders in conditional formats. As far as I can see the only weight allowed, via the dialog, is 1, i.e., standard thickness.

Also note, there is no direct way in XlsxWriter to apply a border around a range. You will need to apply the appropriate border formats (8+ formats) to all the cells at the edges of the range. This is what Excel does, it is just hidden behind the GUI.

Turnstone answered 4/4, 2018 at 8:22 Comment(3)
Is it possible to change the color of the border ? I am not seeing anything int the documentation.Waterresistant
Try set_border_color() or border_color as a parameter: xlsxwriter.readthedocs.io/format.html#format-set-border-colorTurnstone
Nice solution, you saved my time.Galcha

© 2022 - 2024 — McMap. All rights reserved.