Please vote up for my question I just got banned from posting questions at a crucial time for me. I'm a beginner programmer. Please guys.
I have two lists, I need to iterate over each item in both lists (in order) to change Excel cell fill color using openpyxl. I get a syntax error in the line that starts with ws.cell(row = int(items), column
Here is the code:
color_lst= ['FF333399', 'FFFFFF00', 'FFC0C0C0', 'FF00FF00', 'FF008080', 'FF00FFFF', 'FF000080', 'FF800000', 'FF969696', 'FFFF99CC', 'FFFFFF99', 'FF800080', 'FF99CC00', 'FF0000FF']
label_list_for_col_header= ['Energy', 'Green Buildings', 'High Performance Buildings', 'Computing', 'Design', 'Infrared', 'Laser scanning', 'Outdoor Thermal Comfort', 'Urban Desgin', 'Daylighting', 'Thermal Comfort', 'Parametric', 'Simulation Tools', 'N']
gerenral_lst= list()
m= 0
for row in ws.iter_rows('D2:D11'):
for cell in row:
if label_list_for_col_header[m] in cell.value :
general_lst.append(cell.row)
for items in range(len(general_lst)):
ws.cell(row = int(items), column = int(label_list_for_col_header.index(label_list_for_col_header[0])).fill = PatternFill(start_color=str(color_lst[m]), end_color=str(color_lst[m]) , fill_type='solid')
general_lst = []
m +=1
I fixed the code as per the suggestions, however the loop executed only once. Not sure why
m= 0
for row in ws.iter_rows('D2:D11'):
for cell in row:
if label_list_for_col_header[m] in cell.value :
general_lst.append(cell.row)
for items in range(len(general_lst)):
ws.cell(row = general_lst[items], column = int(m+5)).fill = PatternFill(start_color=str(color_lst[m]), end_color=str(color_lst[m]) , fill_type='solid')
general_lst = []
m +=1
I finally got the code to work in case someone else is having the same problem, here is the working code:
m= 0
for lbls in label_list_for_col_header:
j= int(label_list_for_col_header.index(lbls))+5
for row in ws.iter_rows('D2:D11'):
for cell in row:
if lbls in cell.value :
general_lst.append(cell.row)
for items in range(len(general_lst)):
ws.cell(row = general_lst[items], column = j).fill = PatternFill(start_color=str(color_lst[m]), end_color=str(color_lst[m]) , fill_type='solid')
general_lst = []
m +=1