Looping a function, counting how many times its been looped and using that count in function to change a parameter

What i’m trying to do is add +1 for each time a function is ran and then use that new number in the function to specify a new field in the .xls document using Pandas. The function i posted below is ran when a button is pressed using tkinter. Currently it only reads row 1 (a2,b2) in the .xls document.
What i want to do is run this function again or loop and every pass +=1 to the number specified as Row. I know i can use len(fd) to count how many rows the excel sheet has so i can set it to stop when it hits that number. Any help with this would be great i’ve kinda been stuck on this for a couple days and haven’t really got anywhere.

def Run_bot():
 PATH = "C:\Program Files (x86)\chromedriver.exe"
 driver = webdriver.Chrome(PATH)
 df = pd.read_excel("designs.xls", usecols=("State", "TemplateName"))
 Row = 1
 TemplateName = (df.loc[Row, 'State'])
 Design = (df.loc[Row, 'TemplateName'])

  ##Loop from here down
  time.sleep(1)  # gives time for page to respond

  DesName = driver.find_element_by_xpath('//*[@id="Name"]')
  FileName = driver.find_element_by_xpath("//*[@id='File_chosen']/a")
  FileSearch = driver.find_element_by_xpath(
  CreateBtn = driver.find_element_by_xpath(

  DesName.send_keys(TemplateName)  # Inputs template name
  time.sleep(1)  # waits 1 second for page to respond
  FileName.click()  # clicks on search box
  FileSearch.send_keys(Design + "\n")  # types in Design and hits enter
  time.sleep(1)  # waits 1 seconds for art to load

  CreateBtn.click()  # Clicks to create template```

Usually you’d do this with a for-loop:

for Row in range(len(fd)):

to see that happen; note that the above counts from 0 through to
len(fd)-1. Put the whole per-Row code inside the loop.

If you wanted to explicitly count up to len(fd) you could go:

Row = 0
while Row < len(fd):
    ... the main per-Row work goes here ...
    Row += 1

but the for-loop is more succinct, more readabe and more conventional.
You’d use a while loop in situations where Row do not always increment
by 1, but that is not your situation.

Also note: I’m counting from 0, your code starts at 1. I have not
checked which is correct.

Also, one final remark: it is usually best to write Windows paths as
“raw” python strings, eg:

PATH = r"C:\Program Files (x86)\chromedriver.exe"

That leading “r” means that the backslash is not special within the
quotes. Because the backslash is special to both Python and windows
paths, stopping Python interpreting it is important. You get away with
it here, but if there was, say, a “\n” in the path Python would replace
the backslash and “n” with a newline character in the string. Not what
you intend. With a “raw” string that will not happen.

Cameron Simpson cs@cskk.id.au

1 Like

For-loops are also faster than while loops.

Also, it is not generally correct to write Windows paths using raw
strings. Raw strings are designed for regexes, and there are legal
Windows paths which are illegal raw strings.

Just use forward slashes. Windows is perfectly capable of accepting them
instead of backslashes.