简体   繁体   English

Python Openpyxl在xlsx中找不到数字

[英]Python Openpyxl can't find number in xlsx

Python 3.7.1 的Python 3.7.1

I have an excel which looks like something like this 我有一个看起来像这样的excel

xls example xls示例

With my script I would like to find the row where is "222" located and update this line. 使用我的脚本,我想找到“ 222”所在的行并更新此行。 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) 如果单元格包含文本,则脚本将打印出单元格编号(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. 我已经在网上搜索了,但是没有找到解决方案,为什么脚本在我的xls中找不到数字。

Thank you in advance! 先感谢您!

Problem looks to be line 2: issue = "222" . 问题看起来像是第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. 您将issue定义为包含数字222的字符串。相反,只需将其更改为issue = 222 ,它应该可以按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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