user6597365
user6597365

Reputation: 21

Using openpyxl to search for a cell in one column and then to print out the row for that relevant cell

For instance, I want to be able to type into my program via user input the data and then print the row relevant to that cell. Ideally, if 1/08/2016 was inputted into the program then it will run through column 'A' and then locates the relevant date. If date is found then it will print the date and then the relevant data in that row. This is what I have currently below. Any type of suggestions will be great if a direct answer cannot be found.

EDIT: I changed x to date.

from openpyxl import *

wb = load_workbook('C:/Users/W-_-C/PycharmProjects/IT SAT_data doc.xlsx')
ws = wb.get_sheet_by_name('101')
date = input("Prompt: ")
for row in ws.iter_rows('A{}:A{}'.format(ws.min_row, ws.max_row)):
    for cell in row:
        if ws.cell(row=row, column=0).value == date:
            print(ws.cell.value)

Upvotes: 2

Views: 20310

Answers (1)

The Dude
The Dude

Reputation: 1118

If you only want to look in column A for the input I would do something like this:

for i in range(1,ws.max_row):
    if ws.cell(row=row, column=0).value == date:
        for j in range(i, ws.max_column):
            print (ws.cell(row=i, column=j).value)

Assuming you want to print every column on the row matching the input

Upvotes: 2

Related Questions