PyQt PyXLL Jupyter macros Feb 2025


Excel macros — using PyXLL and Jupyter
I illustrate how to use PyXLL and Jupyter to execute macros — simple macros and very sophisticated macros (using PyQt).
Here’s my video

Explanation

I presume everyone reading this has written an Excel VBA macro (subroutine). It’s easy to code a VBA macro.

In Part 1 of this video, the VBA macro gets the value of the selected cell and then presents that value in a message box. Nothing serious at all, just an example.

In Part 2 of the video, I invoke a PyXLL Jupyter pane (CTP), then open a Jupyter notebook.

The notebook has two PyXLL macros. The first executes a rather trivial 'Hello' example. The second executes a PyQt dialog with embedded calculations. These are bona fide structural engineering calculations which provide ASCE (building code) wind loadings on structural members. There is quite a lot of interface and code associated with that interface.

  • usual input/output
  • tabbed output
  • filtering of dropdown
  • tabular output of all shape properties
  • tabular output of all wind calculations
  • output to the windows clipboard (cut and paste)
  • drawing cross sections to scale
  • user specified elevation datum
  • X or Y axis wind direction
  • fireproofing

After running the Jupyter notebook cells, I assign the PyXLL macros to Excel objects: 'popup_messagebox2' is assigned to the shape (Ritchie) and 'show_trial_pyside2X' is assigned to the button (show pyqt).

Summary

The two aspects of interest are:

  • We can easily build Python macros and use them in Excel
  • After the Jupyter pane is dismissed, the macros remain assigned to the objects

As I usually point out, PyXLL code is not written or saved in the Excel worksheets; this means that the code is universally avaiable to all of a user’s worksheets.

Coding
VBA Code

Sub SomeVBASubroutine()
    x = Selection.Value
    MsgBox "Value of selected cell is: " & x
End Sub
Jupyter Code

####################################################################
# 1st cell
#
from pyxll import xl_macro, xlcAlert


@xl_macro
def popup_messagebox2():
    xlcAlert("Hello")
	
	
####################################################################
# 2nd cell
#
import sys

sys.path.append('E:\my_pyxll_modules')

import trial_pyqt_des_exp_wind_2a

from PySide2 import QtWidgets


_qt_app = None

def get_qt_app():
    """Returns a QApplication instance.
    Must be called before showing any dialogs.
    """

    global _qt_app
    if _qt_app is None:

        if not QtWidgets.QApplication.instance():
            app = QtWidgets.QApplication(sys.argv)
        else:
            app = QtWidgets.QApplication.instance()

    return _qt_app

@xl_macro()
def show_trial_pyside2X():
    _qt_app = None
    app = None

    app = get_qt_app()

    # Create the custom form
    dlg = trial_pyqt_des_exp_wind_2a.FormTEW()
    dlg.exec_()

Leave a Reply

Your email address will not be published. Required fields are marked *