添加数据验证逻辑,抛出自定义异常以处理验证失败情况;更新错误处理逻辑以显示详细错误信息

This commit is contained in:
2026-01-20 23:19:10 +08:00
parent 28e35ea429
commit 96350bb8e2
3 changed files with 41 additions and 8 deletions

View File

@@ -160,8 +160,9 @@ class DocxWriter:
cell_end = table.cell(row, col_span + non_none_count - 1) cell_end = table.cell(row, col_span + non_none_count - 1)
cell_start.merge(cell_end) cell_start.merge(cell_end)
except IndexError: except IndexError:
self.signal(f"单元格合并失败:({row}, {col_span}),需要自行检查表格准确性", pass
LOGLEVEL.WARNING) # self.signal(f"单元格合并失败:({row}, {col_span}),需要自行检查表格准确性",
# LOGLEVEL.WARNING)
col_span += non_none_count col_span += non_none_count
start = rows - X + 3 + self.excel_reader.kpi_number start = rows - X + 3 + self.excel_reader.kpi_number

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2025 Jeffrey Hsu - JITToolBox # Copyright (c) 2025-2026 Jeffrey Hsu - JITToolBox
# # # #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
@@ -73,6 +73,13 @@ class ExcelReader:
image = io.BytesIO(self._images[cell]()) image = io.BytesIO(self._images[cell]())
return Image.open(image) return Image.open(image)
class ValidError(Exception):
def __init__(self, message):
self.message = message
def __str__(self):
return self.message
def __init__(self, file_path: str, version_check: bool = False, def __init__(self, file_path: str, version_check: bool = False,
signal: Callable[[str, str], None] = lambda x, y: print(x)): signal: Callable[[str, str], None] = lambda x, y: print(x)):
super().__init__() super().__init__()
@@ -271,9 +278,15 @@ class ExcelReader:
for i in range(len(self.suggestion_template_list), 5): for i in range(len(self.suggestion_template_list), 5):
self.suggestion_template_list.append(None) self.suggestion_template_list.append(None)
self.validate_data() if vd_lst := self.validate_data():
raise self.ValidError("\n\n".join(vd_lst))
self.gen_picture() self.gen_picture()
except self.ValidError as ve:
raise Exception(f"""
数据验证失败:\n\n{str(ve)}
""")
except Exception as e: except Exception as e:
error_message = traceback.format_exc() error_message = traceback.format_exc()
raise Exception(f""" raise Exception(f"""
@@ -284,9 +297,18 @@ class ExcelReader:
def set_file_path(self, file_path: str): def set_file_path(self, file_path: str):
self.file_path = file_path self.file_path = file_path
def validate_data(self): def validate_data(self) -> list[str]:
lst: list[str] = []
self.signal("正在验证数据", LOGLEVEL.INFO) self.signal("正在验证数据", LOGLEVEL.INFO)
return 0 if len(self.kpi_list) != self.kpi_number:
self.signal("\"课程目标\"\"目标支撑的毕业要求指标点\"数量与期望目标数量不符", LOGLEVEL.ERROR)
lst.append(
f"\"课程目标\"\"目标支撑的毕业要求指标点\"数量与期望目标数量不符请检查Excel表格中的\"课程目标\"\"目标支撑的毕业要求指标点\"列是否填写完整。"
f"期望得到 {self.kpi_number} 个,实际检测到 {len(self.kpi_list)} 个。"
f"如想暂时不填请在Excel表格对应的位置添加一个空格"
)
return lst
def run(self): def run(self):
self.parse_excel() self.parse_excel()

View File

@@ -269,3 +269,13 @@ class AchievementWidget(Widget):
duration=5000, duration=5000,
parent=self parent=self
) )
elif level == LOGLEVEL.ERROR:
InfoBar.error(
title='错误',
content=content,
orient=Qt.Horizontal,
isClosable=True,
position=InfoBarPosition.TOP_RIGHT,
duration=-1,
parent=self
)