ããŠã¹ãšããŒããŒããèªç±èªåšã«æããé¢åãªPCäœæ¥ãèªååïŒ
ãPyAutoGUIïŒãã€ãªãŒããžãŒãŠãŒã¢ã€ïŒãã¯ãPythonã䜿ã£ãŠããŠã¹ãããŒããŒãã®æäœãèªååã§ãããéåžžã«åŒ·åã§äººæ°ã®ããã©ã€ãã©ãªã§ãã WindowsãmacOSãLinuxãšãã£ãäž»èŠãªãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã«å¯Ÿå¿ããŠãããPython 2ããã³Python 3ã§åäœããŸãã ãã®ã©ã€ãã©ãªã䜿ãã°ã人éãPCã§è¡ãã»ãšãã©ã®æäœãããã°ã©ã ã§åçŸã§ããŸããããšãã°ãç¹å®ã®å Žæãã¯ãªãã¯ããããã©ãŒã ã«æåãå ¥åãããã¹ã¯ãªãŒã³ã·ã§ãããæ®ããç¹å®ã®ç»åãç»é¢äžã«è¡šç€ºãããã®ãåŸ ã€ããšãã£ãããšãå¯èœã«ãªããŸãã ããããã£ãã¯ã»ããã»ã¹ã»ãªãŒãã¡ãŒã·ã§ã³ïŒRPAïŒããšåŒã°ããåéã§ã掻çšãããŠãããå®åçãªæ¥åãç¹°ãè¿ãäœæ¥ã®èªååã«å€§ããè²¢ç®ããŸãã ãã®èšäºã§ã¯ãPyAutoGUIã®åºæ¬çãªäœ¿ãæ¹ãããå°ãå¿çšçãªãã¯ããã¯ããããŠå©çšããäžã§ã®æ³šæç¹ãŸã§ãå¹ åºã解説ããŠãããŸãã
- ããŠã¹ã«ãŒãœã«ã®ç§»åãã¯ãªãã¯ïŒå·Šã»å³ã»äžå€®ïŒãããã«ã¯ãªãã¯ããã©ãã°ïŒãããã
- ããŒããŒãããã®æåå ¥åãç¹æ®ããŒïŒEnter, Shift, Ctrlãªã©ïŒã®æŒäž
- ãããããŒïŒã·ã§ãŒãã«ããããŒïŒã®å®è¡ (äŸ: Ctrl+C, Alt+F4)
- ã¹ã¯ãªãŒã³ã·ã§ããã®æ®åœ±
- ç»é¢äžã§ã®ç»åèªèïŒæå®ããç»åãç»é¢äžã®ã©ãã«ããããæ¢ãïŒ
- ã¢ã©ãŒãã確èªãå ¥åããã³ãããªã©ã®ã¡ãã»ãŒãžããã¯ã¹è¡šç€º
ð ã€ã³ã¹ããŒã«æ¹æ³
PyAutoGUIã䜿çšããã«ã¯ããŸãã©ã€ãã©ãªãã€ã³ã¹ããŒã«ããå¿ èŠããããŸããPythonã®ããã±ãŒãžç®¡çããŒã«ã§ãã`pip`ã䜿ãã®ãæãç°¡åã§ãã ã¿ãŒããã«ïŒWindowsã®å Žåã¯ã³ãã³ãããã³ããïŒãéãã以äžã®ã³ãã³ããå®è¡ããŠãã ããã
pip install pyautogui
macOSãLinuxç°å¢ã§Python 3ã䜿çšããŠããå Žåã¯ã`pip3`ã䜿ãå¿ èŠããããããããŸããã
pip3 install pyautogui
OSããšã®äŸåé¢ä¿:
- Windows: è¿œå ã®äŸåé¢ä¿ã¯ãããŸããã
- macOS: `pyobjc-core` ãš `pyobjc` ã¢ãžã¥ãŒã«ãå¿ èŠã§ããéåžžã¯PyAutoGUIã®ã€ã³ã¹ããŒã«æã«èªåã§è§£æ±ºãããŸãããããåé¡ãçºçããå Žåã¯ãåå¥ã«ã€ã³ã¹ããŒã«ãè©Šã¿ãŠãã ãã (`pip install pyobjc-core pyobjc`)ããŸããmacOSã§ã¯ã»ãã¥ãªãã£èšå®ã§ãã¹ã¯ãªãããå®è¡ããã¿ãŒããã«ãIDEã«ãã¢ã¯ã»ã·ããªãã£ãããç»é¢åé²ãã®æš©éãäžããå¿ èŠãããå ŽåããããŸãïŒã·ã¹ãã èšå® > ãã©ã€ãã·ãŒãšã»ãã¥ãªãã£ïŒã
- Linux: `python3-xlib` (Python 3ã®å Žå) ãå¿ èŠã§ãããŸããã¹ã¯ãªãŒã³ã·ã§ããæ©èœã®ããã«`scrot`ãTkinter GUIã®ããã«`python3-tk`ãš`python3-dev`ãå¿ èŠã«ãªãããšããããŸãããããã¯éåžžãaptãªã©ã®ããã±ãŒãžãããŒãžã£ã§ã€ã³ã¹ããŒã«ã§ããŸã (`sudo apt-get install python3-xlib scrot python3-tk python3-dev`)ã
ã€ã³ã¹ããŒã«ãå®äºããããPythonã¹ã¯ãªãããã€ã³ã¿ã©ã¯ãã£ãã·ã§ã«ã§`import pyautogui`ãšèšè¿°ããããšã§ãã©ã€ãã©ãªã®æ©èœã䜿ããããã«ãªããŸãã
import pyautogui
ã€ã³ã¹ããŒã«ãæåããã確èªããã«ã¯ã`pip list` ã³ãã³ããå®è¡ããäžèŠ§ã« `pyautogui` ãšãã®ããŒãžã§ã³ã衚瀺ãããŠãããã確èªããŸãã
ð±ïž ããŠã¹æäœã®èªåå
PyAutoGUIã®æãåºæ¬çãªæ©èœã®äžã€ãããŠã¹æäœã®èªååã§ããã«ãŒãœã«ã移åãããããã¯ãªãã¯ãããããã©ãã°ãããããããšãã§ããŸãã
ç»é¢åº§æšã«ã€ããŠ
PyAutoGUIã¯ãç»é¢äžã®äœçœ®ãX座æšãšY座æšã§æå®ããŸãã ç»é¢ã®å·Šäžãåç¹(0, 0)ãšãªããX座æšã¯å³ã«è¡ãã»ã©å¢å ããY座æšã¯äžã«è¡ãã»ã©å¢å ããŸãïŒæ°åŠã®åº§æšç³»ãšã¯Y軞ã®åããéã§ãïŒã äŸãã°ã1920×1080ãã¯ã»ã«ã®ç»é¢ã§ããã°ãå·Šäžã®åº§æšã¯(0, 0)ãå³äžã®åº§æšã¯(1919, 1079)ãšãªããŸãã
çŸåšã®ç»é¢ãµã€ãºïŒè§£å床ïŒãååŸããã«ã¯ã`pyautogui.size()`é¢æ°ã䜿çšããŸãã
import pyautogui
# ç»é¢ãµã€ãºãååŸ (å¹
, é«ãã®ã¿ãã«ãè¿ã)
screenWidth, screenHeight = pyautogui.size()
print(f"ç»é¢ãµã€ãº: å¹
={screenWidth}, é«ã={screenHeight}")
# äŸ: ç»é¢ãµã€ãº: å¹
=1920, é«ã=1080
çŸåšã®ããŠã¹ã«ãŒãœã«ã®äœçœ®ãååŸããã«ã¯ã`pyautogui.position()`é¢æ°ã䜿çšããŸããããã¯ãç¹å®ã®UIèŠçŽ ã®åº§æšã調ã¹ãããšããªã©ã«äŸ¿å©ã§ãã
import pyautogui
import time
print("5ç§åŸã«ããŠã¹ã«ãŒãœã«ã®äœçœ®ãååŸããŸã...")
time.sleep(5) # 5ç§åŸ
æ©ïŒãã®éã«ããŠã¹ãç®çã®äœçœ®ã«åããïŒ
# çŸåšã®ããŠã¹ã«ãŒãœã«äœçœ®ãååŸ
currentMouseX, currentMouseY = pyautogui.position()
print(f"çŸåšã®ããŠã¹äœçœ®: X={currentMouseX}, Y={currentMouseY}")
ããŠã¹ã«ãŒãœã«ã®ç§»å
ããŠã¹ã«ãŒãœã«ãç¹å®ã®äœçœ®ã«ç§»åãããã«ã¯ã`pyautogui.moveTo()`é¢æ°ã䜿çšããŸããX座æšãY座æšããããŠãªãã·ã§ã³ã§ç§»åã«ãããæéïŒç§æ°ïŒãæå®ã§ããŸãã
import pyautogui
import time
# (100, 200)ã®äœçœ®ã«å³åº§ã«ç§»å
pyautogui.moveTo(100, 200)
print("X=100, Y=200 ã«ç§»åããŸããã")
time.sleep(1)
# (500, 500)ã®äœçœ®ã«2ç§ãããŠç§»å
pyautogui.moveTo(500, 500, duration=2)
print("X=500, Y=500 ã«2ç§ãããŠç§»åããŸããã")
çŸåšã®äœçœ®ããçžå¯Ÿçã«ç§»åããããå Žåã¯ã`pyautogui.moveRel()`ãŸãã¯`pyautogui.move()`é¢æ°ã䜿çšããŸããXæ¹åã®ç§»åéãYæ¹åã®ç§»åéããªãã·ã§ã³ã§ç§»åæéãæå®ããŸãã
import pyautogui
import time
# çŸåšäœçœ®ããå³ã«100ãã¯ã»ã«ãäžã«50ãã¯ã»ã«ç§»å (1ç§ããã)
pyautogui.moveRel(100, 50, duration=1)
# pyautogui.move(100, 50, duration=1) ã§ãåã
print("å³ã«100, äžã«50 移åããŸããã")
ããŠã¹ã¯ãªãã¯
ããŠã¹ã®ã¯ãªãã¯ã¯`pyautogui.click()`é¢æ°ã§è¡ããŸããåŒæ°ãªãã§åŒã³åºããšãçŸåšã®ã«ãŒãœã«äœçœ®ã§å·Šã¯ãªãã¯ãå®è¡ããŸãã ç¹å®ã®åº§æšãæå®ããŠã¯ãªãã¯ããããšãå¯èœã§ãããŸããã¯ãªãã¯ãããã¿ã³ïŒ’left’, ‘middle’, ‘right’ïŒãã¯ãªãã¯åæ°ãæå®ã§ããŸãã
import pyautogui
import time
# çŸåšã®äœçœ®ã§å·Šã¯ãªãã¯
pyautogui.click()
print("çŸåšäœçœ®ã§å·Šã¯ãªãã¯ããŸããã")
time.sleep(1)
# (300, 400)ã®äœçœ®ã«ç§»åããŠå·Šã¯ãªãã¯
pyautogui.click(x=300, y=400)
print("X=300, Y=400 ã§å·Šã¯ãªãã¯ããŸããã")
time.sleep(1)
# (500, 600)ã®äœçœ®ã§å³ã¯ãªãã¯
pyautogui.click(x=500, y=600, button='right')
print("X=500, Y=600 ã§å³ã¯ãªãã¯ããŸããã")
time.sleep(1)
# çŸåšã®äœçœ®ã§ããã«ã¯ãªãã¯ïŒå·ŠïŒ
pyautogui.doubleClick()
print("çŸåšäœçœ®ã§ããã«ã¯ãªãã¯ããŸããã")
# ç¹å®ã®ãã¿ã³ã«å¯Ÿå¿ããé¢æ°ãçšæãããŠãã
# pyautogui.rightClick(x=500, y=600)
# pyautogui.middleClick()
ããŠã¹ãã¿ã³ãæŒãããŸãŸã«ãã`mouseDown()`ãšãé¢ã`mouseUp()`ããããŸããããããçµã¿åãããŠã¯ãªãã¯ãè¡šçŸããããšãã§ããŸãã
import pyautogui
import time
# (700, 700)ã§å·Šãã¿ã³ãæŒãã£ã±ãªãã«ãã
pyautogui.moveTo(700, 700)
pyautogui.mouseDown(button='left')
print("X=700, Y=700 ã§å·Šãã¿ã³ãæŒããŸããã")
time.sleep(2)
# å·Šãã¿ã³ãé¢ã
pyautogui.mouseUp(button='left')
print("å·Šãã¿ã³ãé¢ããŸããã")
ããŠã¹ãã©ãã°
ããŠã¹ã®ãã©ãã°ïŒãã¿ã³ãæŒãããŸãŸã«ãŒãœã«ã移åãããæäœïŒã¯ã`pyautogui.dragTo()`ã`pyautogui.dragRel()`ã䜿ããŸãã `moveTo`ã`moveRel`ãšåæ§ã«ã絶察座æšãŸãã¯çžå¯Ÿåº§æšãšç§»åæéãæå®ã§ããŸããã©ã®ãã¿ã³ã§ãã©ãã°ããããæå®å¯èœã§ãïŒããã©ã«ãã¯å·Šãã¿ã³ïŒã
import pyautogui
import time
# çŸåšäœçœ®ãèšé²
startX, startY = pyautogui.position()
# (startX + 200, startY) ã®äœçœ®ãŸã§1ç§ãããŠãã©ãã° (å·Šãã¿ã³)
pyautogui.dragTo(startX + 200, startY, duration=1)
print("å³ã«200ãã¯ã»ã«ãã©ãã°ããŸããã")
time.sleep(1)
# çŸåšäœçœ®ãã(-100, 100)ã®äœçœ®ãŸã§0.5ç§ãããŠãã©ãã° (å·Šãã¿ã³)
pyautogui.dragRel(-100, 100, duration=0.5)
print("å·Šã«100, äžã«100 ãã©ãã°ããŸããã")
ããŠã¹ã¹ã¯ããŒã«
ããŠã¹ãã€ãŒã«ã®ã¹ã¯ããŒã«ã¯`pyautogui.scroll()`é¢æ°ã§è¡ããŸããåŒæ°ã«ã¯ã¹ã¯ããŒã«ããéïŒã¯ãªãã¯æ°ã«çžåœïŒãæå®ããŸããæ£ã®å€ã§äžã«ã¹ã¯ããŒã«ãè² ã®å€ã§äžã«ã¹ã¯ããŒã«ããŸãã ãªãã·ã§ã³ã§ãã¹ã¯ããŒã«ãã座æšãæå®ããããšãã§ããŸãïŒããã©ã«ãã¯çŸåšã®ããŠã¹äœçœ®ïŒã
import pyautogui
import time
# äžã«10ã¯ãªãã¯åã¹ã¯ããŒã«
pyautogui.scroll(10)
print("äžã«10ã¹ã¯ããŒã«ããŸããã")
time.sleep(1)
# äžã«5ã¯ãªãã¯åã¹ã¯ããŒã«
pyautogui.scroll(-5)
print("äžã«5ã¹ã¯ããŒã«ããŸããã")
time.sleep(1)
# ç¹å®ã®åº§æš(x=500, y=500)ã§äžã«20ã¹ã¯ããŒã«
# pyautogui.scroll(20, x=500, y=500)
(å¿çš) Tween / Easing é¢æ°
ããŠã¹ã®ç§»åããã©ãã°ããã人éãããèŠããããã«ãTweenïŒãã¥ã€ãŒã³ïŒãŸãã¯EasingïŒã€ãŒãžã³ã°ïŒé¢æ°ã䜿çšã§ããŸããããã«ããã移åã®éå§æãçµäºæã«é床ãå€åãããããšãã§ããŸãã
import pyautogui
import time
# ç·åœ¢Tween (äžå®é床) - ããã©ã«ã
pyautogui.moveTo(100, 100, duration=1, tween=pyautogui.linear)
print("ç·åœ¢Tweenã§ç§»å")
time.sleep(1)
# Ease-in Quad (éå§ããã£ãã)
pyautogui.moveTo(500, 500, duration=1, tween=pyautogui.easeInQuad)
print("Ease-in Quadã§ç§»å")
time.sleep(1)
# Ease-out Quad (çµäºããã£ãã)
pyautogui.moveTo(100, 100, duration=1, tween=pyautogui.easeOutQuad)
print("Ease-out Quadã§ç§»å")
time.sleep(1)
# Ease-in-out Quad (éå§ãšçµäºããã£ãã)
pyautogui.moveTo(500, 500, duration=1, tween=pyautogui.easeInOutQuad)
print("Ease-in-out Quadã§ç§»å")
ä»ã«ãå€ãã®Tweené¢æ°ãçšæãããŠããŸãã詳ããã¯å ¬åŒããã¥ã¡ã³ããåç §ããŠãã ããã
âšïž ããŒããŒãæäœã®èªåå
PyAutoGUIã䜿ãã°ãããŒããŒãããã®å ¥åãèªååã§ããŸããæåã®å ¥åãç¹å®ã®ããŒã®æŒäžãã·ã§ãŒãã«ããããŒã®å®è¡ãªã©ãå¯èœã§ããããŒããŒãæäœã¯ãçŸåšã¢ã¯ãã£ãïŒãã©ãŒã«ã¹ãããïŒãŠã£ã³ããŠã«å¯ŸããŠéä¿¡ãããŸãã
æååã®å ¥å
ããŒããŒãããæååãå ¥åããã«ã¯ã`pyautogui.write()`é¢æ°ã䜿çšããŸãã
import pyautogui
import time
# ã¡ã¢åž³ãªã©ãäºåã«ã¢ã¯ãã£ãã«ããŠãã
print("5ç§åŸã«æååãå
¥åããŸã...")
time.sleep(5)
# æååãå
¥å
pyautogui.write('Hello, PyAutoGUI world!')
print("'Hello, PyAutoGUI world!' ãå
¥åããŸããã")
# åæåã®å
¥åééã0.1ç§ã«ãã
pyautogui.write('Slow typing test.', interval=0.1)
print("'Slow typing test.' ããã£ããå
¥åããŸããã")
import pyautogui
import pyperclip
import time
# å
¥åãããæ¥æ¬èª
japanese_text = "ããã«ã¡ã¯ãäžçïŒ"
# ã¯ãªããããŒãã«ã³ããŒ
pyperclip.copy(japanese_text)
print(f"'{japanese_text}' ãã¯ãªããããŒãã«ã³ããŒããŸããã")
# ããã¹ããšãã£ã¿ãªã©ãã¢ã¯ãã£ãã«ãã
print("5ç§åŸã«ããŒã¹ãããŸã...")
time.sleep(5)
# ããŒã¹ããå®è¡ (OSã«åãããŠå€æŽ)
# pyautogui.hotkey('ctrl', 'v') # Windows/Linux
pyautogui.hotkey('command', 'v') # macOS
print("ããŒã¹ããå®è¡ããŸããã")
ç¹æ®ããŒã®æŒäž
EnterããŒãShiftããŒãCtrlããŒãAltããŒãç¢å°ããŒãªã©ã®ç¹æ®ããŒãæŒãã«ã¯ã`pyautogui.press()`é¢æ°ã䜿çšããŸããåŒæ°ã«ã¯ããŒåãæååã§æå®ããŸãã å©çšå¯èœãªããŒåã®äžèŠ§ã¯ `pyautogui.KEY_NAMES` ã§ç¢ºèªã§ããŸãã
import pyautogui
import time
# ã¡ã¢åž³ãªã©ãäºåã«ã¢ã¯ãã£ãã«ããŠãã
print("5ç§åŸã«EnterããŒãæŒããŸã...")
time.sleep(5)
pyautogui.press('enter') # EnterããŒãæŒã
print("EnterããŒãæŒããŸããã")
time.sleep(1)
pyautogui.press('a') # 'a'ããŒãæŒã (write('a') ãšã»ãŒåã)
pyautogui.press('b') # 'b'ããŒãæŒã
pyautogui.press('left') # å·Šç¢å°ããŒãæŒã
pyautogui.press('backspace') # BackspaceããŒãæŒã
print("a, b ãå
¥åããå·Šã«ç§»åããŠåé€ããŸããã")
# è€æ°ã®ããŒãé çªã«æŒã
pyautogui.press(['h', 'e', 'l', 'l', 'o', ' ', 'enter'])
print("'hello'ãšå
¥åããŠEnterãæŒããŸããã")
ããŒãæŒãã£ã±ãªãã«ãã `pyautogui.keyDown()` ãšãé¢ã `pyautogui.keyUp()` ããããŸãã
import pyautogui
import time
print("5ç§åŸã«ShiftããŒãæŒããªããå
¥åããŸã...")
time.sleep(5)
pyautogui.keyDown('shift') # ShiftããŒãæŒãã£ã±ãªãã«ãã
pyautogui.press('4') # Shift + 4 ãæŒã ($ãå
¥åãããã¯ã)
pyautogui.press('5') # Shift + 5 ãæŒã (%ãå
¥åãããã¯ã)
pyautogui.keyUp('shift') # ShiftããŒãé¢ã
pyautogui.press('6') # Shiftç¡ãã§6ãæŒã
print("ShiftãæŒããªãã4, 5ãããã®åŸ6ãå
¥åããŸããã")
# pyautogui.hold() ã䜿ããšããç°¡æœã«æžãã (withæ)
with pyautogui.hold('shift'):
pyautogui.press(['left', 'left', 'left']) # ShiftãæŒããªããå·Šç¢å°ã3å
print("ShiftãæŒããªããå·Šç¢å°ã3åæŒããŸããã")
ãããããŒïŒã·ã§ãŒãã«ããããŒïŒ
è€æ°ã®ããŒãåæã«æŒãããããããããããŒïŒã·ã§ãŒãã«ããããŒïŒãå®è¡ããã«ã¯ã`pyautogui.hotkey()`é¢æ°ã䜿çšããŸããåŒæ°ã«åææŒããããããŒåãé çªã«æå®ããŸãã
import pyautogui
import time
# ã¡ã¢åž³ãªã©ã§äœãæåãå
¥åããŠãã
print("5ç§åŸã«å
šéžæ (Ctrl+A) ãå®è¡ããŸã...")
time.sleep(5)
# pyautogui.hotkey('ctrl', 'a') # Windows/Linuxã®å Žå
pyautogui.hotkey('command', 'a') # macOSã®å Žå
print("å
šéžæãå®è¡ããŸããã")
time.sleep(1)
print("3ç§åŸã«ã³ã㌠(Ctrl+C) ãå®è¡ããŸã...")
time.sleep(3)
# pyautogui.hotkey('ctrl', 'c') # Windows/Linuxã®å Žå
pyautogui.hotkey('command', 'c') # macOSã®å Žå
print("ã³ããŒãå®è¡ããŸããã")
time.sleep(1)
print("3ç§åŸã«ããŒã¹ã (Ctrl+V) ãå®è¡ããŸã...")
time.sleep(3)
# pyautogui.hotkey('ctrl', 'v') # Windows/Linuxã®å Žå
pyautogui.hotkey('command', 'v') # macOSã®å Žå
print("ããŒã¹ããå®è¡ããŸããã")
# Alt+F4 (ãŠã£ã³ããŠãéãã - Windows) ã Command+Q (ã¢ããªçµäº - macOS) ãªã©ãå¯èœ
# 泚æ: å®è¡ãããšæ¬åœã«éããŠããŸãã®ã§æ³šæïŒ
# print("5ç§åŸã«ãŠã£ã³ããŠãéããŸã...")
# time.sleep(5)
# pyautogui.hotkey('alt', 'f4') # Windows
# pyautogui.hotkey('command', 'q') # macOS
ðŒïž ã¹ã¯ãªãŒã³é¢é£æ©èœ
PyAutoGUIã¯ãç»é¢ã®ç¶æ ãææ¡ããããã®æ©èœãæäŸããŠããŸããã¹ã¯ãªãŒã³ã·ã§ãããæ®ã£ãããç¹å®ã®ç»åãç»é¢äžã®ã©ãã«ããããæ€åºãããã§ããŸãããããã®æ©èœã¯ãPillowã©ã€ãã©ãªãå¿ èŠã§ãããŸããç»åèªèæ©èœã䜿ãå ŽåãOpenCVã©ã€ãã©ãª (`pip install opencv-python`) ãã€ã³ã¹ããŒã«ããŠãããšãããé«ç²ŸåºŠãªèªèãå¯èœã«ãªãããšããããŸãã
ã¹ã¯ãªãŒã³ã·ã§ãã
ç»é¢å šäœã®ã¹ã¯ãªãŒã³ã·ã§ãããæ®ãã«ã¯ã`pyautogui.screenshot()`é¢æ°ã䜿çšããŸãããã¡ã€ã«åãæå®ããŠä¿åããããPillowã®Imageãªããžã§ã¯ããšããŠçŽæ¥åãåãããšãã§ããŸãã
import pyautogui
# ã¹ã¯ãªãŒã³ã·ã§ããããã¡ã€ã«ã«ä¿å
screenshot_file = 'my_screenshot.png'
pyautogui.screenshot(screenshot_file)
print(f"ã¹ã¯ãªãŒã³ã·ã§ããã {screenshot_file} ãšããŠä¿åããŸããã")
# ã¹ã¯ãªãŒã³ã·ã§ãããImageãªããžã§ã¯ããšããŠååŸ
im = pyautogui.screenshot()
print(f"ã¹ã¯ãªãŒã³ã·ã§ãããImageãªããžã§ã¯ããšããŠååŸããŸãã: {type(im)}")
# ç¹å®ã®é åã ããã¹ã¯ãªãŒã³ã·ã§ãã (å·Šäžã®X, Y, å¹
, é«ã)
im_region = pyautogui.screenshot(region=(100, 200, 300, 400)) # (100, 200)ããå¹
300, é«ã400
im_region.save('region_screenshot.png')
print("æå®é åã®ã¹ã¯ãªãŒã³ã·ã§ãããä¿åããŸããã")
Linuxç°å¢ã§ã¯ãã¹ã¯ãªãŒã³ã·ã§ããæ©èœã®ããã«`scrot`ã³ãã³ããã€ã³ã¹ããŒã«ãããŠããå¿ èŠããããŸã (`sudo apt-get install scrot`)ã
ç»åèªèïŒç»é¢äžã®ç»åãæ¢ãïŒ
äºåã«çšæããç»åãã¡ã€ã«ïŒãã¿ã³ãã¢ã€ã³ã³ãªã©ïŒããçŸåšã®ç»é¢äžã®ã©ãã«ããããæ¢ãããšãã§ããŸããããã¯ã座æšãåºå®ã§ãªãGUIèŠçŽ ãæäœãããå Žåã«éåžžã«äŸ¿å©ã§ãã `pyautogui.locateOnScreen()`é¢æ°ã¯ãç»é¢äžã§æåã«èŠã€ãã£ãç»åã®äœçœ®ãšãµã€ãºïŒå·Šãäžãå¹ ãé«ãïŒãè¿ããŸããèŠã€ãããªãå Žåã¯`None`ãè¿ããŸãã
import pyautogui
import time
# æ¢ãããç»åãã¡ã€ã« (äŸ: 'calc_button_7.png' - é»åã®7ã®ãã¿ã³ç»å)
image_to_find = 'calc_button_7.png'
print(f"ç»é¢äžã§ '{image_to_find}' ãæ¢ããŠããŸã...")
# äºåã«é»åã¢ããªãªã©ã衚瀺ããŠãã
# ç»åãæ¢ã (å°ãæéããããããšããã)
try:
# confidenceåŒæ°ã§äžèŽåºŠã®éŸå€ãæå®ã§ãã (OpenCVãå¿
èŠ)
# grayscale=Trueã§ã°ã¬ãŒã¹ã±ãŒã«ã§æ¢ããšé«éåããå Žåããã
button_location = pyautogui.locateOnScreen(image_to_find, confidence=0.9)
if button_location:
print(f"ç»åãèŠã€ãããŸãã: {button_location}") # äŸ: Box(left=500, top=600, width=50, height=50)
# èŠã€ãã£ãç»åã®äžå¿åº§æšãååŸ
button_center = pyautogui.center(button_location)
print(f"ç»åã®äžå¿åº§æš: {button_center}") # äŸ: Point(x=525, y=625)
# äžå¿ãã¯ãªãã¯
pyautogui.click(button_center)
print("ç»åã®äžå¿ãã¯ãªãã¯ããŸããã")
else:
print("ç»åãèŠã€ãããŸããã§ããã")
except pyautogui.ImageNotFoundException:
print("ç»åãèŠã€ãããŸããã§ãã (äŸå€çºç)ã")
except Exception as e:
# OpenCVãã€ã³ã¹ããŒã«ãããŠããªãå Žåãªã©
print(f"ç»åæ€çŽ¢äžã«ãšã©ãŒãçºçããŸãã: {e}")
print("OpenCVãã€ã³ã¹ããŒã«ãããŠããªãå¯èœæ§ããããŸãã`pip install opencv-python` ãè©ŠããŠãã ããã")
`pyautogui.locateCenterOnScreen()`ã䜿ããšãç»åã®äžå¿åº§æšãçŽæ¥ååŸã§ããŸãã
import pyautogui
image_to_find = 'calc_button_8.png' # äŸ: é»åã®8ã®ãã¿ã³ç»å
try:
button_center = pyautogui.locateCenterOnScreen(image_to_find, confidence=0.9)
if button_center:
print(f"ç»åã®äžå¿ãèŠã€ãããŸãã: {button_center}")
pyautogui.click(button_center)
print("ç»åã®äžå¿ãã¯ãªãã¯ããŸããã")
else:
print("ç»åã®äžå¿ãèŠã€ãããŸããã§ããã")
except Exception as e:
print(f"ç»åæ€çŽ¢äžã«ãšã©ãŒãçºçããŸãã: {e}")
`pyautogui.locateAllOnScreen()`ã䜿ããšãç»é¢äžã«ååšãããã¹ãŠã®è©²åœç®æã®äœçœ®ãæ€åºã§ããŸãã
- ç»åèªèã¯ãã¯ã»ã«åäœã§æ¯èŒãããããç»é¢ã®è§£å床ãããŒããã¢ã€ã³ã³ã®èŠãç®ãå°ãã§ãç°ãªããšèªèã«å€±æããããšããããŸãã
- æ¢ãç»åã¯ãã§ããã ãèæ¯ãå«ãŸãªããç¹åŸŽçãªéšåãåãåããšèªè粟床ãäžãããŸãã
- `confidence`åŒæ°ïŒäžèŽåºŠã®éŸå€ã0ãã1ã®éïŒã調æŽããããšã§ãå€å°èŠãç®ãéã£ãŠãèªèãããããšãã§ããŸããã誀èªèã®ãªã¹ã¯ãé«ãŸããŸã (OpenCVãå¿ èŠã§ã)ã
- èªèåŠçã«ã¯æéããããããšããããŸãã
- ã¹ã¯ãªãŒã³ã·ã§ããæ©èœãšåæ§ãLinuxã§ã¯`scrot`ãå¿ èŠã§ãã
ãã¯ã»ã«æ å ±ã®ååŸ
ç¹å®ã®åº§æšã®ãã¯ã»ã«ã®è²ïŒRGBå€ïŒãååŸãããããã®è²ãç¹å®ã®è²ãšäžèŽãããã©ããã確èªãããã§ããŸãã
import pyautogui
# (100, 200) ã®ãã¯ã»ã«ã®RGBå€ãååŸ
pixel_color = pyautogui.pixel(100, 200)
print(f"座æš(100, 200)ã®ãã¯ã»ã«è²(RGB): {pixel_color}") # äŸ: (255, 136, 0)
# (100, 200) ã®ãã¯ã»ã«ã®è²ã (255, 136, 0) ãšäžèŽããã確èª
matches = pyautogui.pixelMatchesColor(100, 200, (255, 136, 0))
print(f"座æš(100, 200)ã®è²ã¯ (255, 136, 0) ãšäžèŽããŸããïŒ {matches}") # äŸ: True
# 蚱容誀差ãæå®ããããšãå¯èœ (toleranceåŒæ°)
matches_tolerant = pyautogui.pixelMatchesColor(100, 200, (250, 140, 10), tolerance=10)
print(f"蚱容誀差10ã§äžèŽããŸããïŒ {matches_tolerant}")
ð¬ ã¡ãã»ãŒãžããã¯ã¹ã®è¡šç€º
PyAutoGUIã¯ããŠãŒã¶ãŒã«æ å ±ã衚瀺ããããç°¡åãªå ¥åãæ±ãããããããã®ã¡ãã»ãŒãžããã¯ã¹ïŒãã€ã¢ãã°ããã¯ã¹ïŒã衚瀺ããæ©èœãæã£ãŠããŸãã
- ã¢ã©ãŒãããã¯ã¹ (`alert()`): æ
å ±ã衚瀺ãããOKããã¿ã³ã®ã¿ãæã€ããã¯ã¹ã衚瀺ããŸãã
import pyautogui pyautogui.alert(text='åŠçãå®äºããŸããïŒ', title='éç¥', button='äºè§£')
- 確èªããã¯ã¹ (`confirm()`): æ
å ±ã衚瀺ãããOKããšããã£ã³ã»ã«ãã®2ã€ã®ãã¿ã³ãæã€ããã¯ã¹ã衚瀺ããŸãããŠãŒã¶ãŒãã©ã¡ãã®ãã¿ã³ãæŒããããæååã§è¿ããŸãïŒäŸ: ‘OK’, ‘Cancel’ïŒã
import pyautogui response = pyautogui.confirm(text='åŠçãç¶è¡ããŸããïŒ', title='確èª', buttons=['ã¯ã', 'ããã']) if response == 'ã¯ã': print("ãã¯ãããéžæãããŸããã") else: print("ããããããŸãã¯ãã£ã³ã»ã«ãããŸããã")
- ããã³ããããã¯ã¹ (`prompt()`): ããã¹ãå
¥åãæ±ããããã¯ã¹ã衚瀺ããŸãããŠãŒã¶ãŒãå
¥åããæååããŸãã¯ãã£ã³ã»ã«ãããå Žåã¯`None`ãè¿ããŸãã
import pyautogui user_input = pyautogui.prompt(text='ååãå ¥åããŠãã ãã:', title='å ¥åèŠæ±', default='') if user_input is not None: print(f"å ¥åãããåå: {user_input}") else: print("å ¥åããã£ã³ã»ã«ãããŸããã")
- ãã¹ã¯ãŒãããã¯ã¹ (`password()`): ããã³ããããã¯ã¹ãšäŒŒãŠããŸãããå
¥åå
容ãã¢ã¹ã¿ãªã¹ã¯(*)ã§é ãããŸãã
import pyautogui password = pyautogui.password(text='ãã¹ã¯ãŒããå ¥åããŠãã ãã:', title='ãã¹ã¯ãŒãå ¥å', default='', mask='*') if password is not None: print("ãã¹ã¯ãŒããå ¥åãããŸããã") # ã»ãã¥ãªãã£ã®ãã衚瀺ããªã else: print("ãã¹ã¯ãŒãå ¥åããã£ã³ã»ã«ãããŸããã")
ð å®å šã®ããã®æ©èœïŒããŒãºãšãã§ã€ã«ã»ãŒã
GUIãªãŒãã¡ãŒã·ã§ã³ã¯éåžžã«åŒ·åã§ãããæå³ããªãåäœãããå Žåã«PCã®å¶åŸ¡ã倱ããªã¹ã¯ã䌎ããŸããPyAutoGUIã«ã¯ãå®å šã«ã¹ã¯ãªãããå®è¡ããããã®æ©èœãçšæãããŠããŸãã
ããŒãº (Pause)
PyAutoGUIã®åé¢æ°åŒã³åºãã®åŸã«ãèªåçã«äžæåæ¢ïŒããŒãºïŒãå ¥ããããšãã§ããŸããããã¯ãã¹ã¯ãªããã®å®è¡ãéãããŠã¢ããªã±ãŒã·ã§ã³ãè¿œåŸã§ããªãå Žåããåäœã確èªããªããå®è¡ãããå Žåã«åœ¹ç«ã¡ãŸãã `pyautogui.PAUSE` ã«ç§æ°ãèšå®ããŸãã
import pyautogui
# åPyAutoGUIé¢æ°ã®åŸã«1ç§éã®ããŒãºãå
¥ãã
pyautogui.PAUSE = 1.0
pyautogui.moveTo(100, 100) # 1ç§åŸ
æ©
pyautogui.click() # 1ç§åŸ
æ©
pyautogui.write('Paused typing.') # åæåå
¥ååŸã§ã¯ãªããwrite()å
šäœã®åŸã«1ç§åŸ
æ©
# ããŒãºã解é€ããå Žå㯠0.0 ãªã©ã«æ»ã
# pyautogui.PAUSE = 0.0
ç¹å®ã®ç®æã§åŸ æ©ãããå Žåã¯ãPythonæšæºã®`time.sleep()`é¢æ°ã䜿ãããšãæå¹ã§ãã
import pyautogui
import time
pyautogui.click(100, 100)
time.sleep(3) # 3ç§åŸ
æ©
pyautogui.write('Wait for 3 seconds.')
ãã§ã€ã«ã»ãŒã (Fail-Safe)
PyAutoGUIã«ã¯ãã¹ã¯ãªãããæŽèµ°ããå Žåãªã©ã«ç·æ¥åæ¢ãããããã®ããã§ã€ã«ã»ãŒããæ©èœãçµã¿èŸŒãŸããŠããŸãã `pyautogui.FAILSAFE` ã `True` ã«èšå®ãããšãããŠã¹ã«ãŒãœã«ãç»é¢ã®å·Šäžé ïŒåº§æš 0, 0ïŒã«çŽ æ©ã移åãããããšã§ãPyAutoGUIã `pyautogui.FailSafeException` ãšããäŸå€ãçºçãããŠããã°ã©ã ã匷å¶çµäºãããããšãã§ããŸãã
ãã®æ©èœã¯ããã©ã«ãã§æå¹ïŒ`True`ïŒã«ãªã£ãŠããŸãã å®å šã®ãããç¡å¹ã«ããªãããšã匷ãæšå¥šããŸãã
import pyautogui
import time
# ãã§ã€ã«ã»ãŒããæ瀺çã«æå¹ã«ãã (ããã©ã«ãã§Trueãªã®ã§éåžžã¯äžèŠ)
pyautogui.FAILSAFE = True
print("ãã§ã€ã«ã»ãŒããæå¹ã§ãã")
print("5ç§ä»¥å
ã«ããŠã¹ã«ãŒãœã«ãç»é¢å·Šäžã«ç§»åããããšããã°ã©ã ãåæ¢ããŸãã")
try:
for i in range(10):
pyautogui.moveRel(50, 0, duration=0.1) # å°ããã€å³ã«ç§»å
print(".", end="")
# ããã§å·Šäžã«ã«ãŒãœã«ã移åããããšäŸå€ãçºçãã
print("\nã«ãŒããå®äºããŸããã")
except pyautogui.FailSafeException:
print("\nãã§ã€ã«ã»ãŒããäœåããŸããïŒããã°ã©ã ãçµäºããŸãã")
except Exception as e:
print(f"\näºæãã¬ãšã©ãŒãçºçããŸãã: {e}")
ð¡ 掻çšäŸãšæ³šæç¹
掻çšäŸ
PyAutoGUIã®å¿çšç¯å²ã¯åºå€§ã§ããããã€ãäŸãæããŸãã
- å®åçãªããŒã¿å ¥åäœæ¥: ExcelãWebãã©ãŒã ãžã®ããŒã¿å ¥åãèªååããã
- ãœãããŠã§ã¢ã®ãã¹ã: GUIã¢ããªã±ãŒã·ã§ã³ã®ãã¿ã³ã¯ãªãã¯ãå ¥åãã£ãŒã«ãã®ãã¹ããèªååããã
- ç¹°ãè¿ãäœæ¥ã®èªåå: ãã¡ã€ã«ã®ãªããŒã ãç¹å®ã®èšå®å€æŽãªã©ãæ¯æ¥è¡ããããªåçŽäœæ¥ãèªååããã
- ã²ãŒã ã®èªåãã¬ã€ïŒãããïŒ: ç¹å®ã®ç»åãæ¢ããŠã¯ãªãã¯ãããªã©ãç°¡åãªã²ãŒã æäœãèªååããïŒå©çšèŠçŽã«æ³šæïŒã
- Webã¹ã¯ã¬ã€ãã³ã°ã®è£å©: JavaScriptãå€ã䜿ãããŠãããªã©ãéåžžã®ã¹ã¯ã¬ã€ãã³ã°ã©ã€ãã©ãªã§ã¯é£ãããµã€ãã§ããã©ãŠã¶æäœãè£å©ããã
- ãã¬ãŒã³ããŒã·ã§ã³ã®èªåæäœ: ã¹ã©ã€ãã·ã§ãŒã®ããŒãžéããèªååããã
äŸãã°ãç¹å®ã®Webãµã€ããéãããã°ã€ã³ãã©ãŒã ã«æ å ±ãå ¥åããç°¡åãªäŸã§ãã
import pyautogui
import time
import webbrowser
# 察象ã®Webãµã€ããéã
webbrowser.open('https://example.com/login') # å®éã®ãã°ã€ã³ããŒãžã®URLã«çœ®ãæãã
# ããŒãžãèªã¿èŸŒãŸããã®ãåŸ
〠(æéã¯é©å®èª¿æŽ)
time.sleep(5)
# ãŠãŒã¶ãŒåå
¥åãã£ãŒã«ããã¯ãªã㯠(座æšãŸãã¯ç»åèªèã§)
# äŸ: pyautogui.click(x=500, y=300) # äºåã«åº§æšã調ã¹ãŠãã
# ãŸãã¯ããŠãŒã¶ãŒåå
¥åæ¬ã®ç»å 'username_field.png' ãçšæããŠãã
try:
username_field_loc = pyautogui.locateCenterOnScreen('username_field.png', confidence=0.9)
if username_field_loc:
pyautogui.click(username_field_loc)
pyautogui.write('your_username', interval=0.1)
else:
print("ãŠãŒã¶ãŒåãã£ãŒã«ããèŠã€ãããŸããã")
exit() # ãšã©ãŒåŠç
except Exception as e:
print(f"ãŠãŒã¶ãŒåãã£ãŒã«ãæ€çŽ¢äžã«ãšã©ãŒ: {e}")
exit() # ãšã©ãŒåŠç
time.sleep(1)
# ãã¹ã¯ãŒãå
¥åãã£ãŒã«ããã¯ãªã㯠(ã¿ãããŒã§ç§»åãæå¹)
pyautogui.press('tab') # TabããŒã§æ¬¡ã®ãã£ãŒã«ããžç§»å
time.sleep(0.5)
pyautogui.write('your_password', interval=0.1)
time.sleep(1)
# ãã°ã€ã³ãã¿ã³ãã¯ãªã㯠(EnterããŒãŸãã¯åº§æš/ç»åèªèã§)
pyautogui.press('enter')
# ãŸãã¯
# try:
# login_button_loc = pyautogui.locateCenterOnScreen('login_button.png', confidence=0.9)
# if login_button_loc:
# pyautogui.click(login_button_loc)
# else:
# print("ãã°ã€ã³ãã¿ã³ãèŠã€ãããŸããã")
# except Exception as e:
# print(f"ãã°ã€ã³ãã¿ã³æ€çŽ¢äžã«ãšã©ãŒ: {e}")
print("ãã°ã€ã³è©Šè¡å®äºã")
ããã¯ãããŸã§äžäŸã§ãããå®éã®Webãµã€ããã¢ããªã±ãŒã·ã§ã³ã«åãããŠåº§æšãç»åãåŸ æ©æéã調æŽããå¿ èŠããããŸãã
泚æç¹ãšéç
PyAutoGUIã¯äŸ¿å©ã§ãããäžèœã§ã¯ãããŸããã以äžã®ç¹ã«æ³šæããŠå©çšããŸãããã
- ç»é¢è§£å床ãç°å¢ãžã®äŸå: 座æšæå®ãç»åèªèã¯ãå®è¡ããPCã®ç»é¢è§£å床ãOSã®ããŒããã¢ããªã±ãŒã·ã§ã³ã®ããŒãžã§ã³ã衚瀺ç¶æ ã«åŒ·ãäŸåããŸããç°ãªãç°å¢ã§ã¯ã¹ã¯ãªãããæ£ããåäœããªãå¯èœæ§ãé«ãã§ãã
- ã¿ã€ãã³ã°ã®åé¡: ã¢ããªã±ãŒã·ã§ã³ã®å¿çé床ã¯äžå®ã§ã¯ãããŸããã`time.sleep()`ã`pyautogui.PAUSE`ã§é©åãªåŸ æ©æéãèšããå¿ èŠããããŸãããããã§ãã¿ã€ãã³ã°ããããŠå€±æããããšããããŸããç¹å®ã®ç»åã衚瀺ããããŸã§åŸ 〠(`locateOnScreen`ãã«ãŒãããããªã©) ãšãã£ã工倫ãå¿ èŠã§ãã
- ãã©ãŒã«ã¹ã®åé¡: ããŒããŒãæäœã¯ã¢ã¯ãã£ããªãŠã£ã³ããŠã«éä¿¡ãããŸããã¹ã¯ãªããå®è¡äžã«æå³ããªããŠã£ã³ããŠãã¢ã¯ãã£ãã«ãªããšã誀ã£ãæäœãããŠããŸãå¯èœæ§ããããŸããã¹ã¯ãªããã®éå§æã«ç®çã®ãŠã£ã³ããŠã確å®ã«ã¢ã¯ãã£ãã«ããåŠçãå ¥ããããšãæãŸããã§ãã
- ãšã©ãŒãã³ããªã³ã°: ç»åãèŠã€ãããªãã座æšãæ³å®å€ããªã©ã®ãšã©ãŒã¯é »ç¹ã«èµ·ããããŸãã`try…except`ãããã¯ã䜿ã£ãŠããšã©ãŒãçºçããå Žåã®åŠçïŒãªãã©ã€ããã°èšé²ãåŠçã®äžæ¢ãªã©ïŒãé©åã«èšè¿°ããããšãéèŠã§ãã
- ã»ãã¥ãªãã£ãªã¹ã¯: ãã¹ã¯ãŒããªã©ã®æ©å¯æ å ±ã`write()`é¢æ°ã§çŽæ¥å ¥åããã®ã¯ã»ãã¥ãªãã£äžå¥œãŸãããããŸããããŸããæªæã®ããã¹ã¯ãªããã¯PCãä¹ã£åãããšãã§ããŠããŸããããä¿¡é Œã§ããªããœãŒã¹ããã®PyAutoGUIã¹ã¯ãªããã®å®è¡ã¯é¿ããã¹ãã§ãã
- ããã¯ã°ã©ãŠã³ãå®è¡ã®é£ãã: PyAutoGUIã¯åºæ¬çã«ç»é¢äžã§å®éã«ããŠã¹ãããŒããŒããæäœãããããã¹ã¯ãªããå®è¡äžã¯ä»ã®PCäœæ¥ãã§ããŸãããããã¯ã°ã©ãŠã³ãã§å®å šã«èªååãããå Žåã¯ãAPIãä»ã®èªååææ³ïŒSeleniumãªã©ïŒãæ€èšããæ¹ãè¯ãå ŽåããããŸãã
- ã¡ã³ããã³ã¹ã³ã¹ã: GUIã®èŠãç®ãå°ãå€ããã ãã§ã¹ã¯ãªãããåããªããªãããšããããããã¢ããªã±ãŒã·ã§ã³ã®ã¢ããããŒããªã©ã«äŒŽãã¡ã³ããã³ã¹ãå¿ èŠã«ãªãå ŽåããããŸãã
ð ãŸãšã
PyAutoGUIã¯ãPythonã䜿ã£ãŠGUIæäœãèªååããããã®åŒ·åã§ã¢ã¯ã»ã¹ããããã©ã€ãã©ãªã§ããããŠã¹ãããŒããŒãã®åºæ¬çãªæäœãããã¹ã¯ãªãŒã³ã·ã§ãããç»åèªèãã¡ãã»ãŒãžããã¯ã¹è¡šç€ºãŸã§ãå¹ åºãæ©èœãæäŸããŸãã å®åçãªç¹°ãè¿ãäœæ¥ããAPIãæäŸãããŠããªãã¢ããªã±ãŒã·ã§ã³ã®æäœãªã©ãèªååããããšã§ãäœæ¥å¹çã®å€§å¹ ãªåäžãæåŸ ã§ããŸããð
ãã ããç°å¢äŸåæ§ãé«ããã¿ã€ãã³ã°ã®åé¡ããšã©ãŒãã³ããªã³ã°ã«æ³šæãå¿ èŠãªåŽé¢ããããŸããç¹ã«ããã§ã€ã«ã»ãŒãæ©èœãåžžã«æå¹ã«ããŠãããªã©ãå®å šã«é æ ®ãã䜿ãæ¹ãå¿ãããããšãéèŠã§ãã ãã®èšäºããPyAutoGUIã䜿ã£ãèªååã®äžçãžã®ç¬¬äžæ©ãšãªãã°å¹žãã§ãããã²ã身ã®åãã®é¢åãªäœæ¥ãèªååããŠã¿ãŠãã ããïŒð
ãã詳ããæ å ±ããããã§çŽ¹ä»ããããªãã£ãé¢æ°ã«ã€ããŠã¯ãPyAutoGUIã®å ¬åŒããã¥ã¡ã³ããåç §ããŠãã ããã
ã³ã¡ã³ã