File size: 2,491 Bytes
4e67a93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
"""
Test script to verify Excel to CSV conversion functionality
"""

import pandas as pd
import os
import sys
from csv_utils import robust_csv_loader, is_excel_file

def test_excel_detection():
    """Test the Excel file detection function."""
    print("=== Testing Excel File Detection ===")
    
    test_files = [
        "test.csv",
        "test.xls", 
        "test.xlsx",
        "test.xlsm",
        "test.txt",
        "test.XLSX",  # Test case sensitivity
        "file.with.dots.xlsx"
    ]
    
    for filename in test_files:
        result = is_excel_file(filename)
        print(f"{filename}: {'Excel' if result else 'Not Excel'}")
    
    print()

def test_excel_loading():
    """Test loading Excel files through the robust loader."""
    print("=== Testing Excel File Loading ===")
    
    # Create a sample Excel file for testing
    test_data = pd.DataFrame({
        'Email': ['user1@example.com', 'user2@example.com', 'user3@example.com'],
        'FY23/24 全年IPM': ['100%', '120%', '95%'],
        'FY24/25 全年IPM': ['110%', '125%', '98%']
    })
    
    # Save as Excel
    excel_path = 'test_kpi.xlsx'
    test_data.to_excel(excel_path, index=False)
    print(f"Created test Excel file: {excel_path}")
    
    try:
        # Test loading the Excel file
        print("\nLoading Excel file with robust_csv_loader...")
        df = robust_csv_loader(excel_path, required_columns=['Email'])
        
        print(f"\nSuccessfully loaded {len(df)} rows")
        print(f"Columns: {df.columns.tolist()}")
        print("\nFirst few rows:")
        print(df.head())
        
    except Exception as e:
        print(f"Error loading Excel file: {e}")
    
    finally:
        # Clean up test file
        if os.path.exists(excel_path):
            os.remove(excel_path)
            print(f"\nCleaned up test file: {excel_path}")

def main():
    """Run all tests."""
    test_excel_detection()
    test_excel_loading()
    
    print("\n=== Testing analyze_correlations_v2.py with Excel files ===")
    print("The analyze_correlations_v2.py script now supports Excel files!")
    print("You can run it with Excel files like this:")
    print("  python3 analyze_correlations_v2.py -k kpi_data.xlsx -s scores.csv")
    print("  python3 analyze_correlations_v2.py -k kpi_data.xls -s scores.xlsx")
    print("\nThe script will automatically detect Excel files and convert them to CSV for processing.")

if __name__ == "__main__":
    main()