Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Trying to color an excel row based on list

The code below uses openpyxl and iterates through all rows and columns in a spreadsheet searching for a value in a list called subID. It then colors each cell it finds with that value light blue. I am trying to figure out how to color the whole row when a cell is found.

Any suggestions on how I can do this? I am not 100% sure but openpyxl may be forcing me to color each cell individually in the row.

#this colors the cell wherever sub id in list is found.

lightblueFill = PatternFill(start_color='c9e1f8',


# iterate over all rows
for ro in range(1,max_row+1):
    # iterate over all columns
    for co in range(1,max_column+1):
        # get particular cell value 
        #searches all cell and columns for a value in a list called subID
        if cell_obj.value in subID:
            #colors the cells found light blue. 
            cell_obj.fill = lightblueFill
I believe this link could help with that type of formatting. Skip to the first example under conditional formatting (the link is in the table of contacts to skip right to it). It shows how to color whole rows based off of an individual cell's value.

Conditional formatting with Python
newbieAuggie2019 and curranjohn46 like this post
Hi john,
I think this would help you in coloring entire row instead of individual cell

for y in range(1,max_column+1):
ws2.cell(row=ro,column=y).fill = lightblueFill

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Convert Excel to complex list and2handles 1 161 Jun-23-2020, 01:51 PM
Last Post: DPaul
  Make nested system directories based on an unsorted list? koebi 0 200 Mar-25-2020, 01:14 PM
Last Post: koebi
  How to list out specific excel files ajay_pal7 2 377 Mar-10-2020, 05:43 AM
Last Post: Larz60+
  Creating new list based on exact regex match in original list interjectdirector 1 270 Mar-08-2020, 09:30 PM
Last Post: deanhystad
  Filtering Excel Document Data Based On Numerical Values eddywinch82 30 2,864 Feb-25-2020, 06:08 PM
Last Post: eddywinch82
  Need to identify sheet color in excel workbook chewy1418 2 284 Feb-14-2020, 03:26 PM
Last Post: chewy1418
  Excel sheet by color abt_python 1 364 Jan-18-2020, 10:27 PM
Last Post: Clunk_Head
  Exporting list with dictionary to Excel veromi22 0 345 Oct-15-2019, 12:54 AM
Last Post: veromi22
  Openpyxl, format color of cells (cols) based on condition. genderbee 0 1,910 Sep-11-2019, 01:05 PM
Last Post: genderbee
  Create new list from another list based on condition khalidreemy 1 511 Jun-11-2019, 10:21 AM
Last Post: buran

Forum Jump:

Users browsing this thread: 1 Guest(s)