-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall_openpyxl.py
More file actions
83 lines (66 loc) · 2.28 KB
/
install_openpyxl.py
File metadata and controls
83 lines (66 loc) · 2.28 KB
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
"""
Automatic openpyxl installer for Excel import functionality.
"""
import subprocess
import sys
from pathlib import Path
class OpenpyxlInstaller:
"""Handles automatic installation of openpyxl."""
def __init__(self):
self.package_name = "openpyxl"
self.min_version = "3.0.0"
def is_installed(self) -> bool:
"""Check if openpyxl is already installed."""
try:
import openpyxl
return True
except ImportError:
return False
def install(self) -> bool:
"""
Install openpyxl using pip.
Returns:
True if installation succeeded, False otherwise
"""
if self.is_installed():
print(f"{self.package_name} is already installed")
return True
print(f"Installing {self.package_name}...")
try:
# Use the same Python interpreter that's running this script
python_exe = sys.executable
# Install using pip
result = subprocess.run(
[python_exe, "-m", "pip", "install", f"{self.package_name}>={self.min_version}"],
capture_output=True,
text=True,
timeout=120 # 2 minute timeout
)
if result.returncode == 0:
print(f"Successfully installed {self.package_name}")
return True
else:
print(f"Failed to install {self.package_name}")
print(f"Error: {result.stderr}")
return False
except subprocess.TimeoutExpired:
print(f"Installation timed out after 120 seconds")
return False
except Exception as e:
print(f"Error during installation: {e}")
return False
def main():
"""Standalone installer entry point."""
installer = OpenpyxlInstaller()
if installer.is_installed():
print(f"✓ {installer.package_name} is already installed")
return 0
print(f"Installing {installer.package_name}...")
if installer.install():
print(f"✓ Successfully installed {installer.package_name}")
return 0
else:
print(f"✗ Failed to install {installer.package_name}")
return 1
if __name__ == "__main__":
sys.exit(main())