支持两种学生名单
This commit is contained in:
@@ -43,7 +43,7 @@ class DocPaper:
|
||||
for run in para.runs:
|
||||
for key, val in data_list.items():
|
||||
if key in run.text:
|
||||
run.text = run.text.replace(key, val)
|
||||
run.text = run.text.replace(key, str(val))
|
||||
break
|
||||
|
||||
def save(self, path: str = './'):
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import random
|
||||
from typing import Optional, Tuple
|
||||
from typing import Optional, Tuple, NoReturn
|
||||
|
||||
from openpyxl.reader.excel import load_workbook
|
||||
|
||||
@@ -60,12 +60,20 @@ class Student:
|
||||
return self.__str__()
|
||||
|
||||
@staticmethod
|
||||
def load_from_xls(path: str) -> list['Student']:
|
||||
def load_from_xls(path: str) -> list['Student'] | NoReturn:
|
||||
wb = load_workbook(path, read_only=True)
|
||||
ws = wb.active
|
||||
students = []
|
||||
|
||||
if ws.title == 'Sheet1':
|
||||
for row in ws.iter_rows(min_row=6, max_col=5, values_only=True):
|
||||
students.append(Student(*row))
|
||||
elif ws.title == '初始录入':
|
||||
for row in ws.iter_rows(min_row=13, max_col=5, values_only=True):
|
||||
students.append(Student(*row))
|
||||
else:
|
||||
raise Exception("无法解析学生名单")
|
||||
|
||||
wb.close()
|
||||
return [x for x in students if x.valid]
|
||||
|
||||
@@ -114,12 +122,17 @@ class Course:
|
||||
return self.__str__()
|
||||
|
||||
@staticmethod
|
||||
def load_from_xls(path: str) -> 'Course':
|
||||
def load_from_xls(path: str) -> 'Course' | NoReturn:
|
||||
wb = load_workbook(path, read_only=True)
|
||||
ws = wb.active
|
||||
name: str = ws['E3'].value
|
||||
if ws.title == 'Sheet1':
|
||||
name: str = ws['E3'].value[5:]
|
||||
elif ws.title == '初始录入':
|
||||
name: str = ws['D5'].value
|
||||
else:
|
||||
raise Exception("无法解析课程名")
|
||||
wb.close()
|
||||
return Course(name[5:])
|
||||
return Course(name)
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
|
||||
@@ -56,23 +56,23 @@ class DTGWorker(QObject):
|
||||
|
||||
if os.path.exists(pdf_file):
|
||||
os.remove(pdf_file)
|
||||
|
||||
pythoncom.CoInitialize()
|
||||
# https://stackoverflow.com/questions/71292585/python-docx2pdf-attributeerror-open-saveas
|
||||
word = client.Dispatch("Word.Application", pythoncom.CoInitialize())
|
||||
try:
|
||||
word = client.Dispatch("Word.Application")
|
||||
doc = word.Documents.Open(word_file)
|
||||
try:
|
||||
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:
|
||||
doc.Close()
|
||||
word.Quit()
|
||||
elif self.output_type == 'word':
|
||||
os.startfile(word_file)
|
||||
except Exception as e:
|
||||
self.error.emit("😢 不好出错了", e)
|
||||
except Exception:
|
||||
self.error.emit("😢 不好出错了", traceback.format_exc())
|
||||
self.progress[int].emit(-1)
|
||||
finally:
|
||||
self.finished.emit()
|
||||
|
||||
Reference in New Issue
Block a user