Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Excel File reading
#1
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.
Reply
#2
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}')
Output:
Taxi at row: 4 and column: 1 In cell <Cell 'sheet1'.A4>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to convert .trc/.txt file into excel using python ebola 3 1,927 Jan-15-2023, 10:37 PM
Last Post: Yoriz
  reading from a file looseCannon101 14 4,753 Jul-18-2020, 11:29 AM
Last Post: GOTO10
  Weird problem with reading from file and performing calculations pineapple999 1 2,956 Jul-25-2019, 01:30 AM
Last Post: ichabod801
  Handling IO Error / Reading from file Expel 10 4,717 Jul-18-2019, 01:21 PM
Last Post: snippsat
  Reading an Unconventional CSV file OzSbk 2 3,830 May-17-2019, 12:15 PM
Last Post: MvGulik
  reading text file and writing to an output file precedded by line numbers kannan 7 10,246 Dec-11-2018, 02:19 PM
Last Post: ichabod801
  Read directly from excel file using python script dvldgs05 0 2,228 Oct-19-2018, 02:51 AM
Last Post: dvldgs05
  Reading of structured .mat (matlab) file sumit 2 3,369 May-24-2018, 12:12 PM
Last Post: sumit
  File Reading toxicxarrow 9 5,103 May-07-2018, 04:12 PM
Last Post: toxicxarrow
  reading all lines from a text file seadoofanatic 2 2,886 Mar-13-2018, 06:05 PM
Last Post: Narsimhachary

Forum Jump:

User Panel Messages

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