Gets the Excel Application COM object and returns it as a win32com.Dispach,
comtypes.POINTER(IUknown), pythoncom.PyIUnknown or xlwings.App depending on which COM package
is being used.
com_package (string) – The Python package to use when returning the COM object.
It should be None, ‘win32com’, ‘comtypes’, ‘pythoncom’ or ‘xlwings’.
If None the com package set in the configuration file will be used,
or ‘win32com’ if nothing is set.
Calls a callable object (e.g. a function) in a background thread.
This can be useful when calling back into Excel (e.g. updating a cell value) from a worksheet
When using this function from a worksheet function care must be taken to ensure that
an infinite loop doesn’t occur (e.g. if it writes to a cell that’s an input to the function, which
would cause the function to be called again and again locking up Excel).
Note that and Excel COM objects created in the main thread should not be used in the background thread and
doing so may cause Excel to crash.
callable – Callable object to call from a background thread.
args – Arguments to pass to the callable object.
kwargs – Keyword arguments to pass to the callable object.
frompyxllimportxl_func,xl_app,xlfCaller,async_call@xl_func(macro=True)defset_values(rows,cols,value):"""copies `value` to a range of rows x cols below the calling cell"""# get the address of the calling cellcaller=xlfCaller()address=caller.address# the update is done asynchronously so as not to block Excel# by updating the worksheet from a worksheet functiondefupdate_func():xl=xl_app()xl_range=xl.Range(address)# get the cell below and expand it to rows x colsxl_range=xl.Range(range.Resize(2,1),range.Resize(rows+1,cols))# and set the range's valuexl_range.Value=value# kick off the asynchronous call the update functionpyxll.async_call(update_func)returnaddress
When a Python function is called from an Excel worksheet, if an uncaught exception is raised PyXLL
caches the exception and traceback as well as logging it to the log file.
The last exception raised while evaluating a cell can be retrieved using this function.
The cache used by PyXLL to store thrown exceptions is limited to a maximum size, and so if there are
more cells with errors than the cache size the least recently thrown exceptions are discarded. The
cache size may be set via the error_cache_size setting in the config.
When a cell returns a value and no exception is thrown any previous error is not discarded. This
is because doing so would add additional performance overhead to every function call.
xl_cell – An XLCell instance or a COM Range object (the exact type depends
on the com_package setting in the config.
The last exception raised by a Python function evaluated in the cell, as a tuple
(type, value, traceback).
frompyxllimportxl_func,xl_menu,xl_version,get_last_errorimporttraceback@xl_func("xl_cell: string")defpython_error(cell):"""Call with a cell reference to get the last Python error"""exc_type,exc_value,exc_traceback=pyxll.get_last_error(cell)ifexc_typeisNone:return"No error"return"".join(traceback.format_exception_only(exc_type,exc_value))@xl_menu("Show last error")defshow_last_error():"""Select a cell and then use this menu item to see the last error"""selection=xl_app().Selectionexc_type,exc_value,exc_traceback=get_last_error(selection)ifexc_typeisNone:xlcAlert("No error found for the selected cell")returnmsg="".join(traceback.format_exception(exc_type,exc_value,exc_traceback))ifxl_version()<12:msg=msg[:254]xlcAlert(msg)
Returns a function to convert objects of type src_type to dest_type.
Even if there is no function registered that converts exactly from src_type to dest_type, as long
as there is a way to convert from src_type to dest_type using one or more intermediate types this
function will create a function to do that.