import pyautogui import time import openpyxl import os from tkinter import Tk, Label, Button, filedialog, messagebox

创建GUI窗口

window = Tk() window.title('照片采集查询') window.geometry('400x400')

定义全局变量

excel_file_path = '' xuehao_image_path = '' chaxun_image_path = '' hege_image_path = '' guanbi_image_path = ''

选择Excel文件按钮点击事件

def select_excel_file(): global excel_file_path excel_file_path = filedialog.askopenfilename(filetypes=(('Excel files', '.xlsx'), ('All files', '.*'))) excel_file_label.config(text='Excel文件路径:' + excel_file_path) check_selection()

# 检查是否选择了必要项
check_selection()

选择图片按钮点击事件

def select_image_file(image_label): global xuehao_image_path, chaxun_image_path, hege_image_path, guanbi_image_path image_path = filedialog.askopenfilename(filetypes=(('Image files', '.png'), ('All files', '.*'))) if image_label == xuehao_path_label: xuehao_image_path = image_path elif image_label == chaxun_path_label: chaxun_image_path = image_path elif image_label == hege_path_label: hege_image_path = image_path elif image_label == guanbi_path_label: guanbi_image_path = image_path image_label.config(text='图片路径:' + image_path) check_selection()

# 检查是否选择了必要项
check_selection()

检查是否选择了必要项

def check_selection(): if excel_file_path != '' and xuehao_image_path != '' and chaxun_image_path != '' and hege_image_path != '' and guanbi_image_path != '': selection_label.config(text='已选择必要项') start_collection_button.config(state='normal') else: selection_label.config(text='未选择必要项') start_collection_button.config(state='disabled')

开始采集按钮点击事件

def start_collection(): # 倒计时3秒 for i in range(3, 0, -1): query_progress_label.config(text='开始采集,倒计时:' + str(i)) window.update() time.sleep(1)

# 打开Excel文件
workbook = openpyxl.load_workbook(excel_file_path)

# 选择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)

# 点击图片xuehao
click_image(xuehao_image_path)

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

for i in range(2, len(column_h)):
    # 将学号粘贴到当前位置
    pyautogui.moveTo(current_position)
    pyautogui.click()
    pyautogui.typewrite(str(sheet['H' + str(i)].value))
    pyautogui.press('enter')

    # 点击图片chaxun
    click_image(chaxun_image_path)
    time.sleep(0.5)
    # 判断是否已经采集
    if pyautogui.locateOnScreen(hege_image_path):
        # 如果已采集,则在表格中R列对应的单元格中写入'已采集'
        sheet['R' + str(i)].value = '已采集'
    else:
        # 如果未采集,则在表格中R列对应的单元格中写入'未采集'
        sheet['R' + str(i)].value = '未采集'

    # 点击图片guanbi
    click_image(guanbi_image_path)

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

    # 显示查询进度
    query_progress_label.config(text='正在查询:' + str(i - 1) + '/' + str(len(column_h) - 1))
    window.update()
    time.sleep(1)

# 保存Excel文件
result_file_path = os.path.join(os.path.expanduser('~'), 'Desktop', '考试照片查询结果.xlsx')
workbook.save(result_file_path)

# 查询结束提示
query_progress_label.config(text='查询结束,结果已保存到桌面')
window.update()
time.sleep(3)

选择Excel文件按钮

select_excel_button = Button(window, text='选择Excel文件', command=select_excel_file) select_excel_button.pack()

Excel文件路径

excel_file_label = Label(window, text='Excel文件路径:') excel_file_label.pack()

选择图片按钮

xuehao_button = Button(window, text='选择学号图片', command=lambda: select_image_file(xuehao_path_label)) xuehao_button.pack()

chaxun_button = Button(window, text='选择查询图片', command=lambda: select_image_file(chaxun_path_label)) chaxun_button.pack()

hege_button = Button(window, text='选择合格图片', command=lambda: select_image_file(hege_path_label)) hege_button.pack()

guanbi_button = Button(window, text='选择关闭图片', command=lambda: select_image_file(guanbi_path_label)) guanbi_button.pack()

图片路径

xuehao_path_label = Label(window, text='图片路径:') xuehao_path_label.pack()

chaxun_path_label = Label(window, text='图片路径:') chaxun_path_label.pack()

hege_path_label = Label(window, text='图片路径:') hege_path_label.pack()

guanbi_path_label = Label(window, text='图片路径:') guanbi_path_label.pack()

已选择必要项提示

selection_label = Label(window, text='') selection_label.pack()

开始采集按钮

start_collection_button = Button(window, text='开始采集', command=start_collection, state='disabled') start_collection_button.pack()

查询进度

query_progress_label = Label(window, text='正在查询:0/0') query_progress_label.pack()

运行窗口

window.mainloop()

照片采集查询工具 - 自动化采集考试照片

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

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