#!/usr/bin/env python3
"""
Parse MIS.xlsx file to understand structure and design
"""
import sys
try:
    import openpyxl
    from openpyxl import load_workbook
except ImportError:
    print("Installing openpyxl...")
    import subprocess
    subprocess.check_call([sys.executable, "-m", "pip", "install", "openpyxl"])
    import openpyxl
    from openpyxl import load_workbook

try:
    wb = load_workbook('MIS.xlsx', data_only=True)
    
    print("=" * 80)
    print("MIS EXCEL FILE STRUCTURE ANALYSIS")
    print("=" * 80)
    
    # List all sheets
    print(f"\nTotal Sheets: {len(wb.sheetnames)}")
    print(f"Sheet Names: {wb.sheetnames}")
    
    for sheet_name in wb.sheetnames:
        ws = wb[sheet_name]
        print(f"\n{'='*80}")
        print(f"SHEET: {sheet_name}")
        print(f"Max Row: {ws.max_row}, Max Column: {ws.max_column}")
        print(f"{'='*80}")
        
        # Read all cells with data
        for row in range(1, min(ws.max_row + 1, 100)):  # Limit to first 100 rows
            row_data = []
            for col in range(1, min(ws.max_column + 1, 30)):  # Limit to first 30 columns
                cell = ws.cell(row, col)
                value = cell.value
                if value is not None:
                    row_data.append(f"{openpyxl.utils.get_column_letter(col)}{row}:{value}")
            if row_data:
                print(f"Row {row}: {' | '.join(row_data[:5])}")  # Show first 5 cells
        
        # Get merged cells
        if ws.merged_cells:
            print(f"\nMerged Cells: {len(ws.merged_cells)}")
            for merged in list(ws.merged_cells)[:10]:  # Show first 10
                print(f"  {merged}")
        
        # Get cell styles/colors
        print(f"\nSample Cell Styles:")
        for row in range(1, min(10, ws.max_row + 1)):
            for col in range(1, min(10, ws.max_column + 1)):
                cell = ws.cell(row, col)
                if cell.fill.start_color.index != '00000000':  # Not default
                    print(f"  {openpyxl.utils.get_column_letter(col)}{row}: Fill={cell.fill.start_color.index}, Font={cell.font.name}, Bold={cell.font.bold}")
    
    wb.close()
    print("\n" + "=" * 80)
    print("ANALYSIS COMPLETE")
    print("=" * 80)
    
except Exception as e:
    print(f"Error: {e}")
    import traceback
    traceback.print_exc()

