Python Forum
Trying to search excel for a value in row then format row
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to search excel for a value in row then format row
#1
Hi,

I am using openpyxl to find and print values in a column of a spreadsheet. and then want to search for those values in a second spreadsheet, and format the row (color it orange to be exact). I need help on understanding how to search for the the printed values in the second spreadsheet.

Here is how I get the values:
from openpyxl import load_workbook
IntLiveFile = "C:\\Users\\Jcurran\\weeklylive.xlsx"
wb=load_workbook(IntLiveFile)
wb.sheetnames
ws = wb.active
#Extracts the subscriber number for accounts that went live last week.
for row in ws.iter_rows("L"):
    for cell in row:
        if cell.value == 'Integration Live':
            print(ws.cell(row=cell.row, column=3).value) 
#above code gives me desired output from spreadsheet:
141680
469897
48670
281174

I then want to search another spreadsheet for those values. Can I somehow output those above numbers (instead of print?) in a manner where I can then iterate through rows in the second spreadsheet and find each one? Then I will figure out the color formatting which I know how to do. Any thoughts or hints appreciated. I am new at this and feeling stuck.

Thanks in advance!
Reply
#2
Instead of printing the output, you could just store them in a list object. I.e.
valueList = []
for row in ws.iter_rows("L"):
    for cell in row:
        if cell.value == 'Integration Live':
            valueList.append((ws.cell(row=cell.row, column=3).value)) 
Then open the second spreadsheet and do a similar search, checking the values against the items in the list.
for row in ws2.iter_rows("L"):
    for cell in row:
        if cell.value in valueList:
            <do something fun>
Reply
#3
Thanks Tim. Makes total sense now. And it worked of course. Appreciate it!!
Reply
#4
Another mental hurdle I am trying to get past.

The code below will color the individual cell blue if the value is found in column B. But I am trying to figure out how to color the entire row. I have tried a few approaches to no avail so far. Any tips?

lightblueFill = PatternFill(start_color='c9e1f8',
                   end_color='c9e1f8',
                   fill_type='solid')

for row in ws2.iter_rows(range_string="B"):
    for cell in row:
        if cell.value in subID:
            cell.fill = lightblueFill
Reply
#5
I found this with a quick search on Stack Overflow. Are you familiar with the Stack Exchanges? They're a great resource too.

https://stackoverflow.com/questions/4298...w-openpyxl
Reply
#6
I am familiar with stack exchange. I look everywhere for examples.

I can specify a column and/or row and format it, color it. That is no issue.

My challenge is this: once I iterate through all the rows in a file, then find the value I am looking for in a particular column, how do I format or color that entire row? I can only seem to figure out ways to do it for an individual cell. For example this format one cell in the row but I am trying to figure out how to rewrite it so that the whole row will be colored:

lightblueFill = PatternFill(start_color='c9e1f8',
                   end_color='c9e1f8',
                   fill_type='solid')
 
for row in ws2.iter_rows(range_string="B"):
    for cell in row:
        if cell.value in subID:
            cell.fill = lightblueFill   #####I want to change this line to something like row.fill
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Search Excel File with a list of values huzzug 4 1,215 Nov-03-2023, 05:35 PM
Last Post: huzzug
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 1,867 Dec-12-2022, 08:22 PM
Last Post: jh67
  How to format Excel column with comma? dee 0 1,358 Jun-13-2022, 10:11 PM
Last Post: dee
  Converting cells in excel to JSON format desmondtay 4 1,724 May-23-2022, 10:31 AM
Last Post: Larz60+
  Need Help writing data into Excel format ajitnayak87 8 2,503 Feb-04-2022, 03:00 AM
Last Post: Jeff_t
  Exporting dataframes to excel without loosing datetime format Rafa 0 1,229 Oct-27-2021, 10:42 AM
Last Post: Rafa
Smile Set 'Time' format cell when writing data to excel and not 'custom' limors 3 6,274 Mar-29-2021, 09:36 PM
Last Post: Larz60+
  Openpyxl tkinter search a value in Excel column Heathcliff_1 0 3,252 Dec-02-2020, 04:35 PM
Last Post: Heathcliff_1
  String search in different excel Kristenl2784 0 1,700 Jul-20-2020, 02:37 PM
Last Post: Kristenl2784
  copy/pasting in excel WHILE keep file format zarize 0 1,943 Jun-23-2020, 03:51 PM
Last Post: zarize

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020