Python 自动化采集数据 - 使用 PyAutoGUI 和 Openpyxl 操作 Excel 和图像

import pyautogui
import time
import openpyxl

# 打开 Excel 文件
workbook = openpyxl.load_workbook(r'C:\Users\Administrator\Desktop\照片查询.xlsx')

# 选择 Sheet1 工作表
sheet = workbook['Sheet1']

# 获取 H 列的所有单元格内容
column_h = sheet['H']

# 遍历每个单元格并输出内容
for cell in column_h:
    print(cell.value)

# 点击图片
def click_image(image_path):
    location = pyautogui.locateOnScreen(image=image_path)
    pyautogui.click(location)
    time.sleep(1)

# 点击图片 1
click_image(r"C:\Users\Administrator\Desktop\1.png")

# 点击图片 2
click_image(r"C:\Users\Administrator\Desktop\2.png")

# 点击图片 3
click_image(r"C:\Users\Administrator\Desktop\3.png")

# 获取当前鼠标位置
current_position = pyautogui.position()

# 循环操作
for cell in column_h:
    # 获取当前单元格内容
    student_id = cell.value

    # 将学号粘贴到指定位置
    pyautogui.typewrite(student_id)

    # 点击图片 4
    click_image(r"C:\Users\Administrator\Desktop\4.png")

    # 等待图片 6 出现
    time.sleep(2)

    # 判断是否采集成功
    if pyautogui.locateOnScreen(r"C:\Users\Administrator\Desktop\6.png"):
        # 采集成功,点击图片 5
        click_image(r"C:\Users\Administrator\Desktop\5.png")

        # 获取当前鼠标位置
        current_position = pyautogui.position()

        # 粘贴学号到当前位置
        pyautogui.hotkey('ctrl', 'v')

        # 点击图片 4
        click_image(r"C:\Users\Administrator\Desktop\4.png")

        # 等待图片 6 出现
        time.sleep(2)

        # 判断是否采集成功
        if pyautogui.locateOnScreen(r"C:\Users\Administrator\Desktop\6.png"):
            # 采集成功,继续下一个学号
            pass
        else:
            # 采集失败,写入“未采集”
            sheet['R' + str(current_position.y)].value = "未采集"
    else:
        # 采集失败,写入“未采集”
        sheet['R' + str(current_position.y)].value = "未采集"

# 保存 Excel 文件
workbook.save(r'C:\Users\Administrator\Desktop\照片查询.xlsx')

代码功能:

  1. 读取 Excel 文件中的学号数据(位于 H 列)。
  2. 使用 PyAutoGUI 自动点击指定的图片。
  3. 通过识别图片判断采集是否成功。
  4. 如果采集成功,则获取当前鼠标位置并粘贴学号。
  5. 如果采集失败,则在 Excel 文件的 R 列对应单元格中写入“未采集”。

使用说明:

  1. 将代码中的图片路径替换为实际路径。
  2. 确保已安装 PyAutoGUI 和 Openpyxl 库。
  3. 运行代码,开始自动采集数据。

注意:

  • 运行代码前,请确保目标窗口已打开,并且鼠标位于正确位置。
  • 代码仅供参考,实际应用中可能需要根据具体情况进行调整。

代码示例:

# 打开 Excel 文件
workbook = openpyxl.load_workbook(r'C:\Users\Administrator\Desktop\照片查询.xlsx')

# 选择 Sheet1 工作表
sheet = workbook['Sheet1']

# 获取 H 列的所有单元格内容
column_h = sheet['H']

# 循环操作
for cell in column_h:
    # 获取当前单元格内容
    student_id = cell.value

    # 将学号粘贴到指定位置
    pyautogui.typewrite(student_id)

    # 点击图片 4
    click_image(r"C:\Users\Administrator\Desktop\4.png")

    # 等待图片 6 出现
    time.sleep(2)

    # 判断是否采集成功
    if pyautogui.locateOnScreen(r"C:\Users\Administrator\Desktop\6.png"):
        # 采集成功,点击图片 5
        click_image(r"C:\Users\Administrator\Desktop\5.png")

        # 获取当前鼠标位置
        current_position = pyautogui.position()

        # 粘贴学号到当前位置
        pyautogui.hotkey('ctrl', 'v')

        # 点击图片 4
        click_image(r"C:\Users\Administrator\Desktop\4.png")

        # 等待图片 6 出现
        time.sleep(2)

        # 判断是否采集成功
        if pyautogui.locateOnScreen(r"C:\Users\Administrator\Desktop\6.png"):
            # 采集成功,继续下一个学号
            pass
        else:
            # 采集失败,写入“未采集”
            sheet['R' + str(current_position.y)].value = "未采集"
    else:
        # 采集失败,写入“未采集”
        sheet['R' + str(current_position.y)].value = "未采集"

# 保存 Excel 文件
workbook.save(r'C:\Users\Administrator\Desktop\照片查询.xlsx')

代码解释:

  • click_image(image_path) 函数:使用 PyAutoGUI 查找并点击指定图片。
  • pyautogui.typewrite(student_id):在当前窗口中输入学号。
  • pyautogui.locateOnScreen(r"C:\Users\Administrator\Desktop\6.png"):查找指定图片,判断采集是否成功。
  • pyautogui.hotkey('ctrl', 'v'):使用快捷键 Ctrl+V 粘贴学号。
  • sheet['R' + str(current_position.y)].value = "未采集":在 Excel 文件的 R 列对应单元格中写入“未采集”。

总结:

本代码使用 Python 的 PyAutoGUI 和 Openpyxl 库实现自动化采集数据功能,通过识别图像位置,点击操作以及获取鼠标位置进行数据采集,并写入 Excel 文件。代码可以帮助用户简化重复的采集任务,提高工作效率。

Python 自动化采集数据:使用 PyAutoGUI 和 Openpyxl 操作 Excel 和图像

原文地址: https://www.cveoy.top/t/topic/ftTS 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录