Skip to content

Commit 56a384c

Browse files
committed
updated docs for v0.3.0
1 parent de808ce commit 56a384c

9 files changed

+108
-44
lines changed

README.rst

+10-9
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ This essentially hands over control to ``mymodule.py``:
8080
8181
def rand_numbers():
8282
""" produces standard normally distributed random numbers with shape (n,n)"""
83-
wb = Workbook() # Creates a reference to the calling Excel file
83+
wb = Workbook.caller() # Creates a reference to the calling Excel file
8484
n = Range('Sheet1', 'B1').value # Write desired dimensions into Cell B1
8585
rand_num = np.random.randn(n, n)
8686
Range('Sheet1', 'C3').value = rand_num
@@ -121,16 +121,17 @@ Dependencies
121121

122122
* **Windows**: ``pywin32``
123123

124-
* **Mac**: ``psutil``, ``appscript``
124+
On Windows, it is recommended to use one of the scientific Python distributions like
125+
`Anaconda <https://store.continuum.io/cshop/anaconda/>`_,
126+
`WinPython <https://winpython.github.io/>`_ or
127+
`Canopy <https://www.enthought.com/products/canopy/>`_ as they already include pywin32. Otherwise it needs to be
128+
installed from `here <http://sourceforge.net/projects/pywin32/files/pywin32/>`_.
125129

126-
On Windows, it is recommended to use one of the scientific Python distributions like
127-
`Anaconda <https://store.continuum.io/cshop/anaconda/>`_,
128-
`WinPython <https://winpython.github.io/>`_ or
129-
`Canopy <https://www.enthought.com/products/canopy/>`_ as they already include pywin32. Otherwise it needs to be
130-
installed from `here <http://sourceforge.net/projects/pywin32/files/pywin32/>`_.
130+
* **Mac**: ``psutil``, ``appscript``
131131

132-
.. note:: On Mac, the dependencies are automatically being handled if xlwings is installed with ``pip``. However,
133-
the Xcode command line tools need to be available. Mac OS X 10.4 (*Tiger*) or later is required.
132+
On Mac, the dependencies are automatically being handled if xlwings is installed with ``pip``. However,
133+
the Xcode command line tools need to be available. Mac OS X 10.4 (*Tiger*) or later is required.
134+
The recommended Python distribution for Mac is `Anaconda <https://store.continuum.io/cshop/anaconda/>`_.
134135

135136
Optional Dependencies
136137
---------------------

docs/debugging.rst

+5-3
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ Consider the following code structure of your Python source code:
2222
import os
2323
from xlwings import Workbook, Range
2424
25-
def my_macro(workbook_path=None):
26-
wb = Workbook(workbook_path)
25+
def my_macro(wb=None):
26+
if wb is None:
27+
wb = Workbook.caller()
2728
Range('A1').value = 1
2829
2930
if __name__ == '__main__':
3031
# To run from Python, not needed when called from Excel.
3132
# Expects the Excel file next to this source file, adjust accordingly.
3233
path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'myfile.xlsm'))
33-
my_macro(path)
34+
wb = Workbook(path)
35+
my_macro(wb)
3436
3537
3638
``my_macro()`` can now easily be run from Python for debugging and from Excel for testing without having to change the

docs/images/udf_example.png

9.59 KB
Loading

docs/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ xlwings fully supports NumPy arrays and Pandas DataFrames. It works with Microso
2323
datastructures
2424
vba
2525
debugging
26+
udfs
2627
api
2728

2829

docs/installation.rst

+9-8
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@ Dependencies
1919

2020
* **Windows**: ``pywin32``
2121

22-
* **Mac**: ``psutil``, ``appscript``
22+
On Windows, it is recommended to use one of the scientific Python distributions like
23+
`Anaconda <https://store.continuum.io/cshop/anaconda/>`_,
24+
`WinPython <https://winpython.github.io/>`_ or
25+
`Canopy <https://www.enthought.com/products/canopy/>`_ as they already include pywin32. Otherwise it needs to be
26+
installed from `here <http://sourceforge.net/projects/pywin32/files/pywin32/>`_.
2327

24-
On Windows, it is recommended to use one of the scientific Python distributions like
25-
`Anaconda <https://store.continuum.io/cshop/anaconda/>`_,
26-
`WinPython <https://winpython.github.io/>`_ or
27-
`Canopy <https://www.enthought.com/products/canopy/>`_ as they already include pywin32. Otherwise it needs to be
28-
installed from `here <http://sourceforge.net/projects/pywin32/files/pywin32/>`_.
28+
* **Mac**: ``psutil``, ``appscript``
2929

30-
.. note:: On Mac, the dependencies are automatically being handled if xlwings is installed with ``pip``. However,
31-
the Xcode command line tools need to be available. Mac OS X 10.4 (*Tiger*) or later is required.
30+
On Mac, the dependencies are automatically being handled if xlwings is installed with ``pip``. However,
31+
the Xcode command line tools need to be available. Mac OS X 10.4 (*Tiger*) or later is required.
32+
The recommended Python distribution for Mac is `Anaconda <https://store.continuum.io/cshop/anaconda/>`_.
3233

3334
Optional Dependencies
3435
---------------------

docs/quickstart.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ This essentially hands over control to ``mymodule.py``:
7171
7272
def rand_numbers():
7373
""" produces standard normally distributed random numbers with shape (n,n)"""
74-
wb = Workbook() # Creates a reference to the calling Excel file
74+
wb = Workbook.caller() # Creates a reference to the calling Excel file
7575
n = Range('Sheet1', 'B1').value # Write desired dimensions into Cell B1
7676
rand_num = np.random.randn(n, n)
7777
Range('Sheet1', 'C3').value = rand_num
@@ -82,7 +82,7 @@ then go to ``File > Import File...`` and import the ``xlwings.bas`` file. ). It
8282
your ``xlwings`` installation.
8383

8484
.. note:: Always instantiate the ``Workbook`` within the function that is called from Excel and not outside as global
85-
variable. Older versions of the docs/samples were showing the wrong approach.
85+
variable.
8686

8787
For further details, see :ref:`vba`.
8888

docs/udfs.rst

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
.. _udfs:
2+
3+
User Defined Functions (UDFs)
4+
=============================
5+
6+
.. note:: This feature is currently experimental and only available on Windows. It is the first step of an integration
7+
with `ExcelPython <http://ericremoreynolds.github.io/excelpython//>`_. Currently, there is no support for
8+
ExcelPython's decorator functionality and add-in. Also, ExcelPython's config file is obsolete as settings are being
9+
handled in the xlwings VBA module.
10+
11+
12+
In it's current implementation, the UDF functionality is most useful to get access to existing Python code like for example
13+
NumPy's powerful set of functions. This is best explained in a simple example: To expose NumPy's
14+
`pseudo-inverse <http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.pinv.html>`_, you would write the
15+
following VBA code:
16+
17+
.. code-block:: vb.net
18+
19+
Public Function pinv(x As Range)
20+
On Error GoTo Fail:
21+
Set numpy_array = Py.GetAttr(Py.Module("numpy"), "array")
22+
Set pseudo_inv = Py.GetAttr(Py.GetAttr(Py.Module("numpy"), "linalg"), "pinv")
23+
Set x_array = Py.Call(numpy_array, Py.Tuple(x.Value))
24+
Set result_array = Py.Call(pseudo_inv, Py.Tuple(x_array))
25+
Set result_list = Py.Call(result_array, "tolist")
26+
pinv = Py.Var(result_list)
27+
Exit Function
28+
Fail:
29+
pinv = Err.Description
30+
End Function
31+
32+
This then enables you to use ``pinv()`` as array function from Excel cells:
33+
34+
.. figure:: images/udf_example.png
35+
36+
1. Fill a spreadsheet with the following numbers as shown on the image:
37+
38+
``A1``: 1, ``A2``: 2, ``B1``: 2, ``B2``: 4
39+
40+
2. Select cells ``D1:E2``.
41+
42+
3. Type ``pinv(A1:B2)`` into D1 while D1:E2 are still selected.
43+
44+
4. Hit ``Ctrl-Shift-Enter`` to enter the array formula. If done right, the formula bar will automatically
45+
wrap the formula with curly braces ``{}``.
46+
47+
Further documentation
48+
---------------------
49+
50+
For more in depth documentation at this point in time, please refer directly to the
51+
`ExcelPython <http://ericremoreynolds.github.io/excelpython//>`_ project, mainly the following docs:
52+
53+
* `A very simple usage example <https://github.com/ericremoreynolds/excelpython/blob/master/docs/tutorials/Usage01.md>`_
54+
* `A more practical use of ExcelPython <https://github.com/ericremoreynolds/excelpython/blob/master/docs/tutorials/Usage02.md>`_
55+
* `Putting it all together <https://github.com/ericremoreynolds/excelpython/blob/master/docs/tutorials/Usage03.md>`_
56+
* `Ranges, lists and SAFEARRAYs <https://github.com/ericremoreynolds/excelpython/blob/master/docs/tutorials/Usage04.md>`_

docs/vba.rst

+16-6
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,35 @@ While the defaults will often work out-of-the box, you can change the settings a
2828
under ``Function Settings``::
2929

3030
PYTHON_WIN = ""
31-
PYTHON_MAC = GetMacDir("Home") & "/anaconda/bin"
31+
PYTHON_MAC = ""
3232
PYTHON_FROZEN = ThisWorkbook.Path & "\build\exe.win32-2.7"
3333
PYTHONPATH = ThisWorkbook.Path
3434
LOG_FILE = ThisWorkbook.Path & "\xlwings_log.txt"
35+
SHOW_LOG = True
36+
OPTIMIZED_CONNECTION = False
3537

3638
* ``PYTHON_WIN``: This is the directory of the Python interpreter on Windows. ``""`` resolves to your default Python
3739
installation on the PATH, i.e. the one you can start by just typing ``python`` at a command prompt.
3840
* ``PYTHON_MAC``: This is the directory of the Python interpreter on Mac OSX. ``""`` resolves to your default
39-
installation on $PATH but **not** the one defined in .bash_profile! Since you most probably define your default Python
40-
installation in .bash_profile, you will have to adjust this value to match your installation. To get special folders
41+
installation as per PATH on .bash_profile. To get special folders
4142
on Mac, type ``GetMacDir("Name")`` where ``Name`` is one of the following: ``Home``, ``Desktop``, ``Applications``,
4243
``Documents``.
4344
* ``PYTHON_FROZEN`` [Optional]: Currently only on Windows, indicates the directory of the exe file that has been frozen
4445
by either using ``cx_Freeze`` or ``py2exe``. Can be set to ``""`` if unused.
4546
* ``PYTHONPATH`` [Optional]: If the source file of your code is not found, add the path here. Otherwise set it to ``""``.
4647
* ``LOG_FILE``: Directory **including** the file name. This file is necessary for error handling.
48+
* ``SHOW_LOG``: If False, no pop-up with the Log messages (usually errors) will be shown. Use with care.
49+
* ``OPTIMIZED_CONNECTION``: Currently only on Windows, use a COM Server for an efficient connection (experimental!)
4750

4851
.. note:: If the settings (especially ``PYTHONPATH`` and ``LOG_FILE``) need to work on Windows on Mac, use backslashes
4952
in relative file path, i.e. ``ThisWorkbook.Path & "\mydirectory"``.
5053

54+
.. note:: ``OPTIMIZED_CONNECTION = True`` works currently on **Windows only** and is still experimental! This will
55+
use a COM server that will keep the connection to Python alive between different calls and is therefore much more
56+
efficient. However, changes in the Python code are not being picked up until the ``pythonw.exe`` process is restarted
57+
by killing it manually in the Windows Task Manager. The suggested workflow is hence to set
58+
``OPTIMIZED_CONNECTION = False`` for development and to only set it to ``True`` for production.
59+
5160

5261
Subtle difference between the Windows and Mac Version
5362
-----------------------------------------------------
@@ -79,12 +88,13 @@ This essentially hands over control to ``mymodule.py``:
7988
8089
def rand_numbers():
8190
""" produces std. normally distributed random numbers with shape (n,n)"""
82-
wb = Workbook() # Creates a reference to the calling Excel file
91+
wb = Workbook.caller() # Creates a reference to the calling Excel file
8392
n = Range('Sheet1', 'B1').value # Write desired dimensions into Cell B1
8493
rand_num = np.random.randn(n, n)
8594
Range('Sheet1', 'C3').value = rand_num
8695
8796
You can then attach ``MyMacro`` to a button or run it directly in the VBA Editor by hitting ``F5``.
8897

89-
.. note:: Always instantiate the ``Workbook`` within the function that is called from Excel and not outside as
90-
module-wide global variable. Older versions of the docs/samples were showing the wrong approach.
98+
.. note:: Always place ``Workbook.caller()`` within the function that is called from Excel and not outside as
99+
module-wide global variable. Otherwise it doesn't get garbage collected with ``OPTIMIZED_CONNECTION = True``
100+
which prevents Excel from shutting down properly upon exiting and and leaves you with a zombie process.

docs/whatsnew.rst

+9-16
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ API changes
99

1010
* To reference the calling Workbook when running code from VBA, you now have to use ``Workbook.caller()``. This means
1111
that ``wb = Workbook()`` is now consistently creating a new Workbook, whether the code is called interactively or
12-
through VBA.
12+
from VBA.
1313

1414
============================== =========================
1515
**New** **Old**
@@ -26,7 +26,7 @@ This version adds two exciting but still **experimental** features from
2626
will keep the connection to Python alive between different calls and is therefore much more efficient. However,
2727
changes in the Python code are not being picked up until the ``pythonw.exe`` process is restarted by killing it
2828
manually in the Windows Task Manager. The suggested workflow is hence to set ``OPTIMIZED_CONNECTION = False`` for
29-
development and only set it to ``True`` for production - keep in mind though that this feature is stil experimental!
29+
development and only set it to ``True`` for production - keep in mind though that this feature is still experimental!
3030

3131
* User Defined Functions (UDFs): Using ExcelPython's wrapper syntax in VBA, you can expose Python functions as UDFs.
3232

@@ -36,18 +36,12 @@ isn't available through xlwings yet.
3636

3737
Further enhancements include:
3838

39-
* New method :meth:`xlwings.Range.resize` (:issue:`90`)
40-
41-
* New method :meth:`xlwings.Range.offset` (:issue:`89`)
42-
43-
* New property :attr:`xlwings.Range.shape` (:issue:`109`)
44-
45-
* New property :attr:`xlwings.Range.size` (:issue:`109`)
46-
47-
* New property :attr:`xlwings.Range.hyperlink` and new method :meth:`xlwings.Range.add_hyperlink` (:issue:`104`)
48-
49-
* New property :attr:`xlwings.Range.color` (:issue:`97`)
50-
39+
* New method :meth:`xlwings.Range.resize` (:issue:`90`).
40+
* New method :meth:`xlwings.Range.offset` (:issue:`89`).
41+
* New property :attr:`xlwings.Range.shape` (:issue:`109`).
42+
* New property :attr:`xlwings.Range.size` (:issue:`109`).
43+
* New property :attr:`xlwings.Range.hyperlink` and new method :meth:`xlwings.Range.add_hyperlink` (:issue:`104`).
44+
* New property :attr:`xlwings.Range.color` (:issue:`97`).
5145
* The ``len`` built-in function can now be used on ``Range`` (:issue:`109`):
5246

5347
>>> len(Range('A1:B5'))
@@ -60,8 +54,7 @@ Further enhancements include:
6054
cell.color = (255, 0, 0)
6155

6256
* [Mac version]: The VBA module finds now automatically the default Python installation as per ``PATH`` variable on
63-
``.bash_profile`` when ``PYTHON_MAC = ""`` (the default now in the VBA settings) (:issue:`95`).
64-
57+
``.bash_profile`` when ``PYTHON_MAC = ""`` (the default in the VBA settings) (:issue:`95`).
6558
* The VBA error pop-up can now be muted by setting ``SHOW_LOG = False`` in the VBA settings. To be used with
6659
care, but it can be useful on Mac, as the pop-up window is currently showing printed log messages even if no error
6760
occurred(:issue:`94`).

0 commit comments

Comments
 (0)