Openpyxl - условное форматирование - по всем oop в столбце - PullRequest
0 голосов
/ 21 января 2020

Я пытаюсь отформатировать столбец, который будет заполнен через al oop. Вот код, который у меня есть:

from openpyxl import *
import time
name="TestWorkbook"
months = ['January', 'February', 'March', 'April','May', 'June','July']

book = Workbook()
book.remove(book.active)

Months_Iter = ['Dec', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov']
Used_Not_Used = ['Used','Not Used','Not Used','Not Used','Not Used','Used','Not Used','Used','Used','Used','Used']

for i in months:
    sheet=book.create_sheet("Sheet_A")
    sheet.title = ('Month_of_'+ i)

    sheet['A1'] = 56
    now = time.strftime("%x")

    sheet.column_dimensions['B'].width= 50
    sheet['B1']= (' User Port with Voice VLAN - ' + (i))

    sheet.column_dimensions['C'].width= 30
    sheet['C1'] = now
    sheet["C1"].font=Font(color = "FF0000" )

    sheet['D1'] = i
    sheet['D1'].font=Font(italic=True, bold=True)

    sheet['F1'] = ('Months Iteration')
    sheet['F1'].font=Font(color= "00008b")
    sheet.column_dimensions['F'].width=35
    for mon in range(0,len(Months_Iter)):
        sheet.cell(row=mon+2,column=6).value=Months_Iter[mon]

    sheet['G1'] = ('Status')
    for stat in range(0,len(Used_Not_Used)):
        sheet.cell(row=stat+2,column=7).value=Used_Not_Used[stat]
        sheet.conditional_formatting.add('G1:G500', formatting.rule.CellIsRule(operator='containsText', formula=['Used'], fill=red_fill, 
font=red_text))
book.save('workbook_'+(name)+".xlsx")

Последний раздел не выполняет форматирование и выплевывает каждую ошибку под солнцем. Условие: если в столбце G содержится слово «Используется», то выделите его красным.

Где я здесь не так?

...