feat: 新增Excel内容提取支持 & 修复成绩查询课程筛选
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
- 后端 knowledge_analysis_v2.py: 新增 _extract_excel_content 方法支持xlsx/xls文件 - 前端 student-scores.vue: 课程筛选改为动态加载,修复筛选参数传递
This commit is contained in:
@@ -176,7 +176,7 @@ class KnowledgeAnalysisServiceV2:
|
||||
"""
|
||||
提取文档内容
|
||||
|
||||
支持:PDF、Word(docx)、文本文件
|
||||
支持:PDF、Word(docx)、Excel(xlsx/xls)、文本文件
|
||||
"""
|
||||
suffix = file_path.suffix.lower()
|
||||
|
||||
@@ -185,6 +185,8 @@ class KnowledgeAnalysisServiceV2:
|
||||
return await self._extract_pdf_content(file_path)
|
||||
elif suffix in ['.docx', '.doc']:
|
||||
return await self._extract_docx_content(file_path)
|
||||
elif suffix in ['.xlsx', '.xls']:
|
||||
return await self._extract_excel_content(file_path)
|
||||
elif suffix in ['.txt', '.md', '.text']:
|
||||
return await self._extract_text_content(file_path)
|
||||
else:
|
||||
@@ -272,6 +274,35 @@ class KnowledgeAnalysisServiceV2:
|
||||
logger.error(f"文本文件读取失败: {e}")
|
||||
raise ValueError(f"文本文件读取失败: {e}")
|
||||
|
||||
async def _extract_excel_content(self, file_path: Path) -> str:
|
||||
"""提取 Excel 文件内容"""
|
||||
try:
|
||||
from openpyxl import load_workbook
|
||||
|
||||
wb = load_workbook(str(file_path), read_only=True, data_only=True)
|
||||
text_parts = []
|
||||
|
||||
for sheet_name in wb.sheetnames:
|
||||
sheet = wb[sheet_name]
|
||||
text_parts.append(f"【工作表: {sheet_name}】")
|
||||
|
||||
for row in sheet.iter_rows(values_only=True):
|
||||
# 过滤空行
|
||||
row_values = [str(cell) if cell is not None else '' for cell in row]
|
||||
if any(v.strip() for v in row_values):
|
||||
text_parts.append(' | '.join(row_values))
|
||||
|
||||
wb.close()
|
||||
content = '\n'.join(text_parts)
|
||||
return self._clean_content(content)
|
||||
|
||||
except ImportError:
|
||||
logger.error("openpyxl 未安装,无法读取 Excel 文件")
|
||||
raise ValueError("服务器未安装 Excel 读取组件(openpyxl)")
|
||||
except Exception as e:
|
||||
logger.error(f"Excel 文件读取失败: {e}")
|
||||
raise ValueError(f"Excel 文件读取失败: {e}")
|
||||
|
||||
def _clean_content(self, content: str) -> str:
|
||||
"""清理和截断内容"""
|
||||
# 移除多余空白
|
||||
|
||||
Reference in New Issue
Block a user