代码如下所示:
import datetime
import json
import os
import random
import tkinter as tk
import openpyxl
# 花名册文件名
excel_file_path = "花名册.xlsx"#需在当前目录创建对应花名册.xlsx
# 工作表名
excel_sheet = "Sheet1"
# 记录存储文件名
file_path = "name_record.json"
#封装业务逻辑
class Call_Name:
# 私有化变量
_namelist = None
# 读取花名册
def __init__(self):
wb = openpyxl.load_workbook(excel_file_path)
get_sheet = wb[excel_sheet]
list_data = []
first_row = True
try:
for row in get_sheet:
# 忽略首行
if first_row:
first_row = False
continue
# 读取第一个单元格数据
for lab in row:
list_data.append(lab.value)
break
except:
list_data = ['空名单?']
self._namelist = list_data
# 初始化记录
def _re_reset(self):
# 记录使用日期
dls = []
dy = str(datetime.datetime.today().date())
dls.append(dy)
# 记录点名
record = {}
for i in self._namelist:
record[i] = 0
name_record = {"date": dls, "last_use": dy, "record": record}
return name_record
# 创建记录
def mk_record(self):
jf_dict = self._re_reset()
with open(file=file_path, mode='w', encoding='utf-8') as jf:
json.dump(jf_dict, jf, indent=2, sort_keys=True, ensure_ascii=False)
# 读