import csv from pathlib import Path from typing import Any import openpyxl import pandas as pd class FileParserService: """文件解析服务""" @staticmethod def parse_excel(file_path: str | Path) -> dict[str, Any]: """ 解析 Excel 文件 Args: file_path: 文件路径 Returns: 解析结果:headers, sample_rows, total_rows """ file_path = Path(file_path) # 使用 openpyxl 解析 .xlsx if file_path.suffix.lower() == ".xlsx": wb = openpyxl.load_workbook(file_path, read_only=True, data_only=True) ws = wb.active # 获取表头(第一行) headers = [cell.value for cell in ws[1]] # 获取样例数据(前5行,不含表头) sample_rows = [] for row_idx, row in enumerate(ws.iter_rows(min_row=2, max_row=6, values_only=True), start=2): row_data = dict(zip(headers, row)) sample_rows.append(row_data) # 获取总行数 total_rows = ws.max_row - 1 # 减去表头行 wb.close() return { "headers": headers, "sample_rows": sample_rows, "total_rows": total_rows, } # 使用 pandas 解析其他格式 else: df = pd.read_excel(file_path) return { "headers": df.columns.tolist(), "sample_rows": df.head(5).to_dict(orient="records"), "total_rows": len(df), } @staticmethod def parse_csv(file_path: str | Path) -> dict[str, Any]: """ 解析 CSV 文件 Args: file_path: 文件路径 Returns: 解析结果 """ file_path = Path(file_path) # 自动检测编码 encodings = ["utf-8", "gbk", "gb2312", "utf-8-sig"] df = None for encoding in encodings: try: df = pd.read_csv(file_path, encoding=encoding) break except (UnicodeDecodeError, Exception): continue if df is None: raise ValueError("无法解析CSV文件,编码格式不支持") return { "headers": df.columns.tolist(), "sample_rows": df.head(5).to_dict(orient="records"), "total_rows": len(df), } @staticmethod def detect_file_type(file_content: bytes) -> str: """ 检测文件类型 Args: file_content: 文件内容 Returns: 文件类型 """ # 简单的文件类型检测 if file_content[:2] == b"PK": return "xlsx" elif file_content[:8] == b"\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1": return "xls" else: return "csv" # 单例实例 file_parser_service = FileParserService()