How to print the next row every time a function runs openpyxl

0 votes

In order to complete a form on a website, I am using openpxyl to extract values from a certain row and column in an excel document. I complete the form, save it, and then fill out the following form using the values from the following row of the excel sheet.

Excel looks like this:

First Name         Last Name         DOB

George             Hill              9/9/99
Sam                Genius            8/8/88
Bill               Smith             7/7/77

I want to run the below function three times and each time it runs prints out the next row:

 def AddFirstName():
     for i in range(3):
       for row in ws.iter_rows(min_row=1, min_col=1, max_row=3,max_col=1):
         for cell in row:
            print(cell.value)
                
            break
       break  # Tried to use breaks to break the loop so it only prints once for each loop

AddFirstName()
AddFirstName()
AddFirstName()

OR just run it outside the function like this:

    for i in range(3):
       for row in ws.iter_rows(min_row=1, min_col=1, max_row=3,max_col=1):
         for cell in row:
            print(cell.value)
                
            break
       break  # Tried to use breaks to break the loop so it only prints once for each loop

This prints:

George
George
George

How do I fix this so that it prints:

George             
Sam                
Bill 
Jan 7 in Others by Kithuzzz
• 28,700 points
67 views

1 answer to this question.

0 votes

Verify the indentation. I executed the sample code below, and the outcomes were as anticipated:

sheetData = [
    ['George'],
    ['Bill'],
    ['Mike']
]
for i in range(3):
    for row in sheetData:
        for cell in row:
            print(cell)
            break
    break  # indent under second 'for' 

#output:
George
Bill
Mike

If that is not the problem, make sure that each time you cycle through the loop, ws.iter rows() actually gives you a new or the next row. Set a breakpoint there and check or keep an eye on the value of "row" each time.

answered Jan 7 by narikkadan
• 53,160 points

Related Questions In Others

0 votes
1 answer

How to find the time complexity of a program?

Hi@akhtar, The first loop is O(N) and the ...READ MORE

answered Oct 15, 2020 in Others by MD
• 95,420 points
563 views
0 votes
1 answer
0 votes
1 answer

Excel VBA : HOW TO PRINT THE TEXT IN A CELL (like Wrap Text)

Use a LineFeed character to create a ...READ MORE

answered Oct 27, 2022 in Others by narikkadan
• 53,160 points
186 views
0 votes
1 answer

How to insert a new row in the specified cell only, of Excel sheet using c#?

I have this worksheet with a matrix ...READ MORE

answered Nov 24, 2022 in Others by narikkadan
• 53,160 points
264 views
0 votes
1 answer

ImportError: openpyxl is required for loading excel format files

Forget the PsychoPy complications for the time ...READ MORE

answered Oct 3, 2018 in Python by Priyaj
• 58,100 points
477 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 3,323 views
0 votes
1 answer

How to find the last row in a column using openpyxl normal workbook?

ws.max_row will give you the number of rows ...READ MORE

answered Dec 25, 2022 in Others by narikkadan
• 53,160 points
375 views
0 votes
1 answer

How do I align a UserForm next to the active cell?

Answer to Q1 - Yes, it's correct. In ...READ MORE

answered Oct 27, 2022 in Others by narikkadan
• 53,160 points
183 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP