PyAutoGUI क्रॉस-प्लेटफॉर्म GUI ऑटोमेशन लाइब्रेरी है जो माउस और कीबोर्ड को प्रोग्रामेटिकली कंट्रोल करती है।
इंस्टॉलेशन
pip install pyautogui
Linux: अतिरिक्त निर्भरताएं: sudo apt-get install python3-xlib scrot
माउस कंट्रोल
स्क्रीन आकार और पोजीशन
import pyautogui
# स्क्रीन रिज़ॉल्यूशन
width, height = pyautogui.size()
print(f"स्क्रीन: {width}x{height}") # 1920x1080 आदि
# वर्तमान माउस पोजीशन
x, y = pyautogui.position()
print(f"माउस: ({x}, {y})")
माउस मूवमेंट
# निरपेक्ष पोजीशन पर मूव pyautogui.moveTo(100, 100, duration=1.0) # 1 सेकंड में मूव # सापेक्ष मूव pyautogui.moveRel(50, 0, duration=0.5) # दाएं 50 पिक्सेल pyautogui.moveRel(0, -30, duration=0.5) # ऊपर 30 पिक्सेल # इंस्टेंट मूव (कोई ड्यूरेशन नहीं) pyautogui.moveTo(500, 500)
क्लिकिंग
# सिंगल क्लिक pyautogui.click(100, 100) # निर्देशांक पर pyautogui.click() # वर्तमान पोजीशन पर pyautogui.click(button='right') # राइट क्लिक pyautogui.click(clicks=2) # डबल क्लिक pyautogui.click(interval=0.5) # क्लिक्स के बीच अंतराल # माउस डाउन/अप (ड्रैग के लिए) pyautogui.mouseDown() pyautogui.moveTo(200, 200) pyautogui.mouseUp()
स्क्रॉलिंग
pyautogui.scroll(10) # ऊपर स्क्रॉल pyautogui.scroll(-10) # नीचे स्क्रॉल pyautogui.hscroll(10) # हॉरिजॉन्टल स्क्रॉल
कीबोर्ड कंट्रोल
टाइपिंग
# सिंपल टाइपिंग
pyautogui.typewrite('Hello, World!')
# अंतराल के साथ
pyautogui.typewrite('Hello!', interval=0.1)
# की लिस्ट (स्पेशल कीज़ के लिए)
pyautogui.typewrite(['a', 'b', 'left', 'left', 'X', 'Y']) # 'XYab'
की प्रेस/रिलीज
# सिंगल की प्रेस
pyautogui.press('enter')
pyautogui.press('f1')
pyautogui.press('left')
# मल्टीपल कीज़
pyautogui.press(['left', 'left', 'enter'])
# की डाउन/अप (होल्ड के लिए)
pyautogui.keyDown('ctrl')
pyautogui.keyDown('c')
pyautogui.keyUp('c')
pyautogui.keyUp('ctrl')
हॉटकी (शॉर्टकट्स)
# सुविधाजनक हॉटकी फंक्शन
pyautogui.hotkey('ctrl', 'c') # कॉपी
pyautogui.hotkey('ctrl', 'v') # पेस्ट
pyautogui.hotkey('ctrl', 'shift', 'esc') # टास्क मैनेजर (Windows)
pyautogui.hotkey('alt', 'tab') # ऐप स्विच
# समतुल्य मैनुअल:
pyautogui.keyDown('ctrl')
pyautogui.keyDown('shift')
pyautogui.keyDown('esc')
pyautogui.keyUp('esc')
pyautogui.keyUp('shift')
pyautogui.keyUp('ctrl')
उपलब्ध कीज़ सूची
print(pyautogui.KEYBOARD_KEYS) # ['\t', '\n', '\r', ' ', '!', '"', '#', ..., 'a', 'b', ..., # 'enter', 'esc', 'f1'-'f24', 'left', 'right', 'up', 'down', # 'ctrl', 'alt', 'shift', 'tab', 'space', 'win', 'command', ...]
व्यावहारिक उदाहरण: ऑटोमेटेड फॉर्म भरना
import pyautogui
import time
# उपयोगकर्ता को तैयार होने का समय दें
print("5 सेकंड में शुरू होगा... फॉर्म विंडो पर क्लिक करें")
time.sleep(5)
form_data = [
("राहुल", "rahul@example.com", "दिल्ली"),
("प्रिया", "priya@example.com", "मुंबई"),
]
for name, email, city in form_data:
# नाम फील्ड (मान लें पहले फील्ड पर फोकस है)
pyautogui.typewrite(name, interval=0.05)
pyautogui.press('tab')
# ईमेल
pyautogui.typewrite(email, interval=0.05)
pyautogui.press('tab')
# शहर
pyautogui.typewrite(city, interval=0.05)
pyautogui.press('tab')
# सबमिट
pyautogui.press('enter')
time.sleep(1) # अगले फॉर्म के लिए प्रतीक्षा
print("सभी फॉर्म भरे गए!")
स्क्रीनशॉट और इमेज रिकग्निशन
# स्क्रीनशॉट लें
screenshot = pyautogui.screenshot()
screenshot.save('screen.png')
# क्षेत्र स्क्रीनशॉट
region_shot = pyautogui.screenshot(region=(0, 0, 300, 400))
# इमेज ढूंढें स्क्रीन पर (बटन, आइकन, आदि)
button_location = pyautogui.locateOnScreen('submit_button.png')
if button_location:
center = pyautogui.center(button_location)
pyautogui.click(center)
print("बटन मिला और क्लिक किया!")
else:
print("बटन नहीं मिला")
सुरक्षा फीचर्स
- फेल-सेफ: माउस को कोने (0,0) पर तेजी से ले जाएं →
pyautogui.FailSafeExceptionउठता है - पॉज़:
pyautogui.PAUSE = 0.1प्रत्येक कॉल के बाद डिफ़ॉल्ट पॉज़
pyautogui.FAILSAFE = True # डिफ़ॉल्ट True pyautogui.PAUSE = 0.5 # प्रत्येक एक्शन के बाद 0.5 सेकंड रुकें
महत्वपूर्ण नोट्स
- कुछ सिस्टम्स पर एडमिन/एक्सेसिबिलिटी परमिशन आवश्यक
- मैकओएस पर: System Preferences → Security → Accessibility में टर्मिनल/IDE जोड़ें
- गेम्स/एंटी-चीट वाले ऐप्स में काम नहीं कर सकता
- रिलेटिव कोऑर्डिनेट्स (moveRel) अधिक पोर्टेबल हैं