MCPcopy Create free account
hub / github.com/dothinking/PyAddin / UI

Class UI

pyaddin/xlam/ui.py:6–65  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4
5
6class UI:
7
8 def __init__(self, xlam_file:str) -> None:
9 '''Create Excel add-in (*.xlam) with customized ribbon tab.
10
11 Args:
12 xlam_file (str): The add-in file to create or update.
13 '''
14 self.xlam_file = xlam_file
15 self.path, self.name = os.path.split(xlam_file)
16
17
18 def create(self, template_path:str, custom_ui_filename:str='CustomUI.xml'):
19 '''Combine CustomUI.xml with template xml files to create a new add-in file.
20
21 Args:
22 template_path (str): directory for template xml files extracted from a general add-in file.
23 custom_ui_filename (str): full path to CustomUI.xml defining the ribbon UI.
24 '''
25 # copy template files to current path if they're not under current path
26 work_path = os.path.join(self.path, os.path.basename(template_path))
27 if work_path.upper() != template_path.upper():
28 # delete dest dir if exists, otherwise the copy will be forbidden
29 if os.path.isdir(work_path): shutil.rmtree(work_path)
30 shutil.copytree(template_path, work_path)
31
32 # copy customUI.xml to path/template/customUI
33 dest_ui_path = os.path.join(work_path, 'customUI')
34 if not os.path.exists(dest_ui_path):
35 os.mkdir(dest_ui_path)
36 shutil.copy(custom_ui_filename, dest_ui_path)
37
38 # archive xml files and remove original package
39 shutil.make_archive(os.path.join(self.path, self.name), 'zip', work_path)
40 shutil.rmtree(work_path)
41
42 # convert to add-in *.xlam
43 zip_file = os.path.join(self.path, f'{self.name}.zip')
44 shutil.move(zip_file, self.xlam_file)
45
46
47 def update(self, custom_ui_filename:str='CustomUI.xml'):
48 '''Update current add-in ribbon with specified CustomUI.xml.
49
50 Args:
51 custom_ui_filename (str): full path to CustomUI.xml defining the ribbon UI.
52 '''
53 if not os.path.exists(self.xlam_file):
54 raise AddInException(f'Not find add-in file: {self.xlam_file}.')
55
56 # rename: name.xlam -> name.zip
57 zip_file = os.path.join(self.path, '{0}.zip'.format(self.name))
58 shutil.move(self.xlam_file, zip_file)
59
60 # unpack: name.zip -> name/
61 xml_path = os.path.join(self.path, self.name)
62 shutil.unpack_archive(zip_file, xml_path)
63

Callers 2

createMethod · 0.85
updateMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected