This article mainly introduces how to simulate the mouse drag operation in python. The example analyzes the skills of the Python mouse operation and the key bit operation, which has some reference value, for more information, see the following example. Share it with you for your reference. The details are as follows:
The bookmarks in pdf only contain page numbers. you have to drag the existing bookmarks to a directory and add your own tabs. Repeated drag operations are boring. let the program help me implement them. I can have some water and have a rest.
1. Python code
The code is as follows:
#
# _ * _ Coding: UTF-8 _*_
_ Author _ = 'WP'
Import win32api
Import win32con
Import win32gui
From ctypes import *
Import time
SW = 1, 1377
SH = 1, 768
VK_CODE = {
'Backspace': 0x08,
'Tab': 0x09,
'Clear': 0x0C,
'Enter': 0x0D,
'Shift ': 0x10,
'Ctrl ': 0x11,
'Alt': 0x12,
'Pause': 0x13,
'Caps _ lock': 0x14,
'Esc ': 0x1B,
'Spacebar ': 0x20,
'Page _ up': 0x21,
'Page _ low': 0x22,
'End': 0x23,
'Home': 0x24,
'Left _ Arrow': 0x25,
'Up _ Arrow': 0x26,
'Right _ Arrow': 0x27,
'Down _ Arrow': 0x28,
'Select': 0x29,
'Print ': 0x2A,
'Execute ': 0x2B,
'Print _ screen': 0x2C,
'Ins': 0x2D,
'Del ': 0x2E,
'Help': 0x2F,
'0': 0x30,
'1': 0x31,
'2': 0x32,
'3': 0x33,
'4': 0x34,
'5': 0x35,
'6': 0x36,
'7': 0x37,
'8': 0x38,
'9': 0x39,
'A': 0x41,
'B': 0x42,
'C': 0x43,
'D': 0x44,
'E': 0x45,
'F': 0x46,
'G': 0x47,
'H': 0x48,
'I': 0x49,
'J': 0x4A,
'K': 0x4B,
'L': 0x4C,
'M': 0x4D,
'N': 0x4E,
'O': 0x4F,
'P': 0x50,
'Q': 0x51,
'R': 0x52,
'S ': 0x53,
'T': 0x54,
'U': 0x55,
'V': 0x56,
'W': 0x57,
'X': 0x58,
'Y': 0x59,
'Z': 0x5A,
'Numpad _ 0': 0x60,
'Numpad _ 1': 0x61,
'Numpad _ 2': 0x62,
'Numpad _ 3': 0x63,
'Numpad _ 4': 0x64,
'Numpad _ 5': 0x65,
'Numpad _ 6': 0x66,
'Numpad _ 7': 0x67,
'Numpad _ 8': 0x68,
'Numpad _ 9': 0x69,
'Multiply _ key': 0x6A,
'Add _ key': 0x6B,
'Separator _ key': 0x6C,
'Subtract _ key': 0x6D,
'Decimal _ key': 0x6E,
'Ide _ key': 0x6F,
'F1': 0x70,
'F2': 0x71,
'F3 ': 0x72,
'F4': 0x73,
'F5': 0x74,
'F6 ': 0x75,
'F7 ': 0x76,
'F8': 0x77,
'F9': 0x78,
'F3': 0x79,
'F11': 0x7A,
'F12': 0x7B,
'F13': 0x7C,
'F14': 0x7D,
'F15': 0x7E,
'F16': 0x7F,
'F17': 0x80,
'F18': 0x81,
'F19': 0x82,
'F20': 0x83,
'F21': 0x84,
'F22': 0x85,
'F23': 0x86,
'F24': 0x87,
'Num _ lock': 0x90,
'Scroll _ lock': 0x91,
'Left _ shift ': 0xA0,
'Right _ shift ': 0xA1,
'Left _ control': 0xA2,
'Right _ control': 0xA3,
'Left _ menu ': 0xA4,
'Right _ menu ': 0xA5,
'Browser _ back': 0xA6,
'Browser _ forward ': 0xA7,
'Browser _ refresh': 0xA8,
'Browser _ stop': 0xA9,
'Browser _ search': 0xAA,
'Browser _ favorites ': 0xAB,
'Browser _ start_and_home ': 0xAC,
'Volume _ mute ': 0xAD,
'Volume _ low': 0xAE,
'Volume _ up': 0xAF,
'Next _ track': 0xB0,
'Previous _ track': 0xB1,
'Stop _ media ': 0xB2,
'Play/pause_media ': 0xB3,
'Start _ Mail': 0xB4,
'Select _ media ': 0xB5,
'Start _ application_1 ': 0xB6,
'Start _ application_2 ': 0xB7,
'Attn _ key': 0xF6,
'Crsel _ key': 0xF7,
'Exsel _ key': 0xF8,
'Play _ key': 0xFA,
'Zoom _ key': 0xFB,
'Clear _ key': 0xFE,
'+': 0xBB,
',': 0xBC,
'-': 0xBD,
'.': 0xBE,
'/': 0xBF,
''': 0xC0,
';': 0xBA,
'[': 0xDB,
'\': 0xDC,
']': 0xDD,
"'": 0xDE,
''': 0xC0}
Class POINT (Structure ):
_ Fields _ = [("x", c_ulong), ("y", c_ulong)]
Def get_mouse_point ():
Po = POINT ()
Windll. user32.GetCursorPos (byref (po ))
Return int (po. x), int (po. y)
Def mouse_click (x = None, y = None ):
If not x is None and not y is None:
Mouse_move (x, y)
Time. sleep (0.05)
Win32api. mouse_event (win32con. MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
Time. sleep (0.05)
Win32api. mouse_event (win32con. MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
Def mouse_dclick (x = None, y = None ):
If not x is None and not y is None:
Mouse_move (x, y)
Time. sleep (0.05)
Win32api. mouse_event (win32con. MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
Time. sleep (0.05)
Win32api. mouse_event (win32con. MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
Time. sleep (0.05)
Win32api. mouse_event (win32con. MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
Time. sleep (0.05)
Win32api. mouse_event (win32con. MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
Def mouse_move (x, y ):
Windll. user32.SetCursorPos (x, y)
Def mouse_absolute (x, y, x2, y2 ):
Windll. user32.SetCursorPos (x, y) # Move the cursor
Win32api. mouse_event (win32con. MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) # press the left button
Time. sleep (0.2)
Mw = int (x2 * 65535/SW)
Mh = int (y2 * 65535/SH)
Win32api. mouse_event (win32con. MOUSEEVENTF_ABSOLUTE + win32con. MOUSEEVENTF_MOVE, mw, mh, 0, 0)
Time. sleep (0.2)
Win32api. mouse_event (win32con. MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
Def key_input (str = ''):
For c in str:
Win32api. keybd_event (VK_CODE [c], 0, 0)
Win32api. keybd_event (VK_CODE [c], 0, win32con. KEYEVENTF_KEYUP, 0)
Time. sleep (0.01)
Def t0 ():
Pass # do nothing
Def t2 ():
Mouse_clicking (800,200)
For c in 'hello ':
Win32api. keybd_event (, 0,) # The a key code is 86
Win32api. keybd_event (65,0, win32con. KEYEVENTF_KEYUP, 0)
# Print get_mouse_point ()
Def t1 ():
# Mouse_move (1024,470) aa
# Time. sleep (0.05)
# Mouse_dclick () HELLO
Mouse_dclick (1024,470)
Def t3 ():
Mouse_clicking (1024,470)
Str = 'Hello'
For c in str:
Win32api. keybd_event (VK_CODE [c], 0) # The a key code is 86
Win32api. keybd_event (VK_CODE [c], 0, win32con. KEYEVENTF_KEYUP, 0)
Time. sleep (0.01)
Def t4 ():
Mouse_clicking (900,300)
Str = 'Hello'
Key_input (str)
Def t5 ():
# Drag the bookmarks in a pdf file
Po = POINT ()
Windll. user32.GetCursorPos (byref (po ))
J = 50 # operation count
X = 766 # x
Y= 500 # y
N = 10 # Move up 10
For I in range (0, j ):
Mouse_absolute (x, y, x, y-n)
# Windll. user32.SetCursorPos (po. x, po. y)
Mouse_click (po. x, po. y)
Print (str (j-I ))
If _ name _ = "_ main __":
T5 () # move pdf bookmark
# T4 ()
# T3 ()
# T2 ()
# T1 ()
# T0 ()
2. mouse. py file
The code is as follows:
#
# _ * _ Coding: UTF-8 _*_
_ Author _ = 'WP'
Import win32api
Import win32con
Import win32gui
From ctypes import *
Import time
SW = 1, 1377
SH = 1, 768
VK_CODE = {
'Backspace': 0x08,
'Tab': 0x09,
'Clear': 0x0C,
'Enter': 0x0D,
'Shift ': 0x10,
'Ctrl ': 0x11,
'Alt': 0x12,
'Pause': 0x13,
'Caps _ lock': 0x14,
'Esc ': 0x1B,
'Spacebar ': 0x20,
'Page _ up': 0x21,
'Page _ low': 0x22,
'End': 0x23,
'Home': 0x24,
'Left _ Arrow': 0x25,
'Up _ Arrow': 0x26,
'Right _ Arrow': 0x27,
'Down _ Arrow': 0x28,
'Select': 0x29,
'Print ': 0x2A,
'Execute ': 0x2B,
'Print _ screen': 0x2C,
'Ins': 0x2D,
'Del ': 0x2E,
'Help': 0x2F,
'0': 0x30,
'1': 0x31,
'2': 0x32,
'3': 0x33,
'4': 0x34,
'5': 0x35,
'6': 0x36,
'7': 0x37,
'8': 0x38,
'9': 0x39,
'A': 0x41,
'B': 0x42,
'C': 0x43,
'D': 0x44,
'E': 0x45,
'F': 0x46,
'G': 0x47,
'H': 0x48,
'I': 0x49,
'J': 0x4A,
'K': 0x4B,
'L': 0x4C,
'M': 0x4D,
'N': 0x4E,
'O': 0x4F,
'P': 0x50,
'Q': 0x51,
'R': 0x52,
'S ': 0x53,
'T': 0x54,
'U': 0x55,
'V': 0x56,
'W': 0x57,
'X': 0x58,
'Y': 0x59,
'Z': 0x5A,
'Numpad _ 0': 0x60,
'Numpad _ 1': 0x61,
'Numpad _ 2': 0x62,
'Numpad _ 3': 0x63,
'Numpad _ 4': 0x64,
'Numpad _ 5': 0x65,
'Numpad _ 6': 0x66,
'Numpad _ 7': 0x67,
'Numpad _ 8': 0x68,
'Numpad _ 9': 0x69,
'Multiply _ key': 0x6A,
'Add _ key': 0x6B,
'Separator _ key': 0x6C,
'Subtract _ key': 0x6D,
'Decimal _ key': 0x6E,
'Ide _ key': 0x6F,
'F1': 0x70,
'F2': 0x71,
'F3 ': 0x72,
'F4': 0x73,
'F5': 0x74,
'F6 ': 0x75,
'F7 ': 0x76,
'F8': 0x77,
'F9': 0x78,
'F3': 0x79,
'F11': 0x7A,
'F12': 0x7B,
'F13': 0x7C,
'F14': 0x7D,
'F15': 0x7E,
'F16': 0x7F,
'F17': 0x80,
'F18': 0x81,
'F19': 0x82,
'F20': 0x83,
'F21': 0x84,
'F22': 0x85,
'F23': 0x86,
'F24': 0x87,
'Num _ lock': 0x90,
'Scroll _ lock': 0x91,
'Left _ shift ': 0xA0,
'Right _ shift ': 0xA1,
'Left _ control': 0xA2,
'Right _ control': 0xA3,
'Left _ menu ': 0xA4,
'Right _ menu ': 0xA5,
'Browser _ back': 0xA6,
'Browser _ forward ': 0xA7,
'Browser _ refresh': 0xA8,
'Browser _ stop': 0xA9,
'Browser _ search': 0xAA,
'Browser _ favorites ': 0xAB,
'Browser _ start_and_home ': 0xAC,
'Volume _ mute ': 0xAD,
'Volume _ low': 0xAE,
'Volume _ up': 0xAF,
'Next _ track': 0xB0,
'Previous _ track': 0xB1,
'Stop _ media ': 0xB2,
'Play/pause_media ': 0xB3,
'Start _ Mail': 0xB4,
'Select _ media ': 0xB5,
'Start _ application_1 ': 0xB6,
'Start _ application_2 ': 0xB7,
'Attn _ key': 0xF6,
'Crsel _ key': 0xF7,
'Exsel _ key': 0xF8,
'Play _ key': 0xFA,
'Zoom _ key': 0xFB,
'Clear _ key': 0xFE,
'+': 0xBB,
',': 0xBC,
'-': 0xBD,
'.': 0xBE,
'/': 0xBF,
''': 0xC0,
';': 0xBA,
'[': 0xDB,
'\': 0xDC,
']': 0xDD,
"'": 0xDE,
''': 0xC0}
Class POINT (Structure ):
_ Fields _ = [("x", c_ulong), ("y", c_ulong)]
Def get_mouse_point ():
Po = POINT ()
Windll. user32.GetCursorPos (byref (po ))
Return int (po. x), int (po. y)
Def mouse_click (x = None, y = None ):
If not x is None and not y is None:
Mouse_move (x, y)
Time. sleep (0.05)
Win32api. mouse_event (win32con. MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
Time. sleep (0.05)
Win32api. mouse_event (win32con. MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
Def mouse_dclick (x = None, y = None ):
If not x is None and not y is None:
Mouse_move (x, y)
Time. sleep (0.05)
Win32api. mouse_event (win32con. MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
Time. sleep (0.05)
Win32api. mouse_event (win32con. MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
Time. sleep (0.05)
Win32api. mouse_event (win32con. MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
Time. sleep (0.05)
Win32api. mouse_event (win32con. MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
Def mouse_move (x, y ):
Windll. user32.SetCursorPos (x, y)
Def mouse_absolute (x, y, x2, y2 ):
Windll. user32.SetCursorPos (x, y) # Move the cursor
Win32api. mouse_event (win32con. MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) # press the left button
Time. sleep (0.2)
Mw = int (x2 * 65535/SW)
Mh = int (y2 * 65535/SH)
Win32api. mouse_event (win32con. MOUSEEVENTF_ABSOLUTE + win32con. MOUSEEVENTF_MOVE, mw, mh, 0, 0)
Time. sleep (0.2)
Win32api. mouse_event (win32con. MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
Def key_input (str = ''):
For c in str:
Win32api. keybd_event (VK_CODE [c], 0, 0)
Win32api. keybd_event (VK_CODE [c], 0, win32con. KEYEVENTF_KEYUP, 0)
Time. sleep (0.01)
Def t0 ():
Pass # do nothing
Def t2 ():
Mouse_clicking (800,200)
For c in 'hello ':
Win32api. keybd_event (, 0,) # The a key code is 86
Win32api. keybd_event (65,0, win32con. KEYEVENTF_KEYUP, 0)
# Print get_mouse_point ()
Def t1 ():
# Mouse_move (1024,470) aa
# Time. sleep (0.05)
# Mouse_dclick () HELLO
Mouse_dclick (1024,470)
Def t3 ():
Mouse_clicking (1024,470)
Str = 'Hello'
For c in str:
Win32api. keybd_event (VK_CODE [c], 0) # The a key code is 86
Win32api. keybd_event (VK_CODE [c], 0, win32con. KEYEVENTF_KEYUP, 0)
Time. sleep (0.01)
Def t4 ():
Mouse_clicking (900,300)
Str = 'Hello'
Key_input (str)
Def t5 ():
# Drag the bookmarks in a pdf file
Po = POINT ()
Windll. user32.GetCursorPos (byref (po ))
J = 50 # operation count
X = 766 # x
Y= 500 # y
N = 10 # Move up 10
For I in range (0, j ):
Mouse_absolute (x, y, x, y-n)
# Windll. user32.SetCursorPos (po. x, po. y)
Mouse_click (po. x, po. y)
Print (str (j-I ))
If _ name _ = "_ main __":
T5 () # move pdf bookmark
# T4 ()
# T3 ()
# T2 ()
# T1 ()
# T0 ()
3. the running effect is as follows:
I hope this article will help you with Python programming.