Berny
Berny

Reputation: 133

python conditional formatting to excel file

I have an existing excel file, I want to apply some conditional formatting to some cells, I have tried with openpyxl and now with xlsxwriter, basically is to change the font color to gray if the cell is equal to 0:

import xlsxwriter

workbook = xlsxwriter.Workbook(r"C:\Users\simon\REPORT.xlsx")
ws = workbook.get_worksheet_by_name('Summary')

format1 = workbook.add_format({'font_color': '#A6A6A6'})
ws.conditional_format('E21:U49', {'type': 'cell',
                                  'criteria': '==',
                                  'value': '0',
                                  'format': format1})

workbook.close()

But I keep getting the same AttributeError:

AttributeError: 'NoneType' object has no attribute 'conditional_format'

Upvotes: 1

Views: 1566

Answers (1)

Albaraa Ahmed
Albaraa Ahmed

Reputation: 31

This error happens when the object is empty meaning that there might not be a worksheet called Summary check the Sheet's name for any Typos and also remember that Its Case Sensitive.

Upvotes: 1

Related Questions