pythonwhile-loopiteration

Resetting "Last Row" in a While Statement


The objective is to take a value from A2 in an excel file and paste it into a specific location on another app. Then, take the value from B2 in the same excel file and paste it into a different location in another app. Once that's done, I'll take a screenshot, enter "Yes" in C2 of the excel file and repeat the process in row 3 of the excel file. I want to do this through the last row in the excel data set.

The underlying code won't accept a value of 0 from A2 (I need it to), and I'm getting this error:

import pyautogui as pyauto
import xlwings as xlw
import time
from pynput import mouse

wbmd = xlw.Book('Member_Details.xlsx')
md_coords = wbmd.sheets["Coords"]
mdlr = md_coords.cells (md_coords.cells.last_cell.row,3).end('up').row
dlr = md_coords.cells(md_coords.cells.last_cell.row,1).end('up').row + 1

mdilr = md_coords.cells(md_coords.cells.last_cell.row,3).end('up').row + 1

# print(f"mdlr is {mdlr}")
# print(f"dlr is {dlr}")
# print(f"mdilr is {mdilr}")

while mdilr < dlr:

    mdilr = md_coords.cells(md_coords.cells.last_cell.row,3).end('up').row + 1
    
    #Identify X Coord from Workbook.
    x_coord = md_coords.range(f"A{mdilr}").value

    #Identify Y Coord from Workbook.
    y_coord = md_coords.range(f"B{mdilr}").value

    #Click on Location Finder.
    pyauto.click(x=1097, y=827)
    time.sleep(.5)

    #Identify X Coord from Workbook.
    #x_coord = md_coords.range(f"A{mdilr}").value

    #Click on X Coord Box.
    pyauto.click(x=1038, y=784)
    time.sleep(.3)
    pyauto.typewrite(x_coord)

    #Identify Y Coord from Workbook.
    #y_coord = md_coords.range(f"B{mdilr}").value

    #Double-Click on Y Coord Box.
    pyauto.click(x=1189, y=786, clicks=2, interval=.3)
    time.sleep(.3)
    pyauto.typewrite(y_coord)

    #Double-Click on Find Button.
    pyauto.click(x=1263, y=786, clicks=2, interval=.3)

    # #Click Center of Target Tile.
    pyauto.click(x=1100, y=534, clicks=2, interval=.4)

    #Take and Save Screenshot
    im = pyauto.screenshot(region=(941,210, 324, 400))
    im.save(x_coord + '_' + y_coord + '.png')

    #Update workbook to show screenshot saved.
    md_coords.range(f"C{mdilr}").value = "Yes"

    #Resets the last row + 1
    mdilr = md_coords.cells(md_coords.cells.last_cell.row,3).end('up').row + 1

Error:

Traceback (most recent call last):   
  File "c:\Users\reber\Gaming\Rise of Castles\Member_Details\tuple.py", line 37, in <module>
    pyauto.typewrite(x_coord) 
  File "c:\Users\reber\Gaming\Rise of Castles\Member_Details\GetMemberDetails_venv\Lib\site-packages\pyautogui\__init__.py", line 594, in wrapper
    returnVal = wrappedFunction(*args, **kwargs)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
  File "c:\Users\reber\Gaming\Rise of Castles\Member_Details\GetMemberDetails_venv\Lib\site-packages\pyautogui\__init__.py", line 1682, in typewrite
    for c in message:
             ^^^^^^^ TypeError: 'float' object is not iterable

Solution

  • As the comments on the question have pointed out, the exception you're getting seems not to have anything to do with the contents of your excel file, or any of the other details you speak about in the intro to your question.

    Rather, it's a problem with the datatype of the value you're passing to pyauto.typewrite. That function expects a string (or an iterable of strings, perhaps) and you're passing it a float. You should be able to fix that bu adding a call to str, giving you pyauto.typewrite(str(x_coord)) (as suggested by Scott Hunter in the comments).