Bootstrap

pyautogui自动化鼠标键盘操作

pyautogui,用来写自动化脚本,比按键精灵更方便。pyautogui.position()可以获取当前鼠标位置。pyautogui不支持中文输入,利用 pyperclip从剪切板粘贴输入。

# -*- coding: utf-8 -*-
import time
import os
import traceback
import logging
import pyautogui
from logging.handlers import RotatingFileHandler
import pyperclip
pos={"H15":(724,507)
,"D01":(362,507)
,"ELY01":(362,507)
,"ELY36":(486,739)
,"C01":(358,350)}
h=(742-507)/9
w=60
def set_input(value):
    with pyautogui.hold('ctrl'):  # Press the Shift key down and hold it.
        pyautogui.press(['a'])  # Press the left arrow key 4 times.
    pyperclip.copy(value)
    time.sleep(0.3)
    pyautogui.hotkey('Ctrl', 'V')
def fg():
    mouse_click(505,217)
    mouse_click(549,426)#open fg
    set_input("#F95E5E")
    mouse_click(457,441)#open fg
def fg_clear():
    mouse_click(505,217)
    mouse_click(549,426)#open fg
    set_input("#000000")
    mouse_click(457,441)#open fg
def mouse_click(x,y):
    pyautogui.moveTo(x,y)#open fg
    pyautogui.click()
    time.sleep(0.3)
def bg_clear():
    mouse_click(557,217)#open fg
    mouse_click(549+52,426)#open fg
    set_input("#FFFFFF")
    mouse_click(457,441)#open fg
def bg():
    mouse_click(557,217)#open fg
    # time.sleep(0.200)
    mouse_click(549+52,426)#open fg
    set_input("#F95E5E")
    mouse_click(457,441)#open fg
def bj():
    mouse_click(1083,224)#open fg
    mouse_click(908,238)#open fg
    set_input("1")
    mouse_click(1012,299)#open fg
    set_input("#32F92C")
    mouse_click(1008,341)#open fg
    set_input("#32F92C")
    mouse_click(1167,432)#open fg
    # mouse_click(457,441)#open fg
def gs(value):
    mouse_click(411,257)#open fg
    set_input(value)
    mouse_click(330,261)#open fg
def main():
    fg()
    bg()
def toggle_one(x,y):
    logging.info(["pos",x,y])
    mouse_click(x,y)
    fg()
    bg()
    bj()
def toggle_c():
    (x0,y0)=pos["C01"]
    x=x0
    y=y0
    i=1
    while True:
        toggle_one(x,y)
        istr="%02d" % i
        v="=[公式].[if] where [设备编号]='C"+istr+"'"
        logging.info(v)
        gs(v)
        x=x+w*2
        i+=1
        if i>5:
            break
def toggle_d():
    (x0,y0)=pos["D01"]
    x=x0
    y=y0
    i=1
    while True:
        toggle_one(x,y)
        istr="%02d" % i
        v="=[公式].[if] where [设备编号]='D"+istr+"'"
        logging.info(v)
        gs(v)
        y=y+h
        i+=2
        if i>13:
            break
    x=x0+w*2
    y=y0
    i=2
    while True:
        toggle_one(x,y)
        istr="%02d" % i
        v="=[公式].[if] where [设备编号]='D"+istr+"'"
        gs(v)
        y=y+h
        i+=2
        if i>14:
            break  
def toggle_ely1():
    (x0,y0)=pos["ELY01"]
    x=x0
    y=y0
    num=1
    for j in range(5):
        y=y0
        for i in range(7):
            toggle_one(x,y)
            istr="%02d" % num
            v="=[公式].[if] where [设备编号]='ELY"+istr+"'"
            logging.info(v)
            gs(v)
            y=y-h
            num+=1
        x=x+w*2
def toggle_ely36():
    (x0,y0)=pos["ELY36"]
    x=x0
    y=y0
    num=36
    for j in range(2):
        y=y0
        for i in range(7):
            toggle_one(x,y)
            istr="%02d" % num
            v="=[公式].[if] where [设备编号]='ELY"+istr+"'"
            logging.info(v)
            gs(v)
            y=y-h
            num+=1
            if num>48:
                return
        x=x-w*2

def toggle_h():
    (x0,y0)=pos["H15"]
    x=x0
    y=y0
    i=15
    while True:
        toggle_one(x,y)
        v="=[公式].[if] where [设备编号]='H"+str(i)+"'"
        logging.info(v)
        gs(v)
        y=y+h
        i+=2
        if i>33:
            break
    x=x0+w*2
    y=y0
    i=16
    while True:
        toggle_one(x,y)
        v="=[公式].[if] where [设备编号]='H"+str(i)+"'"
        gs(v)
        y=y+h
        i+=2
        if i>34:
            break
if __name__=="__main__":
    import logging
    import sys
    handlers=[RotatingFileHandler(filename="report.log", maxBytes=10*1024*1024, encoding='utf-8',backupCount=3),logging.StreamHandler(sys.stdout)]
    logging.basicConfig(level=logging.DEBUG,
                    format = '%(asctime)s - %(name)s - %(levelname)s -%(funcName)s - %(message)s',
                    handlers=handlers)
    logging.info('begin')
    # toggle_h()
    # toggle_d()
    # toggle_one(724,536)
    # enum1()
    # bj()
    # gs("abc")
    # bg_clear()
    # fg_clear()
    # toggle_c()
    toggle_ely36()

;