Worksheet functions registered using xl_func
can format their results using a
Formatter
.
To specify what formatter should be used for a function use the formatter
kwarg
to the xl_func
decorator. For example:
from pyxll import xl_func, Formatter
import datetime as dt
date_formatter = Formatter(number_format="yyyy-mm-dd")
@xl_func(formatter=date_formatter)
def get_date():
return dt.date.today()
When the function is called from Excel, any previous formatting is cleared and the formatter is applied to the cell.
The standard Formatter
handles many common formatting requirements and takes the following options:
Formatter kwargs | |
---|---|
interior_color | Color value to set the interior color to. |
text_color | Color value to set the text color to. |
bold | If True, set the text style to bold. |
italic | If True, set the text style to italic. |
font_size | Value to set the font size to. |
number_format | Excel number format to use. |
auto_fit | Auto-fit to the content of the cells. May be any of: True (fit column width); False (don’t fit); “columns” (fit column width); “rows” (fit row width); “both” (fit column and row width); |
Color values can be obtained using the static method Formatter.rgb
.
More complex formatting can be done using a custom formatter.
The Formatter
clears all formatting before applying the new formatting, but you can also control
how the formatting is cleared using a custom formatter.
Note
When formatting is applied to Dynamic Array functions PyXLL will keep track of the current array size and save it in the Workbook Metadata.
This is so the previous range can be cleared before re-applying formatting. Without doing this the formatting would remain if the array contracted.