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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With