微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决获取文本文件并自动复制/粘贴/进入 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。