0

I am working on an openpyxl script and am hitting a roadblock on referencing a row number through an iteration of 'for row in sheet.iter_rows'. When attempting to call it without values_only=True it will pull the tuple of the true coordinates of the populated cells. If I call it with values_only=True, it pulls the values of each populated cell.

I'm attempting to fill in column G of whichever row the iteration is on with a formula based on two cells on the same row. I've found a way to get the row number if I am doing it based directly on cell coordinate, but iterating through all of the cells would be inefficient. 4 row iteration > 30 cell iteration.

--Not a duplicate of accessing the index in 'for' loops. That would be calling a specific section of the list generated by p1.iter_rows(min_row=2, values_only=True, I'm trying to reference the row # designation that the for loops is currently on. E.G. If it is on row 2, it would place a 2 after 'D', 'C', or 'G' in the code above. Enumerate would be doing the same thing. I don't need access to the list itself, I need the categorical reference number of the row that is currently being iterated

    for row in p1.iter_rows(min_row=2, values_only=True):
        cvalue = p1['D' + str(row)]
        msrp = p1['C' + str(row)]
        p1['G' + str(row)] = msrp * int(.0825) + cvalue
        test = p1.row.coordinate

Corrected code:

for row in p1.iter_rows(min_row=2, min_col=3, max_col=3):
    for cell in row:
        xy = coordinate_from_string(cell.coordinate)
        colcoor = column_index_from_string(xy[0])
        rowcoor = xy[1]
        cval = p1['C' + str(rowcoor)]
        pval = p1['D' + str(rowcoor)]
  • Can you use [enumerate](https://docs.python.org/3/library/functions.html#enumerate)? – wwii Aug 20 '19 at 18:00
  • Possible duplicate of [Accessing the index in 'for' loops?](https://stackoverflow.com/questions/522563/accessing-the-index-in-for-loops) – wwii Aug 20 '19 at 18:01
  • Not a duplicate of accessing the index in 'for' loops. That would be calling a specific section of the list generated by p1.iter_rows(min_row=2, values_only=True, I'm trying to reference the row # designation that the for loops is currently on. E.G. If it is on row 2, it would place a 2 after 'D', 'C', or 'G' in the code above. Enumerate would be doing the same thing. I don't need access to the list itself, I need the categorical reference number of the row that is currently being iterated. – hohenbuehelia Aug 20 '19 at 18:06
  • Would the current row be the enumerate value plus the `min_row` argument? – wwii Aug 20 '19 at 18:27
  • Possible duplicate of [how to get the current row index with Openpyxl](https://stackoverflow.com/questions/42881099/how-to-get-the-current-row-index-with-openpyxl) – wwii Aug 20 '19 at 18:42
  • I was able to solve it by doing mapping the cell coordinate of a single column. Enumeration would have worked, but it would have required two or three nested statements and I didn't like it counting based on list # instead of coordinate. Thanks for the help. – hohenbuehelia Aug 20 '19 at 18:58
  • [Can I answer my own question?](https://stackoverflow.com/help/self-answer) – wwii Aug 20 '19 at 19:08

0 Answers0