赞
踩
考勤小软件
在大学中,班委考勤需要对每一个同学的情况进行登记,以便在期末的时候能够反馈给相关的老师上课考勤情况。
通常我们班委使用的都是Excel表格进行汇总,当数据量变多了,容易发生差错,而且这个工作重复性高,花费时间多。在这个背景下,我便诞生做一个小软件的想法。
本篇文章使用的语言为:python
使用的软件为:PyCharm
使用的库主要为:openpyxl、PyQt5
首先初始化表格,输入这个学期的每个课程名称,我这里输入的是:
python linux mysql php java
这里同学的名字是代码中写定的一个列表,可以根据自己的需求更改
左上角输入的字符串格式应该如下:
7/24 星期一
课程1
应到:45
实到:44
请假:王靓仔
迟到:康靓仔 张靓仔
旷课:无
点击处理后:
对应的我们在查询窗口输入:
张靓仔
同时,在对应目录下我们也可以直接访问生成的data.xlsx文件:
import calendar
import datetime
import os
import re
import sys
from PyQt5.QtCore import Qt
from PyQt5 import QtGui
from PyQt5.QtWidgets import QWidget, QLabel, QLineEdit, QPushButton, QApplication, QMainWindow, QTextEdit, \
QDesktopWidget, QTableWidget, QTableWidgetItem, QGridLayout, QHeaderView
from openpyxl.reader.excel import load_workbook
from openpyxl.workbook import Workbook
代码如下:
class ExcelWindow(QWidget): # 格式化 def __init__(self): super().__init__() self.init_ui() def init_ui(self): self.setWindowTitle("初始化表格") self.setGeometry(100, 100, 400, 200) self.label = QLabel("请输入这个学期的课程名称(以空格分隔):", self) self.label.move(20, 20) self.edit = QLineEdit(self) self.edit.setGeometry(20, 50, 360, 40) self.button = QPushButton("开始操作", self) self.button.setGeometry(100, 110, 200, 40) self.button.clicked.connect(self.start_operation) icon = QtGui.QIcon("icon.png") # 替换为你的图标文件名 self.setWindowIcon(icon) def start_operation(self): # 使用sys.executable获取运行的exe文件路径 current_dir = os.path.dirname(sys.executable) # 拼接 "data.xlsx" 的完整路径 xlsx_path = os.path.join(current_dir, 'data.xlsx') workbook = Workbook() # 创建一个新的文件 name_ = ["xxx", "xxx", "xxx", "xxx", "xxx", "xxx","xxx"] # 获取同学姓名 courses = self.edit.text().split() # 获取用户输入的课程名称并按空格分隔成列表 # 创建新工作表 stat_sheet = workbook.create_sheet('统计') # 初始化标题行 stat_sheet.cell(row=1, column=1, value='姓名') stat_sheet.cell(row=1, column=2, value='请假总次数') stat_sheet.cell(row=1, column=3, value='迟到总次数') stat_sheet.cell(row=1, column=4, value='旷课总次数') for i, name in enumerate(name_, start=2): stat_sheet.cell(row=i, column=1, value=name) jk = 5 for course in courses: sheet = workbook.create_sheet(course) # 创建工作表,并以课程名命名 stat_sheet.cell(row=1, column=jk, value=course) jk += 1 stat_sheet.cell(row=1, column=jk, value='请假次数') jk += 1 stat_sheet.cell(row=1, column=jk, value='迟到次数') jk += 1 stat_sheet.cell(row=1, column=jk, value='旷课次数') jk += 1 # 初始化第一行从B列开始往右每个单元格为同学的姓名 for i, names in enumerate(name_, start=2): sheet.cell(row=1, column=i, value=names.strip()) # 初始化A列从第二行开始往下分别为当前系统时间往后六个月的每一天 today = datetime.datetime.today().date() # 获取当前计算机时间 dates = [] for i in range(6 * 30): date = today + datetime.timedelta(days=i) month_days = calendar.monthrange(date.year, date.month)[ 1] # 获取当前日期所在月份的天数 while date.day > month_days: # 如果日期超过当月天数,则向后推一天 date += datetime.timedelta(days=1) dates.append(date) for i, date in enumerate(dates, start=2): sheet.cell(row=i, column=1, value=date.strftime("%m/%d")) default_sheet = workbook.get_sheet_by_name('Sheet') # 删除默认的Sheet工作表 workbook.remove_sheet(default_sheet) workbook.save(filename=xlsx_path) # 保存工作簿 workbook.close() # 关闭工作簿 self.close() # 关闭窗口
def execute_code():
app = QApplication([])
window = ExcelWindow()
screen_width = QDesktopWidget().screenGeometry().width() # 获取屏幕大小
screen_height = QDesktopWidget().screenGeometry().height() # 计算窗口左上角坐标使其位于屏幕正中央
x = (screen_width - window.width()) // 2
y = (screen_height - window.height()) // 2
window.move(x, y) # 移动窗口到屏幕正中央
window.show()
app.exec_()
class MainWindow(QMainWindow): # 录入信息 name_ = ["xxx", "xxx", "xxx", "xxx", "xxx", "xxx", "xxx"] def __init__(self): super().__init__() self.setWindowTitle("录入信息") self.setWindowIcon(QtGui.QIcon("icon.png")) # 替换为你的图标文件名 self.label = QLabel("请输入字符串:") self.text_edit = QTextEdit() self.button = QPushButton("处理") self.statistics_button = QPushButton("统计") self.info_label = QLabel("执行情况:") self.info_edit = QTextEdit() self.info_labels = QLabel("查看个人:") self.query_text = QLineEdit() self.query_button = QPushButton("查询") self.info_edits = QTableWidget() self.info_edits.setColumnCount(4) self.info_edits.verticalHeader().setVisible(False) self.info_edits.horizontalHeader().setVisible(False) self.info_edits.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) self.info_edits.verticalHeader().setSectionResizeMode(QHeaderView.Stretch) central_widget = QWidget() self.setCentralWidget(central_widget) layout = QGridLayout(central_widget) layout.addWidget(self.label, 0, 0) layout.addWidget(self.text_edit, 1, 0, 3, 1) layout.addWidget(self.button, 4, 0) layout.addWidget(self.statistics_button, 5, 0) layout.addWidget(self.info_label, 6, 0) layout.addWidget(self.info_edit, 7, 0) layout.addWidget(self.info_labels, 0, 1) layout.addWidget(self.query_text, 0, 2) layout.addWidget(self.query_button, 0, 5) layout.addWidget(self.info_edits, 1, 1, 7, 6) self.button.clicked.connect(self.process_string) self.statistics_button.clicked.connect(self.statistics_) self.query_text.returnPressed.connect(self.table_content) self.query_button.clicked.connect(self.table_content) self.statistics_() self.adjustSize() def table_content(self): name = self.query_text.text() # 获取文本框中的输入字符串 if name not in self.name_: self.query_text.clear() self.info_edits.clear() self.info_edits.setItem(0, 0, QTableWidgetItem(name)) self.info_edits.setItem(0, 1, QTableWidgetItem("被吃掉了")) self.info_edits.setItem(0, 2, QTableWidgetItem('嗷呜~')) self.update_info(f'查无此人') self.update_info(self.formatted_time) self.update_info(f'__________________') return self.query_text.clear() wb = load_workbook(xlsx_path) stat_sheet = wb['统计'] sheet_count = len(wb.sheetnames) + 1 self.info_edits.setRowCount(sheet_count) item = QTableWidgetItem('姓名/课程') item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.info_edits.setItem(0, 0, item) item = QTableWidgetItem('请假') item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.info_edits.setItem(0, 1, item) item = QTableWidgetItem('迟到') item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.info_edits.setItem(0, 2, item) item = QTableWidgetItem('旷课') item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.info_edits.setItem(0, 3, item) ll = 1 jj = 0 for row in range(2, stat_sheet.max_row + 1): if stat_sheet.cell(row=row, column=1).value == name: for col in range(1, stat_sheet.max_column + 1): value = stat_sheet.cell(row=row, column=col).value item = QTableWidgetItem(str(value)) item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.info_edits.setItem(ll, jj, item) jj += 1 if jj % 4 == 0: ll += 1 jj = 0 break self.update_info(f'完成查询') self.update_info(self.formatted_time) self.update_info(f'__________________') wb.close() self.info_edits.resizeRowsToContents() current_time = datetime.datetime.now() formatted_time = current_time.strftime('%y-%m-%d %H:%M:%S') def process_string(self): input_string = self.text_edit.toPlainText() # 获取文本框中的输入字符串 self.text_edit.clear() # 清除文本框内容 result = list(map(str, input_string.split("\n"))) # 调用处理字符串的函数 current_time = datetime.datetime.now() formatted_time = current_time.strftime('%y-%m-%d %H:%M:%S') def extract_numbers(input_string): numbers = re.findall(r'\d+', input_string) return numbers def edit_excel_cell(sheet_name, value1, date_to_find, content): # 使用sys.executable获取运行的exe文件路径 current_dir = os.path.dirname(sys.executable) # 拼接 "data.xlsx" 的完整路径 xlsx_path = os.path.join(current_dir, 'data.xlsx') if os.path.exists(xlsx_path): # 如果文件存在,则读取它 workbook = load_workbook(filename=xlsx_path) if sheet_name in workbook.sheetnames: sheet = workbook[sheet_name] # 查找与给定值1相等的单元格 for row in range(1, sheet.max_row + 1): for column in range(1, sheet.max_column + 1): cell_value = sheet.cell( row=row, column=column).value if cell_value == value1: break else: continue break else: self.update_info("未找到与给定值1相等的单元格,请检查输入。") self.update_info(formatted_time) return row_num = None for row in sheet.iter_rows(min_row=2, max_row=sheet.max_row, min_col=1, max_col=1): cell = row[0] if cell.value == date_to_find.strftime("%m/%d"): row_num = cell.row break if row_num is not None: column_to_edit = column else: self.update_info("找不到指定日期,请检查输入。") self.update_info(formatted_time) return sheet.cell(row=row_num, column=column_to_edit, value=content) workbook.save(filename=xlsx_path) # 保存工作簿 self.update_info("单元格内容已编辑并保存成功。") self.update_info(formatted_time) else: self.update_info("指定的工作表不存在,请检查输入。") self.update_info(formatted_time) workbook.close() # 关闭工作簿 else: self.update_info("Excel文件不存在,请先创建并保存。") self.update_info(formatted_time) try: da_te_ = extract_numbers(result[0]) index_0 = da_te_[0] index_2 = da_te_[1] course = result[1] attend = list(result[4].split(":")[1].split(" ")) late = list(result[5].split(":")[1].split(" ")) absent = list(result[6].split(":")[1].split(" ")) new_list = [index_0, index_2, course, attend] + [late] + [absent] self.data = new_list # 存储数据到self.data date_to_find = datetime.date( 2023, int(self.data[0]), int(self.data[1])) if self.data[3][0] == '无': pass else: for i in self.data[3]: edit_excel_cell(self.data[2], i, date_to_find, "请假") if self.data[4][0] == '无': pass else: for i in self.data[4]: edit_excel_cell(self.data[2], i, date_to_find, "迟到") if self.data[5][0] == '无': pass else: for i in self.data[5]: edit_excel_cell(self.data[2], i, date_to_find, "旷课") except: self.update_info(f'输入的数据错误') self.update_info(formatted_time) self.update_info(f'__________________') def statistics_(self): # 统计代码 wb = load_workbook(xlsx_path) stat_sheet = wb['统计'] row_ = 2 for name in self.name_: absent_count = 0 late_count = 0 skip_count = 0 ren = 5 # 遍历所有表格统计次数 for sheet in wb.worksheets: if sheet.title == '统计': continue absent_counts = 0 late_counts = 0 skip_counts = 0 for col in range(2, sheet.max_column + 1): cell = sheet.cell(row=1, column=col) if cell.value != name: continue for row in range(2, sheet.max_row + 1): cell_value = sheet.cell(row=row, column=col).value if cell_value == '请假': absent_count += 1 absent_counts += 1 elif cell_value == '迟到': late_count += 1 late_counts += 1 elif cell_value == '旷课': skip_count += 1 skip_counts += 1 stat_sheet.cell(row=row_, column=ren, value=sheet.title) ren +=1 stat_sheet.cell(row=row_, column=ren, value=absent_counts) ren += 1 stat_sheet.cell(row=row_, column=ren, value=late_counts) ren += 1 stat_sheet.cell(row=row_, column=ren, value=skip_counts) ren += 1 # 写入统计结果 stat_sheet.cell(row=row_, column=2, value=absent_count) stat_sheet.cell(row=row_, column=3, value=late_count) stat_sheet.cell(row=row_, column=4, value=skip_count) row_ += 1 wb.save(filename=xlsx_path) # 在界面显示统计信息 # 获取行数列数 rows = stat_sheet.max_row # 设置界面表格行数 self.info_edits.setRowCount(rows) # 填充数据 for r in range(1, rows + 1): for c in range(1, 5): value = stat_sheet.cell(row=r, column=c).value item = QTableWidgetItem(str(value)) item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.info_edits.setItem(r - 1, c - 1, item) # 保存工作簿 self.update_info(f'完成统计') self.update_info(self.formatted_time) self.update_info(f'__________________') wb.save(filename=xlsx_path) wb.close() def close_window(self): self.close() # 关闭窗口 def update_info(self, message): self.info_edit.moveCursor(QtGui.QTextCursor.Start) # 将光标移动到文本框开头 self.info_edit.insertPlainText(message + "\n") # 插入新的文本
def processing_content():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
current_dir = os.path.dirname(sys.executable) # 使用sys.executable获取运行的exe文件路径
xlsx_path = os.path.join(current_dir, 'data.xlsx') # 拼接 "data.xlsx" 的完整路径
if os.path.exists(xlsx_path):
processing_content()
else:
execute_code()
processing_content()
首先我们要安装相应的库 Pyinstaller,可以直接在cmd使用pip命令
pip install pyinstaller
当然一般情况,我们国内网速比较慢,需要进行换源操作,这里换清华源:
pip install pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple/
然后我们再到文件相应的目录下,在地址框输入cmd
然后再输入:pyinstaller -F -w
后面加你的文件名
这个小软件实现了openpyxl库和PyQt5库的简单运用,能让我们初步认识openpyxl库和PyQt5库。如果有不足的地方,还请各位大佬指点。
import calendar import datetime import os import re import sys from PyQt5.QtCore import Qt from PyQt5 import QtGui from PyQt5.QtWidgets import QWidget, QLabel, QLineEdit, QPushButton, QApplication, QMainWindow, QTextEdit, \ QDesktopWidget, QTableWidget, QTableWidgetItem, QGridLayout, QHeaderView from openpyxl.reader.excel import load_workbook from openpyxl.workbook import Workbook class ExcelWindow(QWidget): # 格式化 def __init__(self): super().__init__() self.init_ui() def init_ui(self): self.setWindowTitle("初始化表格") self.setGeometry(100, 100, 400, 200) self.label = QLabel("请输入这个学期的课程名称(以空格分隔):", self) self.label.move(20, 20) self.edit = QLineEdit(self) self.edit.setGeometry(20, 50, 360, 40) self.button = QPushButton("开始操作", self) self.button.setGeometry(100, 110, 200, 40) self.button.clicked.connect(self.start_operation) icon = QtGui.QIcon("icon.png") # 替换为你的图标文件名 self.setWindowIcon(icon) def start_operation(self): # 使用sys.executable获取运行的exe文件路径 current_dir = os.path.dirname(sys.executable) # 拼接 "data.xlsx" 的完整路径 xlsx_path = os.path.join(current_dir, 'data.xlsx') workbook = Workbook() # 创建一个新的文件 name_ = ["xxx", "xxx", "xxx", "xxx", "xxx", "xxx", "xxx"] # 获取同学姓名 courses = self.edit.text().split() # 获取用户输入的课程名称并按空格分隔成列表 # 创建新工作表 stat_sheet = workbook.create_sheet('统计') # 初始化标题行 stat_sheet.cell(row=1, column=1, value='姓名') stat_sheet.cell(row=1, column=2, value='请假总次数') stat_sheet.cell(row=1, column=3, value='迟到总次数') stat_sheet.cell(row=1, column=4, value='旷课总次数') for i, name in enumerate(name_, start=2): stat_sheet.cell(row=i, column=1, value=name) jk = 5 for course in courses: sheet = workbook.create_sheet(course) # 创建工作表,并以课程名命名 stat_sheet.cell(row=1, column=jk, value=course) jk += 1 stat_sheet.cell(row=1, column=jk, value='请假次数') jk += 1 stat_sheet.cell(row=1, column=jk, value='迟到次数') jk += 1 stat_sheet.cell(row=1, column=jk, value='旷课次数') jk += 1 # 初始化第一行从B列开始往右每个单元格为同学的姓名 for i, names in enumerate(name_, start=2): sheet.cell(row=1, column=i, value=names.strip()) # 初始化A列从第二行开始往下分别为当前系统时间往后六个月的每一天 today = datetime.datetime.today().date() # 获取当前计算机时间 dates = [] for i in range(6 * 30): date = today + datetime.timedelta(days=i) month_days = calendar.monthrange(date.year, date.month)[ 1] # 获取当前日期所在月份的天数 while date.day > month_days: # 如果日期超过当月天数,则向后推一天 date += datetime.timedelta(days=1) dates.append(date) for i, date in enumerate(dates, start=2): sheet.cell(row=i, column=1, value=date.strftime("%m/%d")) default_sheet = workbook.get_sheet_by_name('Sheet') # 删除默认的Sheet工作表 workbook.remove_sheet(default_sheet) workbook.save(filename=xlsx_path) # 保存工作簿 workbook.close() # 关闭工作簿 self.close() # 关闭窗口 def execute_code(): app = QApplication([]) window = ExcelWindow() screen_width = QDesktopWidget().screenGeometry().width() # 获取屏幕大小 screen_height = QDesktopWidget().screenGeometry().height() # 计算窗口左上角坐标使其位于屏幕正中央 x = (screen_width - window.width()) // 2 y = (screen_height - window.height()) // 2 window.move(x, y) # 移动窗口到屏幕正中央 window.show() app.exec_() class MainWindow(QMainWindow): # 录入信息 name_ = ["xxx", "xxx", "xxx", "xxx", "xxx", "xxx", "xxx"] def __init__(self): super().__init__() self.setWindowTitle("录入信息") self.setWindowIcon(QtGui.QIcon("icon.png")) # 替换为你的图标文件名 self.label = QLabel("请输入字符串:") self.text_edit = QTextEdit() self.button = QPushButton("处理") self.statistics_button = QPushButton("统计") self.info_label = QLabel("执行情况:") self.info_edit = QTextEdit() self.info_labels = QLabel("查看个人:") self.query_text = QLineEdit() self.query_button = QPushButton("查询") self.info_edits = QTableWidget() self.info_edits.setColumnCount(4) self.info_edits.verticalHeader().setVisible(False) self.info_edits.horizontalHeader().setVisible(False) self.info_edits.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) self.info_edits.verticalHeader().setSectionResizeMode(QHeaderView.Stretch) central_widget = QWidget() self.setCentralWidget(central_widget) layout = QGridLayout(central_widget) layout.addWidget(self.label, 0, 0) layout.addWidget(self.text_edit, 1, 0, 3, 1) layout.addWidget(self.button, 4, 0) layout.addWidget(self.statistics_button, 5, 0) layout.addWidget(self.info_label, 6, 0) layout.addWidget(self.info_edit, 7, 0) layout.addWidget(self.info_labels, 0, 1) layout.addWidget(self.query_text, 0, 2) layout.addWidget(self.query_button, 0, 5) layout.addWidget(self.info_edits, 1, 1, 7, 6) self.button.clicked.connect(self.process_string) self.statistics_button.clicked.connect(self.statistics_) self.query_text.returnPressed.connect(self.table_content) self.query_button.clicked.connect(self.table_content) self.statistics_() self.adjustSize() def table_content(self): name = self.query_text.text() # 获取文本框中的输入字符串 if name not in self.name_: self.query_text.clear() self.info_edits.clear() self.info_edits.setItem(0, 0, QTableWidgetItem(name)) self.info_edits.setItem(0, 1, QTableWidgetItem("被吃掉了")) self.info_edits.setItem(0, 2, QTableWidgetItem('嗷呜~')) self.update_info(f'查无此人') self.update_info(self.formatted_time) self.update_info(f'__________________') return self.query_text.clear() wb = load_workbook(xlsx_path) stat_sheet = wb['统计'] sheet_count = len(wb.sheetnames) + 1 self.info_edits.setRowCount(sheet_count) item = QTableWidgetItem('姓名/课程') item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.info_edits.setItem(0, 0, item) item = QTableWidgetItem('请假') item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.info_edits.setItem(0, 1, item) item = QTableWidgetItem('迟到') item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.info_edits.setItem(0, 2, item) item = QTableWidgetItem('旷课') item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.info_edits.setItem(0, 3, item) ll = 1 jj = 0 for row in range(2, stat_sheet.max_row + 1): if stat_sheet.cell(row=row, column=1).value == name: for col in range(1, stat_sheet.max_column + 1): value = stat_sheet.cell(row=row, column=col).value item = QTableWidgetItem(str(value)) item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.info_edits.setItem(ll, jj, item) jj += 1 if jj % 4 == 0: ll += 1 jj = 0 break self.update_info(f'完成查询') self.update_info(self.formatted_time) self.update_info(f'__________________') wb.close() self.info_edits.resizeRowsToContents() current_time = datetime.datetime.now() formatted_time = current_time.strftime('%y-%m-%d %H:%M:%S') def process_string(self): input_string = self.text_edit.toPlainText() # 获取文本框中的输入字符串 self.text_edit.clear() # 清除文本框内容 result = list(map(str, input_string.split("\n"))) # 调用处理字符串的函数 current_time = datetime.datetime.now() formatted_time = current_time.strftime('%y-%m-%d %H:%M:%S') def extract_numbers(input_string): numbers = re.findall(r'\d+', input_string) return numbers def edit_excel_cell(sheet_name, value1, date_to_find, content): # 使用sys.executable获取运行的exe文件路径 current_dir = os.path.dirname(sys.executable) # 拼接 "data.xlsx" 的完整路径 xlsx_path = os.path.join(current_dir, 'data.xlsx') if os.path.exists(xlsx_path): # 如果文件存在,则读取它 workbook = load_workbook(filename=xlsx_path) if sheet_name in workbook.sheetnames: sheet = workbook[sheet_name] # 查找与给定值1相等的单元格 for row in range(1, sheet.max_row + 1): for column in range(1, sheet.max_column + 1): cell_value = sheet.cell( row=row, column=column).value if cell_value == value1: break else: continue break else: self.update_info("未找到与给定值1相等的单元格,请检查输入。") self.update_info(formatted_time) return row_num = None for row in sheet.iter_rows(min_row=2, max_row=sheet.max_row, min_col=1, max_col=1): cell = row[0] if cell.value == date_to_find.strftime("%m/%d"): row_num = cell.row break if row_num is not None: column_to_edit = column else: self.update_info("找不到指定日期,请检查输入。") self.update_info(formatted_time) return sheet.cell(row=row_num, column=column_to_edit, value=content) workbook.save(filename=xlsx_path) # 保存工作簿 self.update_info("单元格内容已编辑并保存成功。") self.update_info(formatted_time) else: self.update_info("指定的工作表不存在,请检查输入。") self.update_info(formatted_time) workbook.close() # 关闭工作簿 else: self.update_info("Excel文件不存在,请先创建并保存。") self.update_info(formatted_time) try: da_te_ = extract_numbers(result[0]) index_0 = da_te_[0] index_2 = da_te_[1] course = result[1] attend = list(result[4].split(":")[1].split(" ")) late = list(result[5].split(":")[1].split(" ")) absent = list(result[6].split(":")[1].split(" ")) new_list = [index_0, index_2, course, attend] + [late] + [absent] self.data = new_list # 存储数据到self.data date_to_find = datetime.date( 2023, int(self.data[0]), int(self.data[1])) if self.data[3][0] == '无': pass else: for i in self.data[3]: edit_excel_cell(self.data[2], i, date_to_find, "请假") if self.data[4][0] == '无': pass else: for i in self.data[4]: edit_excel_cell(self.data[2], i, date_to_find, "迟到") if self.data[5][0] == '无': pass else: for i in self.data[5]: edit_excel_cell(self.data[2], i, date_to_find, "旷课") except: self.update_info(f'输入的数据错误') self.update_info(formatted_time) self.update_info(f'__________________') def statistics_(self): # 统计代码 wb = load_workbook(xlsx_path) stat_sheet = wb['统计'] row_ = 2 for name in self.name_: absent_count = 0 late_count = 0 skip_count = 0 ren = 5 # 遍历所有表格统计次数 for sheet in wb.worksheets: if sheet.title == '统计': continue absent_counts = 0 late_counts = 0 skip_counts = 0 for col in range(2, sheet.max_column + 1): cell = sheet.cell(row=1, column=col) if cell.value != name: continue for row in range(2, sheet.max_row + 1): cell_value = sheet.cell(row=row, column=col).value if cell_value == '请假': absent_count += 1 absent_counts += 1 elif cell_value == '迟到': late_count += 1 late_counts += 1 elif cell_value == '旷课': skip_count += 1 skip_counts += 1 stat_sheet.cell(row=row_, column=ren, value=sheet.title) ren +=1 stat_sheet.cell(row=row_, column=ren, value=absent_counts) ren += 1 stat_sheet.cell(row=row_, column=ren, value=late_counts) ren += 1 stat_sheet.cell(row=row_, column=ren, value=skip_counts) ren += 1 # 写入统计结果 stat_sheet.cell(row=row_, column=2, value=absent_count) stat_sheet.cell(row=row_, column=3, value=late_count) stat_sheet.cell(row=row_, column=4, value=skip_count) row_ += 1 wb.save(filename=xlsx_path) # 在界面显示统计信息 # 获取行数列数 rows = stat_sheet.max_row # 设置界面表格行数 self.info_edits.setRowCount(rows) # 填充数据 for r in range(1, rows + 1): for c in range(1, 5): value = stat_sheet.cell(row=r, column=c).value item = QTableWidgetItem(str(value)) item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.info_edits.setItem(r - 1, c - 1, item) # 保存工作簿 self.update_info(f'完成统计') self.update_info(self.formatted_time) self.update_info(f'__________________') wb.save(filename=xlsx_path) wb.close() def close_window(self): self.close() # 关闭窗口 def update_info(self, message): self.info_edit.moveCursor(QtGui.QTextCursor.Start) # 将光标移动到文本框开头 self.info_edit.insertPlainText(message + "\n") # 插入新的文本 def processing_content(): app = QApplication(sys.argv) window = MainWindow() window.show() app.exec_() current_dir = os.path.dirname(sys.executable) # 使用sys.executable获取运行的exe文件路径 xlsx_path = os.path.join(current_dir, 'data.xlsx') # 拼接 "data.xlsx" 的完整路径 if os.path.exists(xlsx_path): processing_content() else: execute_code() processing_content()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。