PyAddin is an Excel addin template with customized Ribbon menu and a combination of VBA and Python. VBA calls Python script by console arguments, and gets return from running results. So it could be easily extended to your own application.
Shown with VBA and Driven by Python
Python3, argparse, pywin32
Navigate to root directoty and install this package:
python setup.py install
Or install it in developing mode:
python setup.py develop
Uninstall pyaddin via pip:
pip uninstall pyaddin
# initialize project
pyaddin init
# create Excel Addin
pyaddin create --name my_first_addin
# update Excel Addin
pyaddin update --name my_first_addin
- Initialize project
D:\GitHub\PyAddin>mkdir examples
D:\GitHub\PyAddin>cd examples
D:\GitHub\PyAddin\examples>pyaddin init
- Customize Ribbon Tab
Check CustomUI.xml created automatically in Step 1 and define UI structures in pre-defined format.
<!--
Add custom UI definition between <tabs> and </tabs>, e.g.
<tab id="userRibbon" label="PyAddin">
<group id="group_about" label="About">
<button id="about" imageMso="About" size="large" label="About" onAction="callback_about"/>
</group>
</tab>
Please refer to the link below for detail:
https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2007/aa338202(v%3doffice.12)
-->
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
<ribbon startFromScratch="false">
<tabs>
</tabs>
</ribbon>
</customUI>- Create/Update Addin
D:\GitHub\PyAddin\examples>pyaddin create --name my_first_addin
The main addin file my_first_addin.xlam, as well as some dependent files are created under examples:
main.pyinterface for VBA functionRunPython()to call user defined Python scriptsmain.cfgconfiguration for this addin template, including Python interpreter pathscriptsdefault package for user defined Python scripts
If you want to add more features to your addin later, update CustomUI.xml first and run command below to update both the ribbon tab and callback definitions.
D:\GitHub\PyAddin\examples>pyaddin update --name my_first_addin
- Develop Your Addin
- Setting path for Python interpreter
Check main.cfg and set Python path. If embeddable Python is applied, a relative path would be recommended.
# common line starts with #
# set path for python interpreter
# relative path is allowable
[python]
\python\python.exe
# default folder name for outputs
[output]
outputs
# standard output/error files name under [output]
[stdout]
output.log
[stderr]
errors.log
- Fill VBA callback functions
Sub callback_cal(control As IRibbonControl
Dim a1$, a2$, args, res$
a1 = ActiveSheet.Range("A1").Value
a2 = ActiveSheet.Range("A2").Value
args = Array(a1, a2)
If RunPython("scripts.test.division", args, res) Then
ActiveSheet.Range("A3").Value = res
Else
MsgBox res
End If
End SubRunPython is a pre-defined VBA function to call Python scripts from command line, and check return from output/error file generated by the called Python script. The first argument, "scripts.test.division" in this case, refers to the called method.
- Create Python script under
scriptsto do the main work
# scripts/test.py
def division(a, b):
assert a!='', 'cell A1 is empty'
assert b!='', 'cell A2 is empty'
return float(a)/float(b)