A reusable component for running Excel analyses from OpenMDAO. It uses xlwings
to communicate with Excel and works through the following procedure:
- Start Excel.
- Open a pre-defined workbook file.
- Optional: Run pre-calculation macros.
- Set the input variables in corresponding ranges/cells.
- Optional: Run main macros.
- Read values of the output ranges/cells and assign to output variables.
- Optional: Run post-calculation macros.
- Close the workbook without saving it.
Features:
- Cells can be addressed by named ranges (use them!) or regular means (e.g.
C3
). - Timeout handling by mercilessly killing the Excel process after a specified number of seconds.
- The Excel process will be kept alive between evaluations, unless the previous one timed out.
- The macro runner will try to catch and log macro errors, raising an OpenMDAO
AnalysisError
. - Rudimentary logging using the Python logging system.
Non-features/pitfalls/known issues:
- "Trust access to the VBA project object model" must be enabled.
- MacOS has not been tested and will probably not work.
- Non-scalar values (i.e. ranges with more than one cell) have not been tested and will probably not work.
- Multi-processing (i.e. MPI) has not been tested, but might work.
- Excel has a few numeric quirks and you are encouraged to be very critical of your results. I personally don't even recommend using Excel in this context, unless you really have to.
- The timeout handler runs in a separate Python thread. This shouldn't be a problem, but you might want to know.
The component is regularly-ish tested on the latest stable versions of:
- Windows 10, 64-bit
- Microsoft Excel, 32-bit
- Python 3
- OpenMDAO
I wish I could set up a CI pipeline to make this process more transparent and repeatable, but Excel being a proprietary product makes this more or less impossible.
pip install git+https://github.com/ovidner/openmdao-bridge-excel.git#egg=openmdao_bridge_excel
import openmdao.api as om
from openmdao_bridge_excel import ExcelComponent, ExcelVar
prob = om.Problem()
...
excel_comp = prob.add_subsystem("excel", ExcelComponent(
file_path="absolute/or/relative/path/to/file.xlsm",
pre_macros=["foo", "bar"],
main_macros=["main", "foo"],
post_macros=["cleanup"],
inputs=[
ExcelVar("in_1", "Sheet1!C2"),
ExcelVar("in_2", "NamedRangesAreSupported"),
],
outputs=[
ExcelVar("out_1", "B1"),
ExcelVar("out_2", "AnotherNamedRange"),
],
timeout=60, # One minute
))
...
Input/output variables can then be addressed on the OpenMDAO component as usual (e.g. excel.in_1
, excel.out_2
).
This is automagically handled with anaconda-project
:
anaconda-project run setup
You should then be able to run the tests:
anaconda-project run pytest