import pandas as pd
import numpy as np
import sys

# 兼容numpy版本
try:
    np._get_promotion_state = lambda *args, **kwargs: 0
except:
    pass

def generate_excel(l1_oops_csv, l2_oops_csv, l1_exit_csv, l2_exit_csv, output_path):
    """
    生成包含4个sheet的Excel报表
    :param l1_oops_csv: L1等级Oops率统计结果CSV路径
    :param l2_oops_csv: L2等级Oops率统计结果CSV路径
    :param l1_exit_csv: L1等级退出率统计结果CSV路径
    :param l2_exit_csv: L2等级退出率统计结果CSV路径
    :param output_path: 输出Excel文件路径
    """
    # 读取CSV文件
    df_l1_oops = pd.read_csv(l1_oops_csv)
    df_l2_oops = pd.read_csv(l2_oops_csv)
    df_l1_exit = pd.read_csv(l1_exit_csv)
    df_l2_exit = pd.read_csv(l2_exit_csv)

    # 创建Excel文件
    with pd.ExcelWriter(output_path) as writer:
        df_l1_oops.to_excel(writer, sheet_name='L1组件Oops率', index=False)
        df_l2_oops.to_excel(writer, sheet_name='L2组件Oops率', index=False)
        df_l1_exit.to_excel(writer, sheet_name='L1组件退出率', index=False)
        df_l2_exit.to_excel(writer, sheet_name='L2组件退出率', index=False)

    print(f"Excel报表生成成功：{output_path}")

if __name__ == "__main__":
    if len(sys.argv) != 6:
        print("用法：python generate_excel.py <l1_oops_csv路径> <l2_oops_csv路径> <l1_exit_csv路径> <l2_exit_csv路径> <输出Excel路径>")
        sys.exit(1)
    
    generate_excel(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5])
