The following functions and decorators provide access to PyXLL’s type conversion system.
When registering Python functions as Excel worksheet functions using xl_func
, or as Excel macros
using xl_macro
, type information can be supplied that informs the PyXLL add-in how to convert
between Python and Excel values.
Type information can be provided to PyXLL in a number of different ways, include Python type hints, a
function signature string, and using the xl_arg
and xl_return
decorators documented
below.
Custom type conversion methods can be registered using the xl_arg_type
and xl_return_type
decorators.
To access PyXLL’s type converters (including any custom type converters you have registered) you can use the
get_type_converter
function.
See Argument and Return Types for more details about PyXLL’s type conversion features.
get_type_converter
(src_type, dest_type [, src_kwargs=None] [, dest_kwargs=None])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.
Parameters: |
|
---|---|
Returns: | Function to convert from src_type to dest_type. |
Example usage:
from pyxll import xl_func, get_type_converter
@xl_func("var x: var")
def py_function(x):
# if x is a number, convert it to a date
if isinstance(x, float):
to_date = get_type_converter("var", "date")
x = to_date(x)
return "%s : %s" % (x, type(x))
xl_arg
(_name[, _type][, _label][, _description][, **kwargs])Decorator for providing type information for a function argument.
This can be used instead of providing a function signature to xl_func
and xl_macro
.
Parameters: |
|
---|
xl_return
([_type=None] [, **kwargs])Decorator for providing type information for a function’s return value.
This can be used instead of providing a function signature to xl_func
and xl_macro
.
Parameters: |
|
---|
xl_arg_type
(name, base_type [, allow_arrays=True] [, macro=None] [, thread_safe=None] [, typing_type=None])Returns a decorator for registering a function for converting from a base type to a custom type.
Parameters: |
|
---|
xl_return_type
(name, base_type [, allow_arrays=True] [, macro=None] [, thread_safe=None] [, typing_type=None])Returns a decorator for registering a function for converting from a custom type to a base type.
Parameters: |
|
---|