0
from openpyxl import load_workbook

def Email_ID(row_Num, cell_Num, sheet):
    wb = load_workbook('/Users/Umesh/Downloads/Test_Read.xlsx')
    sheet = wb.get_sheet_by_name(sheet)
    email = sheet.cell(row=row_Num, column=cell_Num).value

    if email is not None:
        return email
    else:
        return ''

Using the above code I am able to get a single emailid from the excel sheet and register it in the application, but I need to read all the email ids from the excel sheet one by one by using FOR loop.

can any one help.

Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
py_rot
  • 3
  • 1

1 Answers1

0

I noticed there is not a main entry point to your python script so I incorporated a more abstract version of your code. Besides that I do not understand why you passed in a sheet to def Email_ID when you instantiated one(sheet = wb.get_sheet_by_name(sheet)) inside your function.

I am assuming you mean to get all emails from first column, if not a small change can be made to grab emails vertically.

Besides that, I have tested this code and it works on Mojave MacOS. This python script will get all values from first column dynamically:

from openpyxl import load_workbook

def main():
    curr_sheet = get_sheet()
    for i in range(1, curr_sheet.max_row + 1):
        print(email_id(i, 1, curr_sheet))

# get email from each row based on parameters passed
def email_id(row_Num, cell_Num, sheet):
    email = sheet.cell(row=row_Num, column=cell_Num).value

    if email is not None:
        return email
    else:
        return ''

# setup our sheet to be easily passed through various functions
def get_sheet():
    wb = load_workbook('/Users/sudoxx2/Downloads/test_sheet.xlsx')
    sheet = wb.active
    return sheet

# main entry point to our python script(universal entry point in most python scripts)
if __name__ == "__main__":
    main()

Please let me know if you have any questions.

sudoxx
  • 423
  • 1
  • 4
  • 9