1
+ #! /usr/bin/env python
2
+ # Version: 0.2.5
3
+
4
+ from win32com .client import Dispatch
5
+ from pythonmisc import string_manipulation as sm
6
+
7
+
8
+ class ExcelVBA (object ):
9
+
10
+ visible = False
11
+ display_alerts = False
12
+ instance = None
13
+
14
+ def __init__ (self , visible , alerts ):
15
+ self .instance = Dispatch ("Excel.Application" )
16
+ self .instance .Visible = visible or self .visible
17
+ self .instance .DisplayAlerts = alerts or self .display_alerts
18
+
19
+ def get_workbook_from_file (self , filepath , visible , alerts ):
20
+ wb = self .instance .Workbooks .Open (filepath )
21
+ wb = Dispatch (wb )
22
+ # Override options, since can be a new file without the configs from self.instance
23
+ self .instance .Visible = visible
24
+ self .instance .DisplayAlerts = alerts
25
+ return wb
26
+
27
+ def save_workbook_in_file (self , wb , filepath ):
28
+ return wb .saveAs (filepath )
29
+
30
+ def add_module (self , wb , name , pattern_type , str_code ):
31
+ xlmodule = wb .VBProject .VBComponents .Add (1 )
32
+ module_name = (name + pattern_type ).encode ('ascii' , 'ignore' )
33
+ # Modules names aren't accepted with special characters (only letters)
34
+ module_name = sm .remove_special_characters (module_name )
35
+ xlmodule .CodeModule .Name = module_name
36
+ xlmodule .CodeModule .AddFromString (str_code )
37
+ return xlmodule
38
+
39
+ def add_class (self , wb , name , str_code ):
40
+ xlclass = wb .VBProject .VBComponents .Add (2 )
41
+ module_name = name .encode ('ascii' , 'ignore' )
42
+ # Modules names aren't accepted with special characters (only letters)
43
+ module_name = sm .remove_special_characters (module_name )
44
+ xlclass .CodeModule .Name = module_name
45
+ xlclass .CodeModule .AddFromString (str_code )
46
+ return xlclass
47
+
48
+ def add_form (self , wb ):
49
+ xlform = wb .VBProject .VBComponents .Add (3 )
50
+ return xlform
51
+
52
+ def get_module (self , wb , name , pattern_type ):
53
+ try :
54
+ full_name = (name + pattern_type ).encode ('ascii' , 'ignore' )
55
+ # Modules names aren't accepted with special characters (only letters)
56
+ full_name = sm .remove_special_characters (full_name )
57
+ return wb .VBProject .VBComponents (full_name )
58
+ except Exception as e :
59
+ # print e.args[2]
60
+ print 'Module does not exist!'
61
+ return None
62
+
63
+ def import_component (self , wb , name , pattern_type , filepath ):
64
+ xlcomponent = wb .VBProject .VBComponents .Import (filepath )
65
+ if pattern_type == 'class' :
66
+ module_name = name .encode ('ascii' , 'ignore' )
67
+ # Modules names aren't accepted with special characters (only letters)
68
+ module_name = sm .remove_special_characters (module_name )
69
+ xlcomponent .CodeModule .Name = module_name
70
+ else :
71
+ module_name = (name + pattern_type ).encode ('ascii' , 'ignore' )
72
+ # Modules names aren't accepted with special characters (only letters)
73
+ module_name = sm .remove_special_characters (module_name )
74
+ xlcomponent .CodeModule .Name = module_name
75
+ return xlcomponent
76
+
77
+ def export_component (self , wb , name , pattern_type , filepath ):
78
+ name = name .encode ('ascii' , 'ignore' )
79
+ pattern_type = pattern_type .encode ('ascii' , 'ignore' )
80
+ # Modules names aren't accepted with special characters (only letters)
81
+ name = sm .remove_special_characters (name )
82
+ xlcomponent = self .get_module (wb , name , pattern_type )
83
+ if xlcomponent is not None :
84
+ xlcomponent .Export (filepath )
85
+ else :
86
+ print 'Module does not exist and cannot be exported!'
87
+
88
+ def destroy (self ):
89
+ self .instance .Quit ()
90
+
91
+ def remove_component (self , wb , name , pattern_type ):
92
+ name = name .encode ('ascii' , 'ignore' )
93
+ pattern_type = pattern_type .encode ('ascii' , 'ignore' )
94
+ # Modules names aren't accepted with special characters (only letters)
95
+ name = sm .remove_special_characters (name )
96
+ xlcomponent = self .get_module (wb , name , pattern_type )
97
+ if xlcomponent is not None :
98
+ xlcomponent .Remove ()
99
+ else :
100
+ print 'Module does not exist and cannot be removed!'
101
+
102
+ def remove_all_components (self , wb ):
103
+ try :
104
+ # for i in range(1, wb.VBProject.VBComponents.Count + 1):
105
+ for component in wb .VBProject .VBComponents :
106
+ # xlmodule = wb.VBProject.VBComponents(i)
107
+ if component .Type in [1 , 2 , 3 ]:
108
+ wb .VBProject .VBComponents .Remove (component )
109
+ except Exception as e :
110
+ print e
0 commit comments