learning2learn
learning2learn

Reputation: 411

Is there a way to apply cell format in xlswriter without setting a value?

Is there a way to apply cell format in xlswriter without setting a value? And in terms of worksheet.write use of A1 notation, can it be done to a range?

Like:

format = workbook.add_format()
format.set_border(1)
format.set_bold()
worksheet.write(row=0, col=0, format=format)
worksheet.write('A1:C3', format=format)

Upvotes: 3

Views: 4699

Answers (1)

patrickjlong1
patrickjlong1

Reputation: 3823

Using worksheet.write() you can not specify a range. You can put it inside a loop and make use of xlsxwriter.utility.xl_rowcol_to_cell() which would achieve what you're looking for. Here is further explanation and examples involving xlsxwriter.utility.xl_rowcol_to_cell() from xlsxwriter docs.

Also you can format blank cells by entering "" as the second argument in worksheet.write(). I've provided a full working example below along with an image of the expected output.

import xlsxwriter
from xlsxwriter.utility import xl_rowcol_to_cell

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

format = workbook.add_format()
format.set_border(1)
format.set_bold()
format.set_bg_color("blue")
format.set_num_format('#,##0')

for i in range (0, 3):
    #we're looping through columns
    for j in range (0, 3):
    #we're looping through rows
        cell_reference = xl_rowcol_to_cell(i, j, row_abs=True, col_abs=True)
        worksheet.write('%s' % (cell_reference), "" ,format)

workbook.close()

Expected excel ouptut

Upvotes: 5

Related Questions