pip install pyautogui
document
import pyautogui
pyautogui.size()
pyautogui.position()
pyautogui.moveTo(x=None, y=None, duration=0.0)
pyautogui.dragTo(x=None, y=None, duration=0.0, button=PRIMARY)
click(x=None, y=None, clicks=1, interval=0.0, button=PRIMARY, duration=0.0)
mouseDown
mouseUp
scroll
typewrite
press(keystr, presses=1, interval=0.0)
hotkey
KEYBOARD_KEYS
keyDown
keyUp
pyautogui.screenshot(r’C:\Users\pc\Desktop\test.png’, region=(x, y, width, height))
通过图片匹配获取点击位置
pyautogui.center(pyautogui.locateOnScreen(‘图片路径’, grayscale=True))
pyautogui.locateCenterOnScreen()
解决图片匹配不到的问题:
pip install opencv-python
confidence = 0.9|0.8
等,confidence 越小,定位的精度会下降,但更容易匹配到图片。grayscale=True
会使图像和屏幕截图中的颜色去饱和,可以避免由显示器饱和度不同导致的图像定位失败。(x,y,width,height)
,找图会更快,也避免其他区域图像的干扰import pyautogui
#循环找图,找到就返回图像中心点,没找到就打印'没找到'
def imgs_locate(args):
for i in range(len(args)):
r = pyautogui.locateCenterOnScreen(arg[i])
if r !== None:
return r
else:
pass
# print('没找到')
#测试
print(imgs_locate(['images/test.png','images/test1.png'])
中文输入 使用赋值粘贴
pip install pyperclip
copy(“str”) 修改剪切板内容
pyautogui.hotkey(“Ctrl”, “v”)
获取日期字符串
datetime.datetime.now().strftime(‘%Y-%m-%d’)