Python 在 Openpyxl 中识别单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30992146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Identifying cell in Openpyxl
提问by zfg
I've been working on a project, in which I search an .xlsx document for a cell containing a specific value "x". I've managed to get so far, but I can't extract the location of said cell.
我一直在研究一个项目,在该项目中,我在 .xlsx 文档中搜索包含特定值“x”的单元格。我已经设法做到了,但我无法提取所述单元格的位置。
This is the code I have come up with:
这是我想出的代码:
from openpyxl import load_workbook
wb = load_workbook(filename = 'Abstract.xlsx', use_iterators = True)
ws = wb.get_sheet_by_name(name = 'Abstract')
for row in ws.iter_rows():
for cell in row:
if cell.value == "E01234":
print "TRUE"
When I run this script, if there is a cell with the value "E01234" in the refereed .xlsx, it prints TRUE. What I need now is to get the row and column address (ie: A4) of the cell with the "E01234" value. Further down in my project,
当我运行这个脚本时,如果引用的 .xlsx 中有一个值为“E01234”的单元格,它会打印 TRUE。我现在需要的是获取具有“E01234”值的单元格的行和列地址(即:A4)。在我的项目中,
I wish to edit another cell on the same row as the identified one.
我希望编辑与已识别单元格同一行的另一个单元格。
Any insights?
任何见解?
采纳答案by ashwinjv
cell.row
and cell.column
should give you the addresses you need. Here is what I got from the documentation: https://openpyxl.readthedocs.org/en/latest/api/openpyxl.cell.html?highlight=.row#module-openpyxl.cell.cellIt gives you the list of attributes of a cell
object.
cell.row
并且cell.column
应该给你你需要的地址。这是我从文档中得到的:https://openpyxl.readthedocs.org/en/latest/api/openpyxl.cell.html?highlight=.row#module-openpyxl.cell.cell它为您提供了属性列表的一个cell
对象。