import os from pathlib import Path from PySide6.QtCore import QObject, Signal from module.doc import DocPaper from module.schema import Course, Student, Question from utils.function import resource_path class DTGWorker(QObject): progress = Signal(int) finished = Signal() def __init__( self, input_student_filepath: str, input_question_filepath: str, output_filepath: str, output_filename: str ): super().__init__() self.input_filepath = input_student_filepath self.input_question_filepath = input_question_filepath self.output_filepath = output_filepath self.output_filename = output_filename def run(self): course = Course.load_from_xls(self.input_filepath) students = Student.load_from_xls(self.input_filepath) questions = Question.load_from_xls(self.input_question_filepath) d = DocPaper(self.output_filename, template_path=resource_path("template/template.docx")) for index, student in enumerate(students): if (p := int((index + 1) / len(students) * 100)) != 100: self.progress.emit(p) else: self.progress.emit(99) student.pick_question(questions) d.add_paper(course, student) d.save(self.output_filepath) self.progress.emit(100) os.startfile(Path(self.output_filepath) / f"{self.output_filename}.docx") self.finished.emit()