在上一篇文章中,我们成功的编写了一个py文件,实现了在线自动化处理excell文件处理数据分数,并且将其打包成了一个exe可执行文件,但是当时的py文件中,我们没有做到将处理好的成绩结果按照总分排序,现在我们增加了这部分的代码,完整的程序如下:
import openpyxl
import os
import sys
from openpyxl import load_workbook
data_path = input("请输入文件路径=====>>")
sheet = load_workbook(data_path)
sheet1 = sheet.get_sheet_by_name('Sheet1') ###这个是要读取的数据表格
sheet2 = sheet.get_sheet_by_name('Sheet2') ###将结果写入这个数据集
###先来读取sheet1表格中的数据
data_list = []
for i in range(2, 42):
list1 = []
for j in range(1, 4):
if j == 1:
list1.append(sheet1.cell(i, j).value)
else:
try:
list1.append(int(sheet1.cell(i, j).value))
except PermissionError:
continue
data_list.append(list1)
data_list1 = []
for i in data_list:
sum = i[1] + i[2] * 2 #####最终的总分计算公式,可以更改
i.append(sum)
data_list1.append(i)
print(len(data_list1))
# a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20,a21,a22,a23,a24,a25,a26,a27,a28,a29,a30,a31,a32,a33,a34,a35,a36,a37,a38,a39,a40=data_list1
####接下来按照冒泡排序法的思想,逐个交换位置,动态排序,
for i in range(0, len(data_list1)):
for j in range(i, len(data_list1)):
if data_list1[i][3] < data_list1[j][3]:
data_list1[i], data_list1[j] = data_list1[j], data_list1[i]
###将结果写入sheet2中
for i in range(0, len(data_list1)):
for j in range(0, 4):
sheet2.cell(i + 2, j + 1).value = data_list1[i][j]
sheet.save(data_path)