You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-`Your Group 1`是一个示例分组,其中设计了两个按钮。实际开发中请替换为需要的控件,或者增加其他更多分组。
114
+
115
+
关于 Ribbon 界面的具体介绍及格式规范,参考以下链接。
116
+
117
+
-[General Format of XML Markup Files](https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2007/aa338202(v%3doffice.12)#general-format-of-xml-markup-files)
Copy file name to clipboardExpand all lines: readme.md
+45-39Lines changed: 45 additions & 39 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,18 @@
1
+
[中文](./README_CN.md) | English
2
+
1
3
# PyAddin
2
4
3
-
VBA is not as popular as it used to be, while Excel is still widely used for holding, processing and reviewing data. Generally, an Excel file with macro (xlsm) is filled with data and embedded with complicated VBA scripts. The idea of this project is to split such Excel file into two separate parts: worksheets without macro (xlsx) as data container, and an Excel add-in (xlam) as computing engine.
VBA is not as popular as it used to be, while Excel is still widely used for holding, processing and reviewing data. Generally, an Excel file with macro (xlsm) is filled with data and embedded with VBA scripts for data processing. When the data processing scripts need to be reused, e.g., applying to monthly data, it's more reasonable to split such Excel file into two separate parts: worksheets without macro (xlsx) as data container, and an Excel add-in (xlam) as computing engine.
4
9
5
10
`PyAddin` is a command-line tool facilitating the creation of Excel add-in (*.xlam), with ability to process data with Python.
6
11
7
12
- provides an Excel add-in template with customized Ribbon menu and a combination of VBA and Python.
8
13
- helps to update the Ribbon UI during the development of your add-in.
9
14
10
-
The main idea is VBA macro calling Python script by console arguments, then retrieving return value stored in a temporary file as a bridge. In addition, leverage the ability of `pywin32`, all the interaction with Excel, e.g., reading/writing cell data, setting cell format, could be completed in Python script.
15
+
The main idea is VBA macro calling Python script by console arguments, then retrieving return value stored in a temporary file as a bridge. In addition, leveraging the ability of `pywin32`, all the interaction with Excel, e.g., reading/writing data, setting format, could be completed in Python script.
11
16
12
17
13
18
## Limitation
@@ -41,28 +46,20 @@ pip uninstall pyaddin
41
46
42
47
## Usage
43
48
44
-
```
45
-
> pyaddin -h
46
-
47
-
NAME
48
-
pyaddin - Command line interface for ``PyAddin``.
49
-
50
-
SYNOPSIS
51
-
pyaddin COMMAND | -
52
-
53
-
DESCRIPTION
54
-
Command line interface for ``PyAddin``.
49
+
- Create template project with specified name
55
50
56
-
COMMANDS
57
-
COMMAND is one of the following:
51
+
```
52
+
pyaddin init --name=xxx --quiet=True|False
53
+
```
58
54
59
-
init
60
-
Create template project with specified name under current path.
55
+
- Update add-in based on ribbon UI file
61
56
62
-
update
63
-
Update add-in file (name.xlam) based on ribbon UI file (CustomUI.xml) under working path.
57
+
```
58
+
pyaddin update --name=xxx --quiet=True|False
64
59
```
65
60
61
+
`quiet` is an optional parameter. Run the process in background mode if `True`, i.e., do not launch Excel application explicitly. Default to `True`.
62
+
66
63
## Tutorial
67
64
68
65
### 1. Initialize template project
@@ -91,32 +88,33 @@ sample\
91
88
-`main.cfg` is the configuration file with parameters like Python interpreter path.
92
89
-`scripts` is the default package for user defined Python scripts, e.g., `sample.py` is the first example.
93
90
-`CustomUI.xml` defines the Ribbon UI of this template add-in.
94
-
-`sample.xlam` is a template add-in, see screenshot below.
91
+
-`sample.xlam` is a template add-in, which is the base line to extend your new features.
95
92
93
+
See screenshot of this template add-in below.
96
94
97
95

98
96
99
97
100
98
101
99
### 2. Customize Ribbon Tab
102
100
103
-
Check [`CustomUI.xml`](./pyaddin/resources/CustomUI.xml) created in Step 1, then define UI structures as needed. In this template case, two groups are defined:
101
+
Check [`CustomUI.xml`](./pyaddin/resources/CustomUI.xml) created in Step 1, then define your UI structures as needed. After that, run command below to apply to your add-in. Alternatively, you can do it by unzipping and updating the add-in manually, or with other Custom UI editor.
102
+
103
+
```
104
+
D:\WorkSpace\sample>pyaddin update --name=sample
105
+
```
106
+
107
+
In this template case, two groups are defined:
104
108
105
109
-`setting` group provides supporting features. It should be reused in your add-in.
106
110
-`Your Group 1` is an example for quick start, here two buttons are defined. Feel free to delete or modify it for your own purpose.
107
111
108
-
For detailed information on Microsoft Ribbon UI, please refer to links below.
112
+
For detailed information on Microsoft Ribbon UI, please refer to links below.
109
113
110
114
-[General Format of XML Markup Files](https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2007/aa338202(v%3doffice.12)#general-format-of-xml-markup-files)
After finishing your feature design in `CustomUI.xml`, run command below to apply to the add-in. Alternatively, you can do it by unzipping and updating the add-in file manually, or with other Custom UI editor.
115
-
116
-
```
117
-
D:\WorkSpace\sample>pyaddin update --name=sample
118
-
```
119
-
120
118
121
119
### 3. Implement Ribbon Control Actions
122
120
@@ -129,16 +127,7 @@ Actions defined in `CustomUI.xml` should be implemented in VBA module. For examp
129
127
...>
130
128
```
131
129
132
-
Check the following sample codes from VBA module `UserRibbon` of `sample.xlam`. `RunPython` is a pre-defined VBA function to call Python scripts, and get return value.
133
-
134
-
- The first argument is the path to python method, `package.module.method`. For example, "scripts.sample.run_example_1" refers to `run_example_1` from python script `sample/scripts/sample.py`.
135
-
136
-
- The second, third, ..., arguments refer to any count of parameters passing to python. Note that they **MUST** be simple type like string and number, and are converted to string type when passing to python with command line.
137
-
138
-
- The return value is `Dictionary` type with two keys:
139
-
-`status`: True or False, indicates the status of calling python script.
140
-
-`value`: the return value, or error message depends on `status`.
141
-
130
+
Check the following sample codes from VBA module `UserRibbon` of `sample.xlam`.
142
131
143
132
```vb
144
133
SubCB_Sample_1(controlAsIRibbonControl)
@@ -158,6 +147,23 @@ End Sub
158
147
```
159
148
160
149
150
+
`RunPython()` is a pre-defined VBA function to call Python scripts, and get return value. The function signature:
- The first argument is the path to python method, `package.module.method`. For example, "scripts.sample.run_example_1" refers to `run_example_1` from python script `sample/scripts/sample.py`.
157
+
158
+
- The second argument accepts any count of parameters passing to python. Note that they **MUST** be simple type like string and number, and are converted to string type when passing to python with command line.
159
+
160
+
- The return value is `Dictionary` type with two keys:
161
+
-`status`: True or False, indicates the status of calling python script.
162
+
-`value`: the return value, or error message depends on `status`.
163
+
164
+
165
+
166
+
161
167
### 4. Implement Python Script
162
168
163
169
Based on the calling from `RunPython`, create module file and implement associated methods.
@@ -182,7 +188,7 @@ def run_example_2():
182
188
sheet.Range('A3').Value = x + y
183
189
```
184
190
185
-
The two sample methods above did same thing: add values in cell `A1` and `A2`, then fill to cell `A3`, but in two different direction.
191
+
The two sample methods above did same thing: add values in cell `A1` and `A2`, then fill to cell `A3`, but in two different directions.
186
192
187
193
-`run_example_1` performs the pure computation with parameters received from VBA, then passes back to VBA for further processing, e.g., fill in `A3`. It applies for simle case with small amount of data.
0 commit comments