【问题标题】:Taking a text file and auto copy/paste/enter into GUI input field获取文本文件并自动复制/粘贴/进入 GUI 输入字段
【发布时间】:2021-04-02 01:42:12
【问题描述】:

我有一个包含 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()

【问题讨论】:

    标签: python pyautogui


    【解决方案1】:

    我认为你需要使用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
    

    【讨论】:

    • 我不确定我是否理解这样做的原因。我的想法是延迟上面代码的启动并拉起 GUI 并将光标悬停在输入框上。然后循环将获取 txt 文件的第一行并复制->粘贴->输入,然后单击返回输入框以重新开始 txt 文件中的第二行。这有可能吗?
    • 当您按下Ctrl + C时,当前选中的内容将被复制到剪贴板。但是,在这里,您实际上并没有这种方式,因为您的内容来自文件,并且您无法在使用鼠标之前选择它。如果你真的想做复制/粘贴方式,我建议你看看pyperclip
    猜你喜欢
    • 1970-01-01
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    • 2021-11-25
    • 2023-03-11
    • 2013-06-14
    • 1970-01-01
    相关资源
    最近更新 更多