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