功能添加:可选文件类型 PDF 或 Word
This commit is contained in:
@@ -4,6 +4,7 @@ import traceback
|
||||
import pythoncom
|
||||
from PySide6.QtCore import QObject, Signal
|
||||
from win32com import client
|
||||
from typing import Literal
|
||||
|
||||
from module.achievement.doc import DocxWriter
|
||||
from module.achievement.excel import ExcelReader
|
||||
@@ -13,7 +14,7 @@ from utils.function import resource_path
|
||||
|
||||
|
||||
class DTGWorker(QObject):
|
||||
progress = Signal(int)
|
||||
progress = Signal((int,), (str,))
|
||||
finished = Signal()
|
||||
error = Signal(str, str)
|
||||
|
||||
@@ -22,13 +23,15 @@ class DTGWorker(QObject):
|
||||
input_student_filepath: str,
|
||||
input_question_filepath: str,
|
||||
output_filepath: str,
|
||||
output_filename: str
|
||||
output_filename: str,
|
||||
output_type: Literal['pdf', 'word'] = 'pdf'
|
||||
):
|
||||
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
|
||||
self.output_type = output_type
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
@@ -39,35 +42,38 @@ class DTGWorker(QObject):
|
||||
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)
|
||||
self.progress[int].emit(p)
|
||||
student.pick_question(questions)
|
||||
d.add_paper(course, student)
|
||||
d.save(self.output_filepath)
|
||||
self.progress.emit(100)
|
||||
|
||||
word_file = self.output_filepath + "/" + self.output_filename + ".docx"
|
||||
pdf_file = self.output_filepath + "/" + self.output_filename + ".pdf"
|
||||
|
||||
if os.path.exists(pdf_file):
|
||||
os.remove(pdf_file)
|
||||
if self.output_type == 'pdf':
|
||||
self.progress[int].emit(101)
|
||||
self.progress[str].emit("正在转换文件")
|
||||
|
||||
# https://stackoverflow.com/questions/71292585/python-docx2pdf-attributeerror-open-saveas
|
||||
word = client.Dispatch("Word.Application", pythoncom.CoInitialize())
|
||||
try:
|
||||
doc = word.Documents.Open(word_file)
|
||||
doc.SaveAs(pdf_file, 17)
|
||||
doc.Close()
|
||||
finally:
|
||||
word.Quit()
|
||||
if os.path.exists(pdf_file):
|
||||
os.remove(pdf_file)
|
||||
|
||||
os.remove(word_file)
|
||||
os.startfile(pdf_file)
|
||||
except Exception as _:
|
||||
error_msg = traceback.format_exc()
|
||||
self.error.emit("😢 不好出错了", error_msg)
|
||||
self.progress.emit(-1)
|
||||
# https://stackoverflow.com/questions/71292585/python-docx2pdf-attributeerror-open-saveas
|
||||
word = client.Dispatch("Word.Application", pythoncom.CoInitialize())
|
||||
try:
|
||||
doc = word.Documents.Open(word_file)
|
||||
doc.SaveAs(pdf_file, 17)
|
||||
doc.Close()
|
||||
os.remove(word_file)
|
||||
os.startfile(pdf_file)
|
||||
except Exception as e:
|
||||
raise Exception("PDF转换失败,但Word文档已生成") from e
|
||||
finally:
|
||||
word.Quit()
|
||||
elif self.output_type == 'word':
|
||||
os.startfile(word_file)
|
||||
except Exception as e:
|
||||
self.error.emit("😢 不好出错了", e)
|
||||
self.progress[int].emit(-1)
|
||||
finally:
|
||||
self.finished.emit()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user