简体   繁体   中英

Python Openpyxl can't find number in xlsx

Python 3.7.1

I have an excel which looks like something like this

xls example

With my script I would like to find the row where is "222" located and update this line. I write a script which can find that kind of row but only that case if the cell contains text.

The script:

from openpyxl import load_workbook
issue = "222"
path = "\\.xlsx"

wb = load_workbook(path)

ws = wb.worksheets[0]
for row in ws.iter_rows():
   for cell in row:
       if cell.value == issue:
           print (cell.coordinate)

Outcome:

  • If the cell contain numbers the result is nothing. There is no error and nothing
  • If the cell contains text then the script print out the cell number (B3)

The problem in my case that the cells are only contains numbers. I already searched on the web but didn't find any solution why the script don't find number in my xls.

Thank you in advance!

Problem looks to be line 2: issue = "222" . You defined issue as a string containing the numbers 222. Instead just change that to issue = 222 and it should work as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM