首页 > 编程语言 >Python - 桌面自动化(PyAutoGUI)

Python - 桌面自动化(PyAutoGUI)

时间:2023-09-11 18:00:47浏览次数:35  
标签:PyAutoGUI pyautogui 桌面 鼠标 Python time print import 回收站

一、安装

windows:
pip install pyautogui -i https://pypi.tuna.tsinghua.edu.cn/simple


mac:
pip install pyobjc-core
pip install pyobjc
pip install pyautogui


linux:
sudo apt-get install scrot python3-tk python3-dev
pip install python3-xlib
pip install pyautogui

 

二、鼠标操作:

1:屏幕和鼠标的位置(行X竖Y):

  • 左上角的像素位于坐标0、0。如果屏幕的分辨率为1920 x 1080,则右下角的像素将为1919、1079(因为坐标始于0,而不是1)。
  • 屏幕分辨率大小由该size()函数作为两个整数的元组返回。该position()函数返回鼠标光标的当前X和Y坐标。

 

2:获取屏幕和鼠标的位置:

import pyautogui
# 获取屏幕分辨率
print(pyautogui.size())
# 获取鼠标的坐标位置
x, y = pyautogui.position()
print("X: %s Y: %s" % (x, y))

注意: 获取到的是电脑设置中的分辨率。如果是虚拟机也是按照虚拟机的设置,不能根据电脑中的分辨率。

 

3:鼠标移动和点击:

  • 相对移动:pyautogui.move(65, 168)—相对的是当前鼠标的位置。
  • 绝对移动: pyautogui.moveTo(65, 168)
  • 每个动作后停留时间: pyautogui.PAUSE = 2
  • 判断像素点是不是在屏幕中:pyautogui.onScreen(65, 168)
  • 当前位置点击: pyautogui.click()
  • 先移动到位置再点击: pyautogui.click(x=100, y=200)
  • 移动到某个位置左击,右击,中间点击:pyaotogui.click(x=100, y=200, button=‘right’)
  • 移动到某个位置左击,右击,中间多次点击: pyaotogui.click(x=100, y=200, button=‘right’, clicks=3)
  • 移动到某个位置左击,右击,中间多次点击,中间间隔0.25秒点击:pyaotogui.click(x=100, y=200, button=‘right’, clicks=3, interval=0.25)
  • 左击: pyautogui.leftClick(x, y, 过程中秒数)
  • 右击:pyautogui.rightClick(x, y, 过程中秒数)
  • 双击:pyautogui.doubleClick(x, y, 过程中秒数)

 

4: 鼠标移动案例:

描述:定位桌面的回收站,然后右击,再用相对路径,左击属性。

# 定位桌面的回收站,然后右击,再用相对路径,左击属性:(57, 285)
import pyautogui
# # 每个动作后,停留两秒
# pyautogui.PAUSE = 2
# 1: 移动到回收站的位置,2表示移动过程两秒
pyautogui.moveTo(57, 285, 2)
# 2: 右击
pyautogui.rightClick()
# 3:
pyautogui.move(87, 200, 2)
pyautogui.leftClick()

 

5:鼠标拖拽

案例:将回收站向右拖拽500像素。

import  pyautogui
# 1: 鼠标移动到指定位置---绝对(left表示鼠标左键,middle表示中间键, right表示右键)
pyautogui.dragTo(57, 285, button='left')
# 2: 进行拖拽,3表示执行时间---相对
pyautogui.drag(500, 0, 3)

 

6: 鼠标滚动

案例:定位到代码框,然后滚动

# 案例:定位到代码框,然后滚动
import pyautogui
import time
# 1:定位到代码框:
pyautogui.click(1100, 460,)
# 2: 向下滚动:
pyautogui.scroll(-1000)
time.sleep(1)
# 3: 向上滚动
pyautogui.scroll(1000)

 

7:保护措施:

为了能够及时中断,PyAutoGUI提供了一个保护措施。当pyautogui.FAILSAFE = True时,如果把鼠标光标在屏幕左上角,PyAutoGUI函数就会产生pyautogui.FailSafeException异常。如果失控了,需要中断PyAutoGUI函数,就把鼠标光标在屏幕左上角。要禁用这个特性,就把FAILSAFE设置成False。

案例:禁用保护

import pyautogui
# 运行过程中,用户动鼠标: 与用户不冲突
pyautogui.FAILSAFE = False
while True:
    pyautogui.moveTo(400, 400)
    pyautogui.moveTo(1000, 400)

 

8: mouseDown()和mouseUp()函数

import pyautogui
import time
# 鼠标定位到我的电脑,进行右击
pyautogui.mouseDown(button="right", x=50, y=54)
pyautogui.mouseUp()
time.sleep(2)
# 鼠标定位到属性,进行左击
pyautogui.mouseDown(button="left", x=86, y=356)
pyautogui.mouseUp()

 

 

三、键盘操作:

1:write函数的使用

  • 中文不支持,需要配合pyperclip一起使用。
  • interval 关键字进行延时操作。
    案例:手动打开记事本,定位到记事本,分别写入“hahaha”和“哈哈哈”。

    import pyautogui
    import pyperclip

 

移动到位置进行点击

pyautogui.moveTo(520, 520, 1)
pyautogui.click()

书写中文

pyautogui.write(“hahaha\n\n”, interval=1)

复制中文,然后粘贴

pyperclip.copy(‘哈哈哈’)
pyautogui.hotkey(‘ctrl’, ‘v’)

 

2:press函数,keyDown和keyUp函数的使用:

案例: 使用pass函数,和keydown,keyup,write函数分别完成一次,敲击回车

import pyautogui
import time
pyautogui.moveTo(520, 520, 1)
pyautogui.click()
# 1: 使用press敲击3次回车
pyautogui.press('enter', presses=3)
pyautogui.write("hahaha\n")
time.sleep(1)
# 2:使用keydown 和keyup敲击三次回车
pyautogui.keyDown("enter")
pyautogui.keyUp('enter')
pyautogui.keyDown("enter")
pyautogui.keyUp('enter')
pyautogui.keyDown("enter")
pyautogui.keyUp('enter')
pyautogui.write("hahaha\n")
time.sleep(1)
# 3: 使用write敲击三次回车
pyautogui.write(['enter', 'enter', 'enter'])
pyautogui.write("hahaha\n")

 

3:hotkey函数的使用:

案例使用热键:打开任务管理器:

import pyautogui
pyautogui.hotkey('ctrl', 'shift', 'esc')

 

四、信息框操作:

方法 作用
pyautogui.alert() 简单提示消息框
pyautogui.confirm() 多按钮消息框
pyautogui.prompt() 明文输入消息框
pyautogui.password() 密文输入消息框

案例一:
显示带有文本和单个“确定”按钮的简单消息框。返回单击的按钮的文本。

import pyautogui
el = pyautogui.alert(text='哈喽,我是任善文', title='打招呼', button='OK')
print(el)

 

案例二:显示带有“确定”和“取消”按钮的消息框。可以自定义按钮的数量和文本。返回单击的按钮的文本。(点击取消)

import pyautogui
el = pyautogui.confirm(text='哈喽,我是任善文', title='打招呼', buttons=['OK', 'Cancel'])
print(el)

 

案例三:显示带有文本输入以及“确定”和“取消”按钮的消息框。返回输入的文本,如果单击“取消”,则返回“无”。

import pyautogui
el = pyautogui.prompt(text='哈喽,我是任善文', title='打招呼', default='哈喽,我是XXX')
print(el)

 

案例四:显示带有文本输入以及“确定”和“取消”按钮的消息框。输入的字符显示为*。返回输入的文本,如果单击“取消”,则返回“无”。

import pyautogui
el = pyautogui.password(text='请输入密码', title='密码账号登录', default='12345678', mask='*')
print(el)

 

 

五、截图和定位操作:

1:screenshot函数的使用:

案例: 截取回收站图标,保存到文件中。

import pyautogui
img = pyautogui.screenshot('./images/screenshot.png', region=(1, 250, 90, 100))
print(img)

截图如图所示:

返回的是Image对象

2:locateOnScreen与locateCenterOnScreen函数的使用:

案例一:通过刚才截取的图片,找到回收站的位置,然后双击回收站打开。(全屏搜索)

import pyautogui
import time
# 1:截取回收站的图片
img = pyautogui.screenshot('./images/screenshot.png', region=(1, 250, 90, 100))
print(img) # 图片类型
# 2:全屏搜索图片(回收站)---获取的会是一个区域
rect = pyautogui.locateOnScreen('./images/screenshot.png')
print(type(rect)) # 盒子类型
print(rect[0], rect[1], rect[2], rect[3])
# 3:在区域中获取中心位置
point = pyautogui.center(rect)
print(type(point)) # 点类型
x, y = point
print(x, y)
time.sleep(1)
# 4: 双击中心位置
pyautogui.doubleClick(x, y

 

案例二:使用locateCenterOnScreen,在区域内查询。

import pyautogui
import time
# 1:截取回收站的图片
img = pyautogui.screenshot('./images/screenshot.png', region=(1, 250, 90, 100))
print(img)
# 2:全屏搜索图片(回收站)---获取的会是一个区域
point = pyautogui.locateCenterOnScreen('./images/screenshot.png')
print(type(point))
x, y = point
time.sleep(1)
# 4: 双击中心位置
pyautogui.doubleClick(x, y)

 

3:提高定位精度

1: 安装OpenCv,由于文件太大需要指定默认等待下载时间(如果网速好可以不用指定)

pip —default-timeout=20000 install -U opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple
表示下载成功。

 

2:可选的confidence关键字参数指定函数在屏幕上定位图像的精度。

import pyautogui
# 可以通过图片,在屏幕上定位图像所在的位置
# 找到返回的是一个4边距元组 (top, left, width, height),没有找到返回None
# 全屏幕搜素
rect = pyautogui.locateOnScreen('./images/x3.png', confidence=0.6)
print(rect)

 

4:加速定位

1: 指定搜索区域:

pyautogui.locateOnScreen('./images/computer.png', region=(0, 0, 400, 400))

2: 降低图片颜色的饱和度:

pyautogui.locateOnScreen('./images/computer.png', grayscale=True)

 

====================================================

原文转自:https://www.dandelioncloud.cn/article/details/1604476127198527489

标签:PyAutoGUI,pyautogui,桌面,鼠标,Python,time,print,import,回收站
From: https://www.cnblogs.com/zhuchunlin/p/17694136.html

相关文章

  • 亚马逊美国站桌面暖风机UL1278测试报告
    亚马逊美国站暖风机UL1278测试报告随之天气越来越冷,尤其是亚马逊那边更是大雪纷飞,早上冒着寒风回到办公室,双手都冻得通红,拿出来仿佛还冒着寒气,打字都打得不利索。冬天这么冷真的很影响工作效率,事情做不完又只能加班解决,为了能按时完成工作准时下班,还是给自己一个温暖的办公环境吧。......
  • Python的requests.post函数上传文件和其他数据
    当使用Python的requests.post函数时,可以在其中添加异常处理来捕获可能的网络错误或HTTP错误。以下是一个示例代码,演示如何使用try-except语句来处理requests.post可能抛出的异常:importrequestsurl='http://cbim.com/upload'files={'file1':('file1.txt',open('file1.t......
  • python一键过杀软
    python过杀软新利用python加载shellcode过360、火绒等杀软先上代码将以下代码保存到mt.pyimportbase64importosimportshutilbuf=b"这里替换shellcode"b64buf=base64.b64encode(buf)lzsds="""importctypesimportbase64shellcode=base64.b64decode(\"......
  • 用python爬取天气
    之前做过这么个小网站,能够爬取天气,然后感觉没什么用,有上网站的时间用手机都看完了,然后就寻思能不能发到自己微信或者qq或者邮箱里先写下怎么把数据提出来 importrequestsimportjsonurl=你自己的网址result=requests.get(url)#print(result.text)data=json.loads......
  • python读写xlsx文件
    importosimporttracebackfromopenpyxlimport*fromlogs.loginimportlogginfromopenpyxl.reader.excelimportload_workbookclassread_data_calss:file_name=r'../../Data/data.xlsx'#在当前路劲执行video_list=[]try:'&......
  • python读取yml文件
    classRead_data_class:defread_yml_def(self,dir_path):withopen(dir_path,'r',encoding='utf-8')asf:yaml_list=yaml.safe_load(f)returnyaml_listifname=='main':passclassmain():path=os.pa......
  • Python——15days
    双层语法糖三层语法糖(多层)装饰器的修复技术(了解)有参装饰器*装饰器的写法:自上而下           执行:自下而上双层:@login_auth@outer执行顺先先执行outer——被装饰名字作为参数传入outer里,get_time=outer(index)。通过调用返回值(前提是如果有双层及以上装饰器......
  • Python工具箱系列(四十二)
    RAR文件操作​RAR是广受好评,使用广泛的压缩格式,开发者为尤金·罗谢尔(俄语:ЕвгенийЛазаревичРошал,拉丁转写:YevgenyLazarevichRoshal),RAR的全名是“RoshalARchive”,即“罗谢尔的归档”之意。尤其是winrar一度成为windows上的必备软件。 它的特点如下:​......
  • # yyds干货盘点 # Python判断多个文件夹的文件夹名是否包含“分公司”或“营销中心”
    大家好,我是皮皮。一、前言前几天在Python最强王者群【哎呦喂 是豆子~】问了一个Python自动化办公的问题,一起来看看吧。大佬们请问下 判断多个文件夹的文件夹名是否包含“分公司”或“营销中心” 有没有什么简便的办法可以实现呀?二、实现过程这里【东哥】给了两个示例代码,实现......
  • Python工具箱系列(四十二)
    RAR文件操作RAR是广受好评,使用广泛的压缩格式,开发者为尤金·罗谢尔(俄语:ЕвгенийЛазаревичРошал,拉丁转写:YevgenyLazarevichRoshal),RAR的全名是“RoshalARchive”,即“罗谢尔的归档”之意。尤其是winrar一度成为windows上的必备软件。 它的特点如下:◆RAR通常......