pythonで簡単な自動化ツールを作成

書けない悩みがあるので、自分はよくプリンセスコネクト、アークナイツなど操作簡単なモバイルゲームでよく使っている自動化ツールの書く方法を話しする

はじめに

まずは依存パッケージをインストール、自分のパソコンでpythonは3.7を使っている。3.7以前はできますでしょうかわからない。

pip install pyperclip 
pip install xlrd 
pip install pyautogui==0.9.50 
pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple 
pip install pillow 

pyautoguiは画像に対応する領域を画面の中の座標に戻します,座標ができたらこの位置をpyautoguiが提供するマウスやキーボードを操作する方法で操作する。

画像1

画像2

xlrdはExcelファイルを読み込み、xlrd.open_workbook()にExcelファイルのパスを指定してBookオブジェクトを取得、sheet_by_index()にシート番号を取得する

画像3

画像4

Excelで内容を入力、画像はpngファイル、成立する条件は唯一だけ。ローラは正の数は上へローラ、負の数は下へローラ、データ量は-200と200を試しをオススメ。


画像5


Excelの入力内容をチェックする

画像6

チェック終わりたら、Excelから操作コードや画像名など情報を読む

画像7

以上で終わり

まとめ

最後にソースコードを貼り

import pyautogui
import time
import xlrd
import pyperclip


def mouseClick(clickTimes,lOrR,img,reTry):
   if reTry == 1:
       while True:
           location=pyautogui.locateCenterOnScreen(img,confidence=0.9)
           if location is not None:
               pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)
               break
           print("一致する画像は見つかりませんでした,0.1秒後に再試行します")
           time.sleep(0.1)
   elif reTry == -1:
       while True:
           location=pyautogui.locateCenterOnScreen(img,confidence=0.9)
           if location is not None:
               pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)
           time.sleep(0.1)
   elif reTry > 1:
       i = 1
       while i < reTry + 1:
           location=pyautogui.locateCenterOnScreen(img,confidence=0.9)
           if location is not None:
               pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)
               print("重複")
               i += 1
           time.sleep(0.1)


# データチェック
# cmdType.value  1.0 左クリック    2.0 左ダブルクリック  3.0 右クリック  4.0 入力  5.0 待機  6.0 ローラ
# ctype     空:0
#           文字列タイプ:1
#           数字:2
#           時間:3
#           Boolean:4
#           error:5
def dataCheck(sheet1):
   checkCmd = True
   #行列数チェック
   if sheet1.nrows<2:
       print("データなし")
       checkCmd = False
   #データチェック
   i = 1
   while i < sheet1.nrows:
       # 第1列 命令コードチェック
       cmdType = sheet1.row(i)[0]
       if cmdType.ctype != 2 or (cmdType.value != 1.0 and cmdType.value != 2.0 and cmdType.value != 3.0 
       and cmdType.value != 4.0 and cmdType.value != 5.0 and cmdType.value != 6.0):
           print('第',i+1,"行,第1列データに不具合がある")
           checkCmd = False
       # 第2列 内容チェック
       cmdValue = sheet1.row(i)[1]
       # 画像クリックコード,内容は文字列タイプ
       if cmdType.value ==1.0 or cmdType.value == 2.0 or cmdType.value == 3.0:
           if cmdValue.ctype != 1:
               print('第',i+1,"行,第2列データに不具合がある")
               checkCmd = False
       # 入力コード,内容が必要
       if cmdType.value == 4.0:
           if cmdValue.ctype == 0:
               print('第',i+1,"行,第2列データに不具合がある")
               checkCmd = False
       # 待機コード,内容は数字
       if cmdType.value == 5.0:
           if cmdValue.ctype != 2:
               print('第',i+1,"行,第2列データに不具合がある")
               checkCmd = False
       # ローラコード,内容は数字
       if cmdType.value == 6.0:
           if cmdValue.ctype != 2:
               print('第',i+1,"行,第2列データに不具合がある")
               checkCmd = False
       i += 1
   return checkCmd

#
def mainWork(img):
   i = 1
   while i < sheet1.nrows:
       #操作コードを読む
       cmdType = sheet1.row(i)[0]
       if cmdType.value == 1.0:
           #画像名を読む
           img = sheet1.row(i)[1].value
           reTry = 1
           if sheet1.row(i)[2].ctype == 2 and sheet1.row(i)[2].value != 0:
               reTry = sheet1.row(i)[2].value
           mouseClick(1,"left",img,reTry)
           print("左クリック",img)
       #2左ダブルクリック
       elif cmdType.value == 2.0:
           #画像名を読む
           img = sheet1.row(i)[1].value
           #繰り返す数
           reTry = 1
           if sheet1.row(i)[2].ctype == 2 and sheet1.row(i)[2].value != 0:
               reTry = sheet1.row(i)[2].value
           mouseClick(2,"left",img,reTry)
           print("左ダブルクリック",img)
       #3右クリック
       elif cmdType.value == 3.0:
           #画像名を読む
           img = sheet1.row(i)[1].value
           #繰り返す数
           reTry = 1
           if sheet1.row(i)[2].ctype == 2 and sheet1.row(i)[2].value != 0:
               reTry = sheet1.row(i)[2].value
           mouseClick(1,"right",img,reTry)
           print("右クリック",img) 
       #4入力
       elif cmdType.value == 4.0:
           inputValue = sheet1.row(i)[1].value
           pyperclip.copy(inputValue)
           pyautogui.hotkey('ctrl','v')
           time.sleep(0.5)
           print("入力:",inputValue)                                        
       #5待機
       elif cmdType.value == 5.0:
           #画像名を読む
           waitTime = sheet1.row(i)[1].value
           time.sleep(waitTime)
           print("待機",waitTime,"s")
       #6ローラ
       elif cmdType.value == 6.0:
           #画像名を読む
           scroll = sheet1.row(i)[1].value
           pyautogui.scroll(int(scroll))
           print("ローラ",int(scroll),"距離")                      
       i += 1

if __name__ == '__main__':
   file = 'cmd.xls'
   xlsFile = xlrd.open_workbook(filename=file)
   #インデックスからsheetを取得
   sheet1 = xlsFile.sheet_by_index(0)
   #データチェック
   checkCmd = dataCheck(sheet1)
   if checkCmd:
       key=input('機能選択:: 1.1回 2.死ぬまで繰り返す \n')
       if key=='1':
           mainWork(sheet1)
       elif key=='2':
           while True:
               mainWork(sheet1)
               time.sleep(5)
               print("5秒待ち")    
   else:
       print('入力間違いまだ終了済み')

この記事が気に入ったらサポートをしてみませんか?