pyautoguiでGUIソフトの自動化

cygwinlinuxの場合

pip3 install pyscreeze
pip3 install opencv_python
pip3 install pyautogui

anaconda でエラーした場合

python -m pip pyscreeze
python -m pip opencv_python
python -m pip pyautogui

参考サイトのコードそのまま。 もぐらたたきゲーム https://game.nifty.com/free/50 こんなのが4行でかける衝撃。。

import pyautogui
 
#ずっと繰り返す
while True:
     #特定の範囲から画像と一致する座標(左上の座標と画像の高さ)を全て返す
    for pos in pyautogui.locateAllOnScreen('head2.png', region=(100, 300, 1500, 800), grayscale=True, confidence=0.950):
     
        #座標の1点を取り出して画像の中心を取得する
        cpos = pyautogui.center(pos)
        #画像の中心をクリックする
        pyautogui.click(cpos)
    #マウスを移動させる(ハンマーでモグラが隠れるのを防ぐため)
    pyautogui.moveTo(100, 250)

コロンの入力

コロンが日本語キーボード対応のバグで入力ができないため以下の修正が必要.

C:\ProgramData\Anaconda3\Lib\site-packages\pyautogui_pyautogui_win.py

def _keyDown(key):
    # 略
    needsShift = pyautogui.isShiftCharacter(key)

    # 以下を追加
    if key == '@': needsShift = False
    if key == '^': needsShift = False
    if key == ':': needsShift = False

    """
    # OLD CODE: The new code relies on having all keys be loaded in keyboardMapping from the start.
    if key in keyboardMapping.keys():

例1: 入力待ち

import os
import pyautogui
import time
import pyscreeze
from pyscreeze import ImageNotFoundException

for tryCount in range(100):
        try: 
            if pyautogui.locateOnScreen("syturyoku.png"):
                    break
        except ImageNotFoundException: 
                time.sleep(0.1)
                    
x,y=pyautogui.locateCenterOnScreen("syturyoku.png")   
pyautogui.moveTo(x+90,y)
pyautogui.click(x+90,y)
pyautogui.hotkey('ctrl','a')
#pyautogui.press('del',presses=10000)
pyautogui.press('del')
#time.sleep(0.3)
output=r'C:\\Users\user1\Documents\python_test\outfile\test.pdf'
print(output)
pyautogui.write(output)
time.sleep(0.1)
pyautogui.press('enter')     

例2:

ImageNotFoundExceptionは画面がでてくるまでの待ち等に使う。

import os
import pyautogui
import time
import pyscreeze
from pyscreeze import ImageNotFoundException

def news_article_search(date1,type1,page1):   
    x,y = pyautogui.locateCenterOnScreen("keisai.png")
    pyautogui.click(x,y)
    #From
    pyautogui.moveTo(x+80,y+40)
    pyautogui.click(x+80,y+40)
    pyautogui.press('del',presses=20)
    pyautogui.write(date1)
    
    #To
    pyautogui.moveTo(x+358,y+36)
    pyautogui.click(x+358,y+36)
    pyautogui.press('del',presses=20)
    pyautogui.write(date1)
    #発効形態
    #朝刊
    if type1 == 1:
        pyautogui.moveTo(x+88,y+91)
        pyautogui.click(x+88,y+91)
    #夕刊
    if type1 == 2:
        pyautogui.moveTo(x+220,y+91)
        pyautogui.click(x+220,y+91)

    #付録
    if type1 == 3:
        pyautogui.moveTo(x+300,y+91)
        pyautogui.click(x+300,y+91)

    #号外
    if type1 == 4:
        pyautogui.moveTo(x+400,y+91)
        pyautogui.click(x+400,y+91)

    #ページ数
    pyautogui.moveTo(x+20,y+160)
    pyautogui.click(x+12,y+160)
    pyautogui.press('del',presses=20)
    pyautogui.write(page1)
    
    #検索
    pyautogui.moveTo(x+408,y+160)
    pyautogui.click(x+408,y+160)
    #検索結果の表示
    time.sleep(0.4)
    for tryCount in range(100):
        try: 
            if pyautogui.locateOnScreen("gamenmodoru.png"):
                break
        except ImageNotFoundException: 
            time.sleep(0.4)
            
    try: 
        x,y = pyautogui.locateCenterOnScreen("title.png")
        pyautogui.moveTo(x,y)
        pyautogui.click(x,y)
    except:
        print("no") 
        x,y = pyautogui.locateCenterOnScreen("gamenmodoru.png")
        pyautogui.moveTo(x,y)
        pyautogui.click(x,y)


def print_list(output_file_name="test.pdf"):
    for tryCount in range(100):
        try: 
            if pyautogui.locateOnScreen("insatsu_list_small.png"):
            #if pyautogui.locateOnScreen("printamei2.png"):
                break
        except ImageNotFoundException: 
            time.sleep(0.3)
            
        #x,y = pyautogui.locateCenterOnScreen("printamei2.png")
    x,y = pyautogui.locateCenterOnScreen("insatsu_list_small.png")
    pyautogui.moveTo(x+100,y)
    pyautogui.click(x+100,y)
    
    for tryCount in range(100):
        try: 
                #if pyautogui.locateOnScreen("insatsu_cube.png"):
            if pyautogui.locateOnScreen("insatu_cubu_list.png"):
                break
        except ImageNotFoundException: 
            time.sleep(0.1)
        #x,y=pyautogui.locateCenterOnScreen("insatsu_cube.png")
    x,y=pyautogui.locateCenterOnScreen("insatu_cubu_list.png")
    pyautogui.moveTo(x,y)
    pyautogui.click(x,y)
    pyautogui.hotkey('enter')
   
    for tryCount in range(100):
        try: 
                #if pyautogui.locateOnScreen("syturyoku.png"):
            if pyautogui.locateOnScreen("cubepdf_logo.png"):    
                break
        except ImageNotFoundException: 
            time.sleep(0.1)
                    
    x,y=pyautogui.locateCenterOnScreen("cubepdf_logo.png")        
        #x,y=pyautogui.locateCenterOnScreen("syturyoku.png") 
    pyautogui.moveTo(x+60,y+180)
        #pyautogui.click(x+90,y)
    pyautogui.click(x+60,y+180)
    pyautogui.hotkey('ctrl','a')
        #pyautogui.press('del',presses=10000)
    pyautogui.press('del')
        #time.sleep(0.3)
    output=r'C:\\Users\user1\Documents\python_test\outfile\test.pdf'
    output='C:\\\\Users\\user1\\Documents\\python_test\\outfile\\'+output_file_name
    print(output)
    pyautogui.write(output)
    time.sleep(0.1)
    pyautogui.press('enter')    
        
      
    try: 
        for tryCount in range(100):
                #if pyautogui.locateOnScreen("syturyoku.png"):
            if pyautogui.locateOnScreen("cubepdf_logo.png"):  
                if pyautogui.locateOnScreen("uwagaki_small.png"): 
                   print("uwagaki") 
                   pyautogui.press('enter')          
            
                print("hello")
                time.sleep(0.5)
            else:
                print("no")
                break
    except ImageNotFoundException:    
        print("end")
                    #break
 


def print_article(output_file_name="test.pdf"):
    for tryCount in range(100):
        try: 
            #if pyautogui.locateOnScreen("insatsu_list_small.png"):
            if pyautogui.locateOnScreen("printamei.png"):
                break
        except ImageNotFoundException: 
            time.sleep(0.3)
    x,y = pyautogui.locateCenterOnScreen("printamei.png")
        #x,y = pyautogui.locateCenterOnScreen("insatsu_list_small.png")
    pyautogui.moveTo(x+100,y)
    pyautogui.click(x+100,y)
    for tryCount in range(100):
        try: 
            if pyautogui.locateOnScreen("insatsu_cube.png"):
                #if pyautogui.locateOnScreen("insatu_cubu_list.png"):
                break
        except ImageNotFoundException: 
                time.sleep(0.1)
    x,y=pyautogui.locateCenterOnScreen("insatsu_cube.png")
    #x,y=pyautogui.locateCenterOnScreen("insatu_cubu_list.png")
    pyautogui.moveTo(x,y)
    pyautogui.click(x,y)
    pyautogui.hotkey('enter')
   
    for tryCount in range(100):
        try: 
            #if pyautogui.locateOnScreen("syturyoku.png"):
            if pyautogui.locateOnScreen("cubepdf_logo.png"):    
                break
        except ImageNotFoundException: 
                time.sleep(0.1)
                    
    x,y=pyautogui.locateCenterOnScreen("cubepdf_logo.png")        
    #x,y=pyautogui.locateCenterOnScreen("syturyoku.png") 
    pyautogui.moveTo(x+60,y+180)
    #pyautogui.click(x+90,y)
    pyautogui.click(x+60,y+180)
    pyautogui.hotkey('ctrl','a')
    #pyautogui.press('del',presses=10000)
    pyautogui.press('del')
    #time.sleep(0.3)
    output=r'C:\\Users\user1\Documents\python_test\outfile\test.pdf'
    output='C:\\\\Users\\user1\\Documents\\python_test\\outfile\\'+output_file_name
    print(output)
    pyautogui.write(output)
    time.sleep(0.1)
    pyautogui.press('enter')          
    time.sleep(0.1)
    #print("uwagaki0")
    #if pyautogui.locateOnScreen("uwagaki_small.png"): 
    #  print("uwagaki") 
    #   pyautogui.press('enter')          
       

   
        
    try: 
        for tryCount in range(100):
                #if pyautogui.locateOnScreen("syturyoku.png"):
            if pyautogui.locateOnScreen("cubepdf_logo.png"):  
                 if pyautogui.locateOnScreen("uwagaki_small.png"): 
                   print("uwagaki") 
                   pyautogui.press('enter')          
       
    
                 print("hello")
                 time.sleep(0.5)
            else:
                print("no")
                break
    except ImageNotFoundException:    
        print("end")
                    #break
 
        
#紙面表示        
def print_paper(output_file_name="test.pdf"):
    #一覧が表示されるまで待つ
    for tryCount in range(100):
        try: 
            if pyautogui.locateOnScreen("kensaku_keka_ha.png"):
                break
        except ImageNotFoundException: 
            time.sleep(0.3)
    
    #List のprint
    pyautogui.hotkey('alt','f')
    pyautogui.hotkey('enter')
    
    print_list(output_file_name="test2.pdf");
    #rewrewrew
    #紙面の表示
    ##リストのactive 化
    print("test2a")
    x,y=pyautogui.locateCenterOnScreen("kensaku_keka_ha.png")
    pyautogui.click(x,y) 
    print("test2")
    ##紙面表示
    pyautogui.hotkey('alt', 'V')
    pyautogui.hotkey('enter')
    #time.sleep(0.4)
    #紙面の表示まで待ち
    for tryCount in range(100):
        try: 
            if pyautogui.locateOnScreen("title_simen.png"):
                break
        except ImageNotFoundException: 
            time.sleep(0.3)
    
    #ピンの削除
    pyautogui.hotkey('alt', 'D')
    pyautogui.hotkey('P')

    
    #印刷
    pyautogui.hotkey('ctrl', 'p')
    print_article(output_file_name=output_file_name)
    
    #記事窓を閉じる
    pyautogui.hotkey('alt','f4')
    time.sleep(0.5)
    #リスト窓を閉じる
    x,y=pyautogui.locateCenterOnScreen("kensaku_keka_ha.png")
    pyautogui.click(x,y) 
    pyautogui.hotkey('alt','f')
    #time.sleep(0.5)
    pyautogui.hotkey('c')
    
news_article_search(date1="46/06/09",type1=2,page1="1")
print_paper(output_file_name="out_460609"+"_"+"2"+"_"+"1"+".pdf")