174 lines
7.4 KiB
Python
174 lines
7.4 KiB
Python
from PySide6.QtWidgets import QWidget, QApplication, QFileDialog, QTableWidgetItem
|
||
from Ui_获取小闲平台使用数据new import Ui_Form
|
||
from database_tools import *
|
||
|
||
|
||
def getFilename(string):
|
||
filename = re.findall(r"\d{21}_([\s\S]*?)_高[一二三]_数学",string)[0]
|
||
return filename
|
||
|
||
def getDate(string):
|
||
date = re.findall(r"\((\d{8})\).zip",string)
|
||
if len(date) > 0:
|
||
return date[0]
|
||
else:
|
||
return ""
|
||
|
||
class MyWindow(QWidget,Ui_Form):
|
||
def __init__(self):
|
||
super().__init__()
|
||
self.setupUi(self)
|
||
self.bind()
|
||
def bind(self):
|
||
self.lineEdit_threshold.setText("0.75")
|
||
self.tableWidget.setColumnWidth(0,200)
|
||
self.tableWidget.setColumnWidth(1,280)
|
||
self.tableWidget.setColumnWidth(2,80)
|
||
self.pushButton_file.clicked.connect(self.getFilePath)
|
||
self.pushButton_folder.clicked.connect(self.getDirPath)
|
||
self.pushButton_exec.clicked.connect(self.exec)
|
||
self.singlefile = True
|
||
|
||
|
||
def getFilePath(self): #选取文件并放置在tableWidget的第一行
|
||
self.tableWidget.setRowCount(1)
|
||
pathlist = QFileDialog.getOpenFileName(self,"选择文件",".","zip文件(*.zip);;所有文件(*)")
|
||
self.zipfilepath = pathlist[0]
|
||
self.tableWidget.setItem(0,0,QTableWidgetItem(pathlist[0]))
|
||
self.singlefile = True
|
||
filename = getFilename(pathlist[0])
|
||
self.tableWidget.setItem(0,1,QTableWidgetItem(filename))
|
||
self.tableWidget.setItem(0,2,QTableWidgetItem(getDate(pathlist[0])))
|
||
|
||
|
||
|
||
|
||
|
||
def getDirPath(self): #选取文件夹中的所有符合答题纸特征的.zip文件并放置在tableWidget中
|
||
dirpath = QFileDialog.getExistingDirectory(self,"选择文件夹")
|
||
self.filelist = [os.path.join(dirpath,f) for f in os.listdir(dirpath) if ".zip" in f and re.findall(r"\d{21}",f)!=[]]
|
||
self.singlefile = False
|
||
count = 0
|
||
for f in self.filelist:
|
||
if count + 1 > self.tableWidget.rowCount():
|
||
self.tableWidget.insertRow(self.tableWidget.rowCount())
|
||
self.tableWidget.setItem(count,0,QTableWidgetItem(f))
|
||
self.tableWidget.setItem(count,1,QTableWidgetItem(getFilename(f)))
|
||
self.tableWidget.setItem(count,2,QTableWidgetItem(getDate(f)))
|
||
count += 1
|
||
def exec(self):
|
||
if self.singlefile:
|
||
self.execsinglefile()
|
||
else:
|
||
self.execmultifile()
|
||
|
||
def execmultifile(self): #待修改
|
||
directory = self.label_filepath.text()
|
||
files = [f for f in os.listdir(directory) if ".zip" in f]
|
||
datedfiles = []
|
||
tempdir = "临时文件/zips"
|
||
statsfilename = "小题分_按学号(数学).xlsx"
|
||
answersheetseekingpath = "../备课组"
|
||
glossoutput = ""
|
||
threshold = float(self.lineEdit_threshold.text())
|
||
for f in files:
|
||
fpath = os.path.join(directory,f)
|
||
if len(re.findall(r"\(\d{8}\)",f)) == 0:
|
||
print(f"文件 {f} 未标注日期.")
|
||
else:
|
||
datedfiles.append(fpath)
|
||
for zipfpath in datedfiles:
|
||
try:
|
||
try:
|
||
shutil.rmtree(tempdir)
|
||
except:
|
||
pass
|
||
makedir(tempdir)
|
||
date = re.findall(r"\((\d{8})\)",zipfpath)[0]
|
||
xiaoxianpid = ParseZipname(zipfpath)
|
||
paperinfo = FindPaper(xiaoxianpid, answersheetseekingpath)
|
||
gradename = paperinfo[1]
|
||
idlist = paperinfo[2]
|
||
zf = zipfile.ZipFile(zipfpath)
|
||
zf.extractall(tempdir) #解压zip文件中的所有内容到tempdir
|
||
statsfilepathlist = FindFile(tempdir,statsfilename)
|
||
validcols,marks = generateColIndexandMarks(statsfilepathlist,statsfilename,paperinfo)
|
||
dfcurrent = pd.read_excel(os.path.join(statsfilepathlist[0],statsfilename))
|
||
correspondence_dict = generateIDtoUsageCorrespondence(idlist,validcols,dfcurrent.iloc[1,validcols])
|
||
output = CalculateUsages(statsfilepathlist,statsfilename,gradename,threshold,marks,correspondence_dict,validcols,date)
|
||
if CheckUsagesValidity(output) == 0:
|
||
glossoutput += output
|
||
else:
|
||
print(f"{zipfpath} 数据有误, 可能需要检查每一题的满分数据")
|
||
self.label_filepath.setText(f"{zipfpath} 数据有误, 可能需要检查每一题的满分数据")
|
||
print(f"文件 {zipfpath} 处理完成")
|
||
except:
|
||
print(f"{zipfpath} 操作中有错误, 需要检查源数据")
|
||
self.label_filepath.setText(f"{zipfpath} 操作中有错误, 需要检查源数据")
|
||
SaveTextFile(glossoutput,"文本文件/metadata.txt")
|
||
os.system("code 文本文件/metadata.txt")
|
||
# print(datedfiles)
|
||
|
||
|
||
|
||
|
||
def execsinglefile(self): #待修改
|
||
date = self.lineEdit_date.text()
|
||
threshold = float(self.lineEdit_threshold.text())
|
||
if not len(date.strip()) == 8:
|
||
self.lineEdit_date.setText("日期格式有误")
|
||
elif not threshold >= 0 or not threshold <= 1:
|
||
self.lineEdit_threshold.setText("阈值有误")
|
||
else:
|
||
tempdir = "临时文件/zips"
|
||
statsfilename = "小题分_按学号(数学).xlsx"
|
||
answersheetseekingpath = "../备课组"
|
||
try:
|
||
shutil.rmtree(tempdir)
|
||
except:
|
||
pass
|
||
makedir(tempdir)
|
||
xiaoxianpid = ParseZipname(self.zipfilepath)
|
||
paperinfo = FindPaper(xiaoxianpid, answersheetseekingpath)
|
||
gradename = paperinfo[1]
|
||
idlist = paperinfo[2]
|
||
|
||
zf = zipfile.ZipFile(self.zipfilepath)
|
||
zf.extractall(tempdir) #解压zip文件中的所有内容到tempdir
|
||
|
||
# papertype = CheckPaperType(tempdir,statsfilename)
|
||
statsfilepathlist = FindFile(tempdir,statsfilename)
|
||
validcols,marks = generateColIndexandMarks(statsfilepathlist,statsfilename,paperinfo)
|
||
dfcurrent = pd.read_excel(os.path.join(statsfilepathlist[0],statsfilename))
|
||
correspondence_dict = generateIDtoUsageCorrespondence(idlist,validcols,dfcurrent.iloc[1,validcols])
|
||
output = CalculateUsages(statsfilepathlist,statsfilename,gradename,threshold,marks,correspondence_dict,validcols,date)
|
||
if CheckUsagesValidity(output) == 0:
|
||
if not self.checkBox_appendflag.isChecked():
|
||
SaveTextFile(output,"文本文件/metadata.txt")
|
||
print("数据文件已输出至metadata.txt")
|
||
self.label_filepath.setText("数据文件已输出至metadata.txt")
|
||
os.system("code 文本文件/metadata.txt")
|
||
else:
|
||
AppendTextFile("\n\n"+output,"文本文件/metadata.txt")
|
||
print("数据文件已添加至metadata.txt")
|
||
self.label_filepath.setText("数据文件已添加至metadata.txt")
|
||
os.system("code 文本文件/metadata.txt")
|
||
|
||
|
||
else:
|
||
print("数据有误, 可能需要检查每一题的满分数据")
|
||
self.label_filepath.setText("数据有误, 可能需要检查每一题的满分数据")
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
if __name__ == '__main__':
|
||
app = QApplication([])
|
||
windows = MyWindow()
|
||
windows.show()
|
||
app.exec()
|
||
|