Thanks to visit codestin.com
Credit goes to github.com

Skip to content

An Excel addin combining with VBA and Python, shown with VBA and driven by Python.

Notifications You must be signed in to change notification settings

huiguo2008/PyAddin

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PyAddin

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


Requirements

Python3, argparse, pywin32

Install and Uninstall

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

Usage

# initialize project
pyaddin init

# create Excel Addin
pyaddin create --name my_first_addin

# update Excel Addin
pyaddin update --name my_first_addin

Example - Create Addin Template

  1. Initialize project
D:\GitHub\PyAddin>mkdir examples
D:\GitHub\PyAddin>cd examples
D:\GitHub\PyAddin\examples>pyaddin init
  1. 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>
  1. 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.py interface for VBA function RunPython() to call user defined Python scripts
  • main.cfg configuration for this addin template, including Python interpreter path
  • scripts default 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
  1. 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 Sub

RunPython 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 scripts to 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)

About

An Excel addin combining with VBA and Python, shown with VBA and driven by Python.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 75.8%
  • Visual Basic .NET 24.2%