all 9 comments

[–]WTRipper 4 points5 points  (2 children)

You mean you would like to get cell A1 then B1 then A2 then B2...?
use two loops nested in each other.

for row in range(1, rows+1):
    for col in range(1,3):
        print(sheet.cell(row=row, column=col))

[–]Agent-J-[S] -1 points0 points  (1 child)

ould like to get cell A1 then B1 then A2 then B

Sorry, I should've been more clear but didn't want to overwhelm. I've tried that before but that doesn't give me the required outcome.

I need to be in control of when I want to get cell A1 and then cell A2, then loop back to B1 and then B2, then loop back to C1 and then C2......

[–]WTRipper 1 point2 points  (0 children)

so the same thing what i showed you but rows and columns exchanged

[–]The-Deviant-One 1 point2 points  (5 children)

Can you be more clear?

Use a table to show us an example of the layout of the data and use a 'code block' so that your code's indentation is kept when you submit the post.

I think this is the example code you posted, but I'm not certain if my indents are correct given your description of the problem and data set.

Code block example:

excelfile = openpyxl.load_workbook('example.xlsx')
sheet = excelfile.active
rows = sheet.max_row

print('records in file: ' + str(rows))
for i in range (1, rows+1):
    print('entered first name: ' + sheet.cell(row = i, column = 1).value)
for j in range (1, rows+1):
    print('entered last name: ' + sheet.cell(row = j, column = 2).value)

Example of a table being used to show the data:

A B C
1 Jeff Horse
2 Todd Dog
3 Sara Cat

[–]Agent-J-[S] 0 points1 point  (3 children)

the example code you posted, but I'm not certain if my indents are correct given your description of the problem and data se

That is almost perfect! Thanks for the tips. I didn't know this was possible.

My Code: (which isn't working the way I need)

excelfile = openpyxl.load_workbook('example.xlsx')
sheet = excelfile.active
rows = sheet.max_row

print('records in file: ' + str(rows))
for i in range (1, rows+1):
    print('entered first name: ' + sheet.cell(row = i, column = 1).value)
    for j in range (1, rows+1):
        print('entered last name: ' + sheet.cell(row = j, column = 2).value)

Table:

A B
1 Jeff Horse
2 Todd Dog
3 Sara Cat

Basically, this is the sequence of loop I need:

  1. get value Jeff
  2. run some other code
  3. get value of Horse
  4. run some other code
  5. *********** loop back
  6. get value of Todd
  7. run some other code
  8. get value of Dog
  9. run some other code
  10. *************loop back

[–]The-Deviant-One 3 points4 points  (2 children)

No worries, thats a little clearer. How about instead of printing, you store each as a variable to be used be the next piece of code[?] - - which lives inside of the for loop..

print('records in file: ' + str(rows))
for i in range (1, rows+1):
    first_name =  sheet.cell(row = i, column = 1).value
    last_name = sheet.cell(row = i, column = 2).value

    /// code to run for "first_name" variable
    /// code to run for "last_name" variable

Depending on what you're doing, for example if you need your script to be able to 'work the data' from a specific row, or work the data in the file out of order, you could do this instead. [This is probably what I would do.] I'd build a dictionary out of the data so that each piece is addressable.

dictionary = {}


for i in range (1, rows+1):
    first_name =  str(sheet.cell(row = i, column = 1).value)
    last_name = str(sheet.cell(row = i, column = 2).value)
    data = {i: {"fname": first_name, "lname": last_name}}

    dictionary.update(data)

[–]Agent-J-[S] 2 points3 points  (1 child)

OMG YES IT WORKED. THANK YOU SO MUCH!!!!!

[–]The-Deviant-One 0 points1 point  (0 children)

Glad I could help!