File size: 6,102 Bytes
5f3e9f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"""Platform and dependency validation for PowerPoint automation.

This module provides utilities to verify that the system meets the requirements
for PowerPoint automation features (Windows OS, PowerPoint installation, etc.).
"""

import platform
import sys
from typing import Tuple, Optional


class PlatformCheckError(Exception):
    """Base exception for platform compatibility issues."""
    pass


class WindowsRequiredError(PlatformCheckError):
    """Raised when PowerPoint features are used on non-Windows platforms."""
    pass


class PowerPointNotInstalledError(PlatformCheckError):
    """Raised when PowerPoint is not installed or not accessible."""
    pass


class PyWin32NotInstalledError(PlatformCheckError):
    """Raised when pywin32 package is not installed."""
    pass


def is_windows() -> bool:
    """Check if the current platform is Windows.
    
    Returns:
        True if running on Windows, False otherwise
    """
    return platform.system() == 'Windows'


def check_platform_compatibility() -> Tuple[bool, Optional[str]]:
    """Verify the system can run PowerPoint automation.
    
    Returns:
        Tuple of (is_compatible, error_message)
        - is_compatible: True if all requirements met
        - error_message: None if compatible, error description otherwise
    """
    # Check if running on Windows
    if not is_windows():
        return False, (
            f"PowerPoint automation requires Windows. "
            f"Current platform: {platform.system()} {platform.release()}"
        )
    
    # Check if pywin32 is installed
    try:
        import win32com.client
    except ImportError:
        return False, (
            "pywin32 is not installed. "
            "Install with: pip install pywin32"
        )
    
    return True, None


def check_powerpoint_installed() -> Tuple[bool, Optional[str]]:
    """Check if Microsoft PowerPoint is installed and accessible via COM.
    
    Returns:
        Tuple of (is_installed, error_message)
        - is_installed: True if PowerPoint is accessible
        - error_message: None if installed, error description otherwise
    """
    if not is_windows():
        return False, "PowerPoint is only available on Windows"
    
    try:
        import win32com.client
        import pywintypes
        
        # Try to create PowerPoint application instance
        try:
            ppt = win32com.client.Dispatch("PowerPoint.Application")
            version = ppt.Version
            ppt.Quit()
            return True, None
        except pywintypes.com_error as e:
            return False, (
                "Microsoft PowerPoint is not installed or not accessible. "
                f"COM error: {e.args[2][2] if len(e.args) > 2 else str(e)}"
            )
    except ImportError:
        return False, "pywin32 is not installed"


def get_powerpoint_version() -> Optional[str]:
    """Get the installed PowerPoint version.
    
    Returns:
        Version string (e.g., "16.0") or None if not installed
    """
    if not is_windows():
        return None
    
    try:
        import win32com.client
        ppt = win32com.client.Dispatch("PowerPoint.Application")
        version = ppt.Version
        ppt.Quit()
        return version
    except Exception:
        return None


def check_all_requirements() -> Tuple[bool, str]:
    """Check all requirements for PowerPoint automation.
    
    Returns:
        Tuple of (all_met, status_message)
        - all_met: True if all requirements are met
        - status_message: Detailed status message
    """
    messages = []
    all_met = True
    
    # Check platform
    platform_ok, platform_msg = check_platform_compatibility()
    if not platform_ok:
        all_met = False
        messages.append(f"❌ Platform: {platform_msg}")
    else:
        messages.append(f"✓ Platform: Windows {platform.release()}")
    
    # Check PowerPoint installation
    if platform_ok:
        ppt_ok, ppt_msg = check_powerpoint_installed()
        if not ppt_ok:
            all_met = False
            messages.append(f"❌ PowerPoint: {ppt_msg}")
        else:
            version = get_powerpoint_version()
            messages.append(f"✓ PowerPoint: Version {version} installed")
    
    # Check Python version
    py_version = f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}"
    if sys.version_info >= (3, 8):
        messages.append(f"✓ Python: {py_version}")
    else:
        all_met = False
        messages.append(f"❌ Python: {py_version} (requires 3.8+)")
    
    status_message = "\n".join(messages)
    return all_met, status_message


def require_windows() -> None:
    """Raise an exception if not running on Windows.
    
    Raises:
        WindowsRequiredError: If not running on Windows
    """
    if not is_windows():
        raise WindowsRequiredError(
            f"This feature requires Windows. Current platform: {platform.system()}"
        )


def require_powerpoint() -> None:
    """Raise an exception if PowerPoint is not installed.
    
    Raises:
        PowerPointNotInstalledError: If PowerPoint is not accessible
    """
    is_installed, error_msg = check_powerpoint_installed()
    if not is_installed:
        raise PowerPointNotInstalledError(error_msg)


def require_all() -> None:
    """Raise an exception if any requirements are not met.
    
    Raises:
        PlatformCheckError: If any requirement is not met
    """
    all_met, status_message = check_all_requirements()
    if not all_met:
        raise PlatformCheckError(
            f"PowerPoint automation requirements not met:\n{status_message}"
        )


if __name__ == "__main__":
    # Run checks when executed directly
    all_met, status = check_all_requirements()
    print("PowerPoint Automation Requirements Check")
    print("=" * 50)
    print(status)
    print("=" * 50)
    if all_met:
        print("\n✓ All requirements met! PowerPoint automation is available.")
    else:
        print("\n❌ Some requirements are not met. PowerPoint automation is unavailable.")
        sys.exit(1)