当前位置: 首页 > news >正文

pyautogui的一些自动化示例,附代码

以下为您提供一些 pyautogui 的自动化示例及代码:

  1. 模拟鼠标点击和移动:
    import pyautogui
    # 获取屏幕的宽度和高度
    screen_width, screen_height = pyautogui.size()
    # 将鼠标移动到屏幕中心
    pyautogui.moveTo(screen_width / 2, screen_height / 2)
    # 在屏幕中心点击鼠标左键
    pyautogui.click(screen_width / 2, screen_height / 2)
    
  2. 模拟键盘输入:
    import pyautogui
    # 模拟键盘输入字符串
    pyautogui.typewrite("Hello, World!", interval=0.25)
    
  3. 屏幕截图:
    import pyautogui
    # 截取整个屏幕
    screenshot = pyautogui.screenshot()
    # 保存截图
    screenshot.save('screenshot.png')
    
  4. 制作垃圾邮件自动化:
    import pyautogui as pag
    import time
    from tkinter import Tk
    from tkinter.filedialog import askopenfilename# 提供输入方法
    Tk().withdraw()
    filename = askopenfilename()# 创建延迟调整垃圾邮件的速度
    timeDelay = int(input("If you want a delay, enter the number of seconds for the delay : ").split()(0))
    if timeDelay < 1:timeDelay = 1
    time.sleep(5)# 使用 PyAutoGUI 发送垃圾邮件
    f = open(filename, "r")
    for word in f:time.sleep(timeDelay)pag.typewrite(word)pag.press("enter")
    
  5. 向下滚动条到底:
    import pyautogui
    import time# 等待 1 秒,确保所有窗口都已加载完成
    time.sleep(1)
    # 模拟按下键盘的"Page Down"键,向下滚动屏幕内容
    pyautogui.scrollDown()
    
  6. 识别图像并点击:
    import pyautogui
    # 查找图像的位置
    image_location = pyautogui.locateOnScreen('image.png')
    # 如果图像存在,则点击该位置
    if image_location:x, y = pyautogui.center(image_location)pyautogui.click(x, y)
    

以上示例展示了 pyautogui 在不同场景下的自动化操作及相应的代码实现。

pyautogui模拟鼠标操作的代码示例

在 Python 中,使用 pyautogui 库进行模拟鼠标操作非常方便。以下是一些常见的模拟鼠标操作的代码示例:

import pyautogui
import time# 移动鼠标到指定位置
pyautogui.moveTo(100, 100, duration=1)  # 用 1 秒时间移动到坐标 (100, 100)# 单击鼠标左键
pyautogui.click(200, 200)  # 在坐标 (200, 200) 处单击左键# 双击鼠标左键
pyautogui.doubleClick(300, 300)  # 在坐标 (300, 300) 处双击左键# 拖动鼠标
pyautogui.dragTo(400, 400, duration=2)  # 用 2 秒时间将鼠标从当前位置拖动到 (400, 400)

pyautogui模拟键盘输入的代码示例

import pyautogui# 模拟输入单个字符
pyautogui.press('a')# 模拟输入字符串
pyautogui.typewrite('Hello, World!')# 模拟输入组合键
pyautogui.hotkey('ctrl', 'c')

pyautogui屏幕截图的代码示例

import pyautogui# 截取整个屏幕
screenshot = pyautogui.screenshot()
screenshot.save('screenshot.png')# 截取指定区域
x = 100
y = 100
width = 500
height = 500
screenshot = pyautogui.screenshot(region=(x, y, width, height))
screenshot.save('region_screenshot.png')

pyautogui制作垃圾邮件自动化的代码示例

import pyautogui as pag
import time
from tkinter import Tk
from tkinter.filedialog import askopenfilename# 提供输入方法
Tk().withdraw()
filename = askopenfilename()# 创建延迟调整垃圾邮件的速度
timeDelay = int(input("If you want a delay, enter the number of seconds for the delay : ").split()(0))
if timeDelay < 1:timeDelay = 1
time.sleep(5)# 使用 PyAutoGUI 发送垃圾邮件
msg = input("Enter the message: ")
n = input("How many times?: ")
for i in range(0, int(n)):pyautogui.typewrite(msg)

pyautogui滚动条操作的代码示例

import pyautogui# 向上滚动一次
pyautogui.scroll(1)# 向下滚动一次
pyautogui.scroll(-1)

pyautogui识别图像并点击的代码示例

import pyautogui
import cv2# 加载待识别图像
button_img = cv2.imread('button.png')# 在屏幕上查找图像
button_location = pyautogui.locateOnScreen(button_img, confidence=0.8)# 如果找到图像,点击其中心
if button_location is not None:button_x, button_y = pyautogui.center(button_location)pyautogui.click(button_x, button_y)
else:print('未找到按钮图像')

以上就是关于 pyautogui 的一些自动化示例代码,通过这些代码可以实现各种自动化操作,为工作和生活带来便利。但在实际应用中,请确保您的操作符合法律法规和道德规范,并注意不要对他人造成不良影响。


http://www.mrgr.cn/news/12098.html

相关文章:

  • 计算机毕业设计选题推荐-OA办公管理系统-Java/Python项目实战
  • 金融涉案账户压降行动的实施成效与挑战
  • jenkins发送html邮件配置步骤与注意事项?
  • 认知杂谈25
  • SpringBoot -在Axis2中,RPCServiceClient调用WebService
  • Android PopupWindow弹窗动态显示在View的上下方,
  • 8月27日cs61c
  • Redis6.0新特性
  • 在我的博士科研生活中,SCI的英语写作一直是我的挑战。
  • Vue3+Ts封装input组件时遇到的问题
  • 【iOS端】基于Uniapp跨平台接入即构RTC+相芯美颜
  • APP封装安装配置参考说明
  • swift自定义数据集微调Qwen-7B大模型,转换模型后使用ollama跑起来
  • AutoGPT开源项目解读
  • Halcon20.11深度学习语义分割模型
  • 数据结构概念
  • 使用hutool实现http的调用
  • Java中IO基础文本数据处理:BufferedReader 和 BufferedWriter
  • leetcode 76.最小覆盖子串
  • Python习题 154:用装饰器实现开始执行和结束执行时间