MCPcopy Index your code
hub / github.com/dataease/SQLBot / parse_excel_preview

Function parse_excel_preview

backend/apps/datasource/utils/excel.py:28–64  ·  view source on GitHub ↗
(save_path: str, max_rows: int = 10)

Source from the content-addressed store, hash-verified

26
27
28def parse_excel_preview(save_path: str, max_rows: int = 10):
29 sheets_data = []
30 if save_path.endswith(".csv"):
31 df = pd.read_csv(save_path, engine='c')
32 fields = []
33 for col in df.columns:
34 fields.append({
35 "fieldName": col,
36 "fieldType": infer_field_type(df[col].dtype)
37 })
38 preview_df = df.head(max_rows).replace({pd.NA: None, float('nan'): None})
39 preview_data = preview_df.to_dict(orient='records')
40 sheets_data.append({
41 "sheetName": "Sheet1",
42 "fields": fields,
43 "data": preview_data,
44 "rows": len(df)
45 })
46 else:
47 sheet_names = pd.ExcelFile(save_path).sheet_names
48 for sheet_name in sheet_names:
49 df = pd.read_excel(save_path, sheet_name=sheet_name, engine='calamine')
50 fields = []
51 for col in df.columns:
52 fields.append({
53 "fieldName": col,
54 "fieldType": infer_field_type(df[col].dtype)
55 })
56 preview_df = df.head(max_rows).replace({pd.NA: None, float('nan'): None})
57 preview_data = preview_df.to_dict(orient='records')
58 sheets_data.append({
59 "sheetName": sheet_name,
60 "fields": fields,
61 "data": preview_data,
62 "rows": len(df)
63 })
64 return sheets_data

Callers 1

innerFunction · 0.85

Calls 4

infer_field_typeFunction · 0.85
replaceMethod · 0.80
appendMethod · 0.45
to_dictMethod · 0.45

Tested by

no test coverage detected