Stupid.Fat.Cat
Stupid.Fat.Cat

Reputation: 11285

Python xlsxwriter, add formatting to cell as I write them instead of bulk

Is it possible to apply formatting conditions as I write the data to the worksheet instead of doing this:

worksheet.conditional_format('B3:K12', {'type': 'duplicate',
                                     'format': format})

So something like:

worksheet.write(CELL, "yada yada yada", format=format)

Upvotes: 0

Views: 282

Answers (1)

kennes
kennes

Reputation: 2145

Yes, I think this is possible.

You can start with a base format:

base = {
    'num_format':'#,##0', 
    'font_size':'12'
}

As you write you can use the format.set_ methods to change or add elements to your base format as you write data.

I typically write in a loop like fashion and change the formatting based on the columns.

Example:

body = workbook.add_format(base)

body.set_num_format(some_format) # number example

body.set_font_name(some_format) # font example

sheet.write(CELL, some data, body)

Upvotes: 1

Related Questions