Search code examples
pythonxlwt

How to change background color of excel cell with python xlwt library?


I use xlwt Python library to write data in excel workbook.
And now I have some problems with adding background color to excel cell.

For example I have next color in RGB(10,20,30), what is the easiest way to do this? Is there any way to set this color to cell?

I found only this post which similar with my problem.


Solution

  • In this example, I have shown how to set background color for cells, you can run it for result:

    from xlwt import Workbook
    import xlwt
    book = Workbook()
    sheet1 = book.add_sheet('Sheet 1')
    for i in range(0, 100):
        st = xlwt.easyxf('pattern: pattern solid;')
        st.pattern.pattern_fore_colour = i
        sheet1.write(i % 24, i // 24, 'Test text', st)
    book.save('simple.xls')