获取文本文件并自动复制/粘贴/进入 GUI 输入字段

问题描述

我有一个包含 1,000 个索引号的文本文件。我需要复制-> 粘贴-> 将每一行逐行输入到 GUI 输入字段中。如何将每个“行”合并到热键中并重复?

with open(file) as f:
    for line in f:
        pyautogui.hotkey('ctrl','c')
        pyautogui.hotkey('ctrl','v')
        pyautogui.press('enter')
        pyautogui.click()

解决方法

我认为您需要使用 pyautogui.typewrite() (documentation)。

我将使用的方法是将光标移动到文本字段,单击它,添加一点延迟,然后使用上述方法。

示例(某种伪代码):

pyautogui.moveTo(x,y)  # location coordinates of the text field
pyautogui.click()

with open(file) as f:
    for line in f:        
        time.sleep(0.5)       # add a bit of delay to simulate human-like behavior so that nothing gets out of sync
        pyautogui.typewrite(line)
        pyautogui.moveTo(x1,y1) # location of the button you need to press to process the current input or anything else you need to do
        
        # further processing in the GUI

        pyautogui.moveTo(x,y)  # location coordinates of the text field
        pyautogui.click()
        pyautogui.typewrite("")  # clear it out for next input