xlOil Python Excel.Application object

The Excel.Application object is the root of Excel’s COM interface. If you have used VBA you will likely have come accross it. In xlOil, you can get a reference to this object with xloil.app(). From there the comtypes or pywin32 libraries provides syntax similar to VBA to call methods on the object.

The available methods are documented extensively at Excel object model overview and Application Object

COM support can be provided by ‘comtypes’, a newer pure python package or ‘win32com’ (also called pywin32) a well-established more C++ based library. You can pass a lib argument to xloil.app or the to_com method. If omitted, the default is ‘comtypes’, unless changed in the XLL’s ini file.

Application.Run

In VBA this takes a function name and a variable argument list and attempts to call the specified function dynamically. In xlOil, use xloil.excel_func and xloil.excel_func_async.

Examples

We lift some examples directly from the comtypes help

xl = xlo.app()

# Accessing a range with empty index
X = xl.Range["A1", "C1"].Value[:]
# X now contains a tuple like (10, "20", 31.4)

# Alternative syntax, gives Y == X
Y = xl.Range["A1", "C1"].Value[()]

# Writing to a range uses the same syntax
xl.Range["A1", "C1"].Value[:] = (3, 2, 1)
xl.Range["A1", "C1"].Value[()] = (1, 2, 3)

# Looks very similar but uses the xlOil range object so has slightly
# different syntax. We're calling the *Range* constructor so we use
# round brackets.
xlo.Range("A1:C1").value = (1, 2, 3)

Troubleshooting

https://stackoverflow.com/questions/52889704/python-win32com-excel-com-model-started-generating-errors