#!/usr/bin/env python3
import pandas as pd
import sys
import json

def read_excel_file(file_path):
    try:
        # 读取Excel文件
        excel_file = pd.ExcelFile(file_path)
        
        print(f"📊 Excel文件分析报告")
        print(f"文件名: {file_path}")
        print(f"工作表数量: {len(excel_file.sheet_names)}")
        print(f"工作表名称: {excel_file.sheet_names}")
        print("=" * 60)
        
        results = {}
        
        for sheet_name in excel_file.sheet_names:
            print(f"\n📋 工作表: {sheet_name}")
            
            # 读取工作表
            df = excel_file.parse(sheet_name)
            
            print(f"行数: {df.shape[0]}")
            print(f"列数: {df.shape[1]}")
            print(f"列名: {list(df.columns)}")
            
            # 显示前几行数据
            print("\n前5行数据:")
            print(df.head().to_string())
            
            # 统计信息
            print("\n📈 数据统计:")
            print(f"空值数量: {df.isnull().sum().sum()}")
            print(f"数据类型分布:")
            print(df.dtypes.value_counts())
            
            # 保存到结果
            results[sheet_name] = {
                'rows': df.shape[0],
                'columns': df.shape[1],
                'column_names': list(df.columns),
                'sample_data': df.head().to_dict(),
                'null_count': int(df.isnull().sum().sum())
            }
            
            print("-" * 40)
        
        # 保存分析结果
        with open('/root/.openclaw/agents/101/workspace/excel_analysis.json', 'w', encoding='utf-8') as f:
            json.dump(results, f, ensure_ascii=False, indent=2)
        
        print(f"\n✅ 分析完成！结果已保存到: /root/.openclaw/agents/101/workspace/excel_analysis.json")
        
        return results
        
    except Exception as e:
        print(f"❌ 读取Excel文件时出错: {e}")
        return None

if __name__ == "__main__":
    file_path = "/root/.openclaw/media/inbound/ä¼_ä_å¾_ä_è_½å_é_ªè_å_æ_å_é---86c8a8c3-f1bc-47fc-88e5-fb1bd39dc7fc.xlsx"
    read_excel_file(file_path)