Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identifying cell in Openpyxl

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.

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,

I wish to edit another cell on the same row as the identified one.

Any insights?

like image 583
zfg Avatar asked Jun 23 '15 00:06

zfg


1 Answers

cell.coordinate will return the cells address in "A1" style.

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.cell It gives you the list of attributes of a cell object.

like image 89
ashwinjv Avatar answered Sep 26 '22 06:09

ashwinjv