Excel File reading - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Excel File reading (/thread-33117.html) |
Excel File reading - vanjoe198 - Mar-30-2021 Hello, I am doing an assignment and not sure what im doing wrong. My job is to search the columns and rows of an excel file using openpxyl. So far I have got this code done. import openpyxl def functionOne (): print("Please enter the word you would like to find in the excel sheet.") userGuess = input() sampleWorkbook = openpyxl.load_workbook("c:\\Assignment.xlsx") sheet = sampleWorkbook.active for col in sheet.iter_cols(min_row=1, max_col=4, max_row=100): for cell in col: if userGuess == cell: print(cell) functionOne ()So, I need to output the column and row ID if the word was found in the list. Each column has 100 words and there is 4 rows. When I have entered a word that I know thats in the file I get nothing back. I have tried replacing the part of the code: if userGuess == cell: print(cell) with print (cell) and I get the word that is in the file outputted back to me. I'm new to programming so Im not sure what I'm doing wrong here. Thanks. RE: Excel File reading - snippsat - Mar-31-2021 If you start loop on iter_rows instead ,then can get back info like cell.value, cell.row..,ect.Example: from openpyxl import Workbook import openpyxl file = "email.xlsx" wb = openpyxl.load_workbook(file) ws = wb.worksheets[0] search_word = 'Taxi' for row in ws.iter_rows(): for cell in row: if search_word in cell.value: print(f'{cell.value} at row: {cell.row} and column: {cell.column} In cell {cell}')
|