Quantitative Analysis
Parallel Processing
Numerical Analysis
C++ Multithreading
Python for Excel
Python Utilities

I. Motivation and design (PFE).
II. Installation of the Python for Excel.
III. Tutorial introduction into the Python for Excel.
1. Stability guarantee.
2. PythonForExcel ControlShell.
3. What to do if the connection to Excel from the ControlShell is lost or if the prompt "waits" for too long.
4. Possible littering of memory with orphaned Excel processes.
5. In-cell execution of Python statements.
6. Calling Python from VBA.
7. Calling VBA from Python.
8. Debugging with Python for Excel.
9. Unicode support.
10. Deactivation of Python for Excel.
11. Very important note about pfe-script execution.
IV. PFE Programmer's reference.
Index. Contents.

PythonForExcel ControlShell.

he ControlShell pops up if any output is directed into the default value of sys.stderr provided by the Python for Excel or when the user presses one of the key combinations<Alt>+<Shift>+<P>, <Alt>+<Shift>+<Q>, <Alt>+<Shift>+<Z> from the Excel application. One of these key combinations may not work if the version of Excel reserves such combination for something else.


The picture above describes the starting look of the ControlShell. The prompt is the python shell that controls functionality of the window itself. Such shell is called "control shell" below. The control shell contains the object "theShell" in its address space. Such object provides access to the entire object model of the ControlShell. For example, theShell.saveSettings() would save the current position and size of the window. You may discover ways to modify the functionality by examining the py files in the "scripts" directory of the PythonForExcel installation.

The Excel instance may be controlled by switching to a different shell. Press <Ctrl>+<Enter> and the different prompt would appear.


This is what we call "addin shell". It provides control of the Excel application. In particular, it has Application, ExcelListener objects and vba module. These objects may be explored by regular python means but there is also autocompletion and call tips.


Everything has the same name as it has in the VBA for Excel. Hence, another way to explore would be to record a regular VBA for Excel macro and look at its code.

The ExcelListener object has no equivalent in VBA. It has "On*" attribute for every event that Excel fires.


Every such attribute has a "bind" method that accepts an event handler. Several handlers may be bound to any event. The expected signature of the event handler may be obtained by calling the "signature" method.


The ControlShell may be used to control several Excel instances. A connection to ControlShell and access to the addin shell is gained by pressing one of the key combinations <Alt>+<Shift>+<P>, <Alt>+<Shift>+<Q>, <Alt>+<Shift>+<Z> from Excel and (repeated) <Ctrl>+<Enter> from the ControlShell. Disconnection of Excel from ControlShell is accomplished by pressing <Ctrl>+<Delete> at the addin shell in the ControlShell window.

The ControlShell is a multithreaded application. It blocks only when the control shell ">>>" runs. It does not block when any or all addin shells run. Full functionality is retained while output is received from addin shells.

When a command is entered at the addin prompt, the PFE obtains control of the corresponding Excel instance, executes the command in-process on the main thread of Excel and releases the control of Excel after the python statement has returned control. For example, if sys.stdin.readline() is typed then the Excel instance (but not ControlShell) blocks


until the command is completed.


Similarly, if "execfile"-statement is executed then the script supplied by the "execfile"-statement would retain continuous control of the Excel instance until it returns.

Entering multiline commands is possible.


The shell automatically positions the cursor where the input should begin with proper identation. The identation level is increased by ending the line with the colon and decreased by pressing <Enter> at an empty line.

Previous commands are accessible with <Ctrl>+<Up> and <Ctrl>+<Down>. Copying, pasting and cutting are context sensitive. There is a context menu at right click. <Ctrl>+<Num+> and <Ctrl>+<Num-> control the size of the font. Pressing <Ctrl>+<SpaceBar> restores calltip if closed.

The ControlShell retains its address space while it is closed but loses it if the computer is restarted.

Index. Contents.

Copyright 2007