You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用keyboard/pyautogui写入长文本卡顿失效的技术求助

解决长文本写入记事本卡顿/失效问题

核心解决方案:放弃keyboard.write()或pyautogui.write()的逐字符模拟按键方式,改用系统剪贴板+Ctrl+V粘贴的方式传递内容,这是更稳定高效的文本输入方案。

问题原因

keyboard.write()和pyautogui.write()是通过逐字符模拟键盘输入实现文本写入,当文本长度增加时,系统处理按键事件的延迟会累积,导致丢字符、卡顿甚至失效。而剪贴板粘贴是一次性传递完整文本,不存在这类问题。

修改后的代码实现

方案1:使用pyperclip库(简单易用)

先安装依赖:

pip install pyperclip

修改循环写入部分:

import pygetwindow as gw
import pyautogui
import keyboard
from time import sleep
import pyperclip
import global_variables

def copiar():
    try:
        excel = win32.GetActiveObject("Excel.Application")
    except:
        print("Excel not found.")
        return

    workbook_name = excel.ActiveWorkbook.Name
    if workbook_name != 'CONEMB_ZLE.xlsx':
        print("Excel not found, open.")
        return

    excel.Visible = True
    sheet = excel.ActiveSheet
    global_variables.valor_global = sheet.Application.Selection.Text
    pyautogui.press('down')

while True:
    # 激活Excel窗口并复制内容
    excel_window = gw.getWindowsWithTitle('CONEMB_ZLE - Excel')[0]
    excel_window.activate()
    copiar()
    item = global_variables.valor_global
    
    # 激活记事本窗口
    notepad_window = gw.getWindowsWithTitle('transportadora - Bloco de notas')[0]
    notepad_window.activate()
    
    print(item)
    
    # 剪贴板传递内容+粘贴
    pyperclip.copy(item)
    pyautogui.hotkey('ctrl', 'v')
    
    sleep(0.5)  # 缩短不必要的等待时间
    keyboard.press('enter')

方案2:使用win32clipboard(无需额外安装依赖)

如果不想新增依赖,可以直接用Windows API操作剪贴板:

import pygetwindow as gw
import pyautogui
import keyboard
from time import sleep
import win32clipboard
import global_variables

def set_clipboard_text(text):
    win32clipboard.OpenClipboard()
    win32clipboard.EmptyClipboard()
    win32clipboard.SetClipboardText(text, win32clipboard.CF_UNICODETEXT)
    win32clipboard.CloseClipboard()

def copiar():
    try:
        excel = win32.GetActiveObject("Excel.Application")
    except:
        print("Excel not found.")
        return

    workbook_name = excel.ActiveWorkbook.Name
    if workbook_name != 'CONEMB_ZLE.xlsx':
        print("Excel not found, open.")
        return

    excel.Visible = True
    sheet = excel.ActiveSheet
    global_variables.valor_global = sheet.Application.Selection.Text
    pyautogui.press('down')

while True:
    excel_window = gw.getWindowsWithTitle('CONEMB_ZLE - Excel')[0]
    excel_window.activate()
    copiar()
    item = global_variables.valor_global
    
    notepad_window = gw.getWindowsWithTitle('transportadora - Bloco de notas')[0]
    notepad_window.activate()
    
    print(item)
    
    # 用Windows API设置剪贴板并粘贴
    set_clipboard_text(item)
    pyautogui.hotkey('ctrl', 'v')
    
    sleep(0.5)
    keyboard.press('enter')

注意事项

  • 确认记事本窗口标题完全匹配transportadora - Bloco de notas,避免找不到窗口的问题
  • 等待时间sleep()可根据实际运行情况调整,无需保留2秒的长等待

内容的提问来源于stack exchange,提问作者SirGG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 14:46:09