Use pywin32 to simulate mouse and keyboard operations in windows

Source: Internet
Author: User

Because you need to use a software to process a batch of eps files in batches, You Need To simulate mouse and keyboard actions so that they can be operated automatically.

Copy codeThe Code is as follows:
#-*-Coding: UTF-8 -*-
Import OS
Import time
Import win32gui
Import win32api
Import win32con
From PIL import ImageGrab

# OS. startfile ("D :\\ artcut6 \ Prog \ Artcut6.exe ")
# Time. sleep (1)

Wdname1 = u "2009 []-[No title-1]"
W1hd = win32gui. FindWindow (0, wdname1)
Print w1hd
W2hd = win32gui. find1_wex (w1hd, None)
Print w2hd

Def aotohelper_wt (I ):
# Retrieve window focus
Win32gui. SetForegroundWindow (w2hd)
# Shortcut: Alt + F
Win32api. keybd_event (,) # Alt
Win32api. keybd_event (, 0,) # F
Win32api. keybd_event (, win32con. KEYEVENTF_KEYUP, 0) # Release the button
Win32api. keybd_event (18,0, win32con. KEYEVENTF_KEYUP, 0)

# Shortcut key I
Win32api. keybd_event (,) # I
Win32api. keybd_event (73,0, win32con. KEYEVENTF_KEYUP, 0)

Time. sleep (0.2)
Wdname3 = u "open"
W3hd = win32gui. FindWindow (None, wdname3) # "open" window handle
# Print w3hd

# Win32gui. MoveWindow (w3hd, 50, 50,300,200, True)
If I <= 9:
Msg = "YC-00" + str (I)
Elif 10 <= I <= 99:
Msg = "YC-0" + str (I)
Else:
Msg = "YC-" + str (I)
Edithd = win32gui. find1_wex (w3hd, None, "Edit", None)
Win32api. SendMessage (edithd, win32con. WM_SETTEXT, None, msg)
Time. sleep (0.1)
# Btnhd = win32gui. find1_wex (w3hd, None, "BUTTON", None)
# Print btnhd
# Simulate the shortcut key Alt + O
Win32api. keybd_event (,) # Alt
Win32api. keybd_event (79,0, 0,0) # O
Win32api. keybd_event (79,0, win32con. KEYEVENTF_KEYUP, 0) # Release the button
Win32api. keybd_event (18,0, win32con. KEYEVENTF_KEYUP, 0)
Time. sleep (0.1)
# Simulate mouse operations
Win32api. SetCursorPos ([30,150]) # Set a position for the mouse focus
Win32api. mouse_event (win32con. MOUSEEVENTF_LEFTDOWN, 0, 0, 0)
Win32api. SetCursorPos ([500,500])
Win32api. mouse_event (win32con. MOUSEEVENTF_LEFTUP, 0, 0, 0)
Time. sleep (0.1)
# Simulate the shortcut key F7 (Limit observation)
Win32api. keybd_event (,) # F7
Win32api. keybd_event (118,0, win32con. KEYEVENTF_KEYUP, 0) # Release the button
Time. sleep (0.2)
# Use PIL screenshot
Path = "C :\\ Users \ LY \ Desktop \ pic \\"
Filename = "YC-" + str (I) + ". jpg"
Im = ImageGrab. grab ()
Im. save (path + filename)

# Simulate the shortcut key F8 (return to the original page size)
Win32api. keybd_event (119,0, 0, 0) # F8
Win32api. keybd_event (119,0, win32con. KEYEVENTF_KEYUP, 0) # Release the button

# Simulate keyboard event delete
Win32api. keybd_event (,) # Delete
Win32api. keybd_event (, win32con. KEYEVENTF_KEYUP, 0) # Release the button
Time. sleep (1)

For I in range (2, 85 ):
Aotohelper_wt (I)
Print I
Print "work done! "

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.