Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional Formatting xlwt

I have seen some posts that say you can NOT perform conditional formatting using xlwt, but they were rather old. I was curious if this has evolved?

I have been searching for about half a day now. Furthermore, if I con't write it directly from xlwt, can I create an .xls file containing a single cell with the conditional format I want and have xlrd read that format and paste it into the sheet I aim to produce then using xlwt?

like image 521
jensencl Avatar asked Apr 23 '13 18:04

jensencl


People also ask

What is conditional formatting with example?

Conditional Formatting (CF) is a tool that allows you to apply formats to a cell or range of cells, and have that formatting change depending on the value of the cell or the value of a formula. For example, you can have a cell appear bold only when the value of the cell is greater than 100.


1 Answers

xlrd and xlwt still don't support conditional formatting. xlrd doesn't read it, xlwt doesn't write it.

There is a new and awesome module, called xlsxwriter. It does support conditional formatting out of the box. The project is active, documentation is pretty good. Plus, there are a lot of examples.

Here's an example:

from xlsxwriter.workbook import Workbook

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

worksheet.write('A1', 49)
worksheet.write('A2', 51)

format1 = workbook.add_format({'bold': 1, 'italic': 1})
worksheet.conditional_format('A1:A2', {'type': 'cell',
                                       'criteria': '>=',
                                       'value': 50,
                                       'format': format1})
workbook.close()
like image 175
alecxe Avatar answered Sep 22 '22 23:09

alecxe