【Python】win32api的打开程序及按键操作、win32clipboard的剪切板操作

时间:2021-09-02 05:58:11   收藏:0   阅读:10
import time

import win32api
import win32clipboard
import win32con

timespan = 0.3

if __name__ == ‘__main__‘:
    win32api.ShellExecute(0, ‘open‘, ‘notepad.exe‘, ‘‘, ‘‘, 1)
    time.sleep(timespan)

    # 按下ctrl+v
    win32api.keybd_event(0x11, 0, 0, 0)
    win32api.keybd_event(0x56, 0, 0, 0)
    win32api.keybd_event(0x56, 0, win32con.KEYEVENTF_KEYUP, 0)
    win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)
    time.sleep(timespan)

    # 按下ctrl+s
    win32api.keybd_event(0x11, 0, 0, 0)
    win32api.keybd_event(0x53, 0, 0, 0)
    win32api.keybd_event(0x53, 0, win32con.KEYEVENTF_KEYUP, 0)
    win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)
    time.sleep(timespan)

    filename = ‘dilexliu.log‘
    win32clipboard.OpenClipboard()
    win32clipboard.EmptyClipboard()
    win32clipboard.SetClipboardText(filename)
    win32clipboard.CloseClipboard()
    time.sleep(timespan)

    # 按下ctrl+v
    win32api.keybd_event(0x11, 0, 0, 0)
    win32api.keybd_event(0x56, 0, 0, 0)
    win32api.keybd_event(0x56, 0, win32con.KEYEVENTF_KEYUP, 0)
    win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)
    time.sleep(timespan)

    # 按下回车
    win32api.keybd_event(0x0D, 0, 0, 0)
    win32api.keybd_event(0x0D, 0, win32con.KEYEVENTF_KEYUP, 0)
    time.sleep(timespan)

原文:https://www.cnblogs.com/dilex/p/15211427.html

评论(0)
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!