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} )
python 2.7 - how to add border to a range of cells using xlsxwriter? - Stack Overflow
excel - python XlsxWriter set border around multiple cells - Stack Overflow
python - Thick border in xlsxwriter - Stack Overflow
how can I put a border around a dataframe with xlsxwriter?
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} )
An improvement on the accepted answer to get the conditional formatting method working for both empty cells & non-empty cells.
workbook = xlsxwriter.Workbook('hello_.xlsx')
border = workbook.add_format ({ 'border':1})
worksheet = workbook.add_worksheet('results')
headers = ['Total', 'GST', 'Total(Inc GST)', 'Commission', 'Final Payment']
vals = [31514.47, 3151.44 , 34665.917, 2773.27, 31892.64]
start_row=9
start_col=3
worksheet.write_column (start_row, start_col ,headers)
worksheet.write_column (start_row, start_col+4 ,vals)
# This method works for all cell regardless of being empty or not
worksheet.conditional_format( start_row, start_col, start_row+5, start_col+6, { 'type' : 'cell' ,
'criteria': 'not equal to',
'value': '"Text_with_0_probability"',
'format' : border} )
workbook.close()
XlsxWriter is an awesome module that made my old job 1,000x easier (thanks John!), but formatting cells with it can be time-consuming. I've got a couple helper functions I use to do stuff like this.
First, you need to be able to create a new format by adding properties to an existing format:
def add_to_format(existing_format, dict_of_properties, workbook):
"""Give a format you want to extend and a dict of the properties you want to
extend it with, and you get them returned in a single format"""
new_dict={}
for key, value in existing_format.__dict__.iteritems():
if (value != 0) and (value != {}) and (value != None):
new_dict[key]=value
del new_dict['escapes']
return(workbook.add_format(dict(new_dict.items() + dict_of_properties.items())))
Now build off of that function with:
def box(workbook, sheet_name, row_start, col_start, row_stop, col_stop):
"""Makes an RxC box. Use integers, not the 'A1' format"""
rows = row_stop - row_start + 1
cols = col_stop - col_start + 1
for x in xrange((rows) * (cols)): # Total number of cells in the rectangle
box_form = workbook.add_format() # The format resets each loop
row = row_start + (x // cols)
column = col_start + (x % cols)
if x < (cols): # If it's on the top row
box_form = add_to_format(box_form, {'top':1}, workbook)
if x >= ((rows * cols) - cols): # If it's on the bottom row
box_form = add_to_format(box_form, {'bottom':1}, workbook)
if x % cols == 0: # If it's on the left column
box_form = add_to_format(box_form, {'left':1}, workbook)
if x % cols == (cols - 1): # If it's on the right column
box_form = add_to_format(box_form, {'right':1}, workbook)
sheet_name.write(row, column, "", box_form)
Gilad's answer is great b/c it is compatible with Python 3. I modified it further to handle scenarios with a single column or row.
# Format cell borders via a configurable RxC box
def draw_frame_border(workbook, worksheet, first_row, first_col, rows_count, cols_count,thickness=1):
if cols_count == 1 and rows_count == 1:
# whole cell
worksheet.conditional_format(first_row, first_col,
first_row, first_col,
{'type': 'formula', 'criteria': 'True',
'format': workbook.add_format({'top': thickness, 'bottom':thickness,
'left': thickness,'right':thickness})})
elif rows_count == 1:
# left cap
worksheet.conditional_format(first_row, first_col,
first_row, first_col,
{'type': 'formula', 'criteria': 'True',
'format': workbook.add_format({'top': thickness, 'left': thickness,'bottom':thickness})})
# top and bottom sides
worksheet.conditional_format(first_row, first_col + 1,
first_row, first_col + cols_count - 2,
{'type': 'formula', 'criteria': 'True', 'format': workbook.add_format({'top': thickness,'bottom':thickness})})
# right cap
worksheet.conditional_format(first_row, first_col+ cols_count - 1,
first_row, first_col+ cols_count - 1,
{'type': 'formula', 'criteria': 'True',
'format': workbook.add_format({'top': thickness, 'right': thickness,'bottom':thickness})})
elif cols_count == 1:
# top cap
worksheet.conditional_format(first_row, first_col,
first_row, first_col,
{'type': 'formula', 'criteria': 'True',
'format': workbook.add_format({'top': thickness, 'left': thickness,'right':thickness})})
# left and right sides
worksheet.conditional_format(first_row + 1, first_col,
first_row + rows_count - 2, first_col,
{'type': 'formula', 'criteria': 'True', 'format': workbook.add_format({'left': thickness,'right':thickness})})
# bottom cap
worksheet.conditional_format(first_row + rows_count - 1, first_col,
first_row + rows_count - 1, first_col,
{'type': 'formula', 'criteria': 'True',
'format': workbook.add_format({'bottom': thickness, 'left': thickness,'right':thickness})})
else:
# top left corner
worksheet.conditional_format(first_row, first_col,
first_row, first_col,
{'type': 'formula', 'criteria': 'True',
'format': workbook.add_format({'top': thickness, 'left': thickness})})
# top right corner
worksheet.conditional_format(first_row, first_col + cols_count - 1,
first_row, first_col + cols_count - 1,
{'type': 'formula', 'criteria': 'True',
'format': workbook.add_format({'top': thickness, 'right': thickness})})
# bottom left corner
worksheet.conditional_format(first_row + rows_count - 1, first_col,
first_row + rows_count - 1, first_col,
{'type': 'formula', 'criteria': 'True',
'format': workbook.add_format({'bottom': thickness, 'left': thickness})})
# bottom right corner
worksheet.conditional_format(first_row + rows_count - 1, first_col + cols_count - 1,
first_row + rows_count - 1, first_col + cols_count - 1,
{'type': 'formula', 'criteria': 'True',
'format': workbook.add_format({'bottom': thickness, 'right': thickness})})
# top
worksheet.conditional_format(first_row, first_col + 1,
first_row, first_col + cols_count - 2,
{'type': 'formula', 'criteria': 'True', 'format': workbook.add_format({'top': thickness})})
# left
worksheet.conditional_format(first_row + 1, first_col,
first_row + rows_count - 2, first_col,
{'type': 'formula', 'criteria': 'True', 'format': workbook.add_format({'left': thickness})})
# bottom
worksheet.conditional_format(first_row + rows_count - 1, first_col + 1,
first_row + rows_count - 1, first_col + cols_count - 2,
{'type': 'formula', 'criteria': 'True', 'format': workbook.add_format({'bottom': thickness})})
# right
worksheet.conditional_format(first_row + 1, first_col + cols_count - 1,
first_row + rows_count - 2, first_col + cols_count - 1,
{'type': 'formula', 'criteria': 'True', 'format': workbook.add_format({'right': thickness})})
Hi. I'm writing a dataframe to Excel and need to put a border around the whole dataframe. I'm finding it quite difficult.
Given a typical case:
writer = pd.ExcelWriter(output_file, engine='xlsxwriter')
df.to_excel(writer, sheet_name = 'great', index=False)
workbook = writer.book
worksheet = writer.sheets['great']
The only way I've found to write a border is like this, but you need to write the data (header in this example) and format with a border.
headers = df.columns
row_index = 10
start_col = 0
end_col = start_col + len(headers)
for i in range(start_col, end_col):
worksheet.write(row_index, i, headers[i-start_col], format2)
I don't need to write the data - it's already in the dataframe. I just want to put a line at the bottom at the least, and preferably a box around the whole thing.
How to do it?

