Converters and Options - Xlwings Dev Documentation PDF
Converters and Options - Xlwings Dev Documentation PDF
org
13-17 minutes
xlwings
Introduced with v0.7.0, converters define how Excel ranges and their values are converted both during
reading and writing operations. They also provide a consistent experience across xlwings.Range objects
and User Defined Functions (UDFs).
Converters are explicitly set in the options method when manipulating Range objects or in the @xw.arg
and @xw.ret decorators when using UDFs. If no converter is specified, the default converter is applied
when reading. When writing, xlwings will automatically apply the correct converter (if available) according to
the object’s type that is being written to Excel. If no converter is found for that type, it falls back to the
default converter.
All code samples below depend on the following import:
Syntax:
xw.Range UDFs
Note
Keyword arguments (kwargs) may refer to the specific converter or the default converter. For example, to
set the numbers option in the default converter and the index option in the DataFrame converter, you
would write:
xw.Range('A1:C3').options(pd.DataFrame, index=False, numbers=int).value
Default Converter¶
If no options are set, the following conversions are performed:
single cells are read in as floats in case the Excel cell holds a number, as unicode in case it holds text,
as datetime if it contains a date and as None in case it is empty.
columns/rows are read in as lists, e.g. [None, 1.0, 'a string']
2d cell ranges are read in as list of lists, e.g. [[None, 1.0, 'a string'], [None, 2.0, 'another
string']]
The following options can be set:
ndim
Force the value to have either 1 or 2 dimensions regardless of the shape of the range:
>>> import xlwings as xw
>>> sht = xw.Book().sheets[0]
>>> sht.range('A1').value = [[1, 2], [3, 4]]
>>> sht.range('A1').value
1.0
>>> sht.range('A1').options(ndim=1).value
[1.0]
>>> sht.range('A1').options(ndim=2).value
[[1.0]]
>>> sht.range('A1:A2').value
[1.0 3.0]
>>> sht.range('A1:A2').options(ndim=2).value
[[1.0], [3.0]]
numbers
By default cells with numbers are read as float, but you can change it to int:
>>> sht.range('A1').value = 1
>>> sht.range('A1').value
1.0
>>> sht.range('A1').options(numbers=int).value
1
Alternatively, you can specify any other function or type which takes a single float argument.
Using this on UDFs looks like this:
@xw.func
@xw.arg('x', numbers=int)
def myfunction(x):
# all numbers in x arrive as int
return x
Note: Excel always stores numbers internally as floats, which is the reason why the converter rounds
numbers first before turning them into integers. Otherwise it could happen that e.g. 5 might be returned as
4 in case it is represented as a floating point number that is slightly smaller than 5. Should you require
Python’s original in your converter, use instead.
dates
By default cells with dates are read as datetime.datetime, but you can change it to datetime.date:
Range:
>>> import datetime as dt
>>> sht.range('A1').options(dates=dt.date).value
UDFs: @xw.arg('x', dates=dt.date)
Alternatively, you can specify any other function or type which takes the same keyword arguments as
datetime.datetime, for example:
>>> my_date_handler = lambda year, month, day, **kwargs: "%04i-%02i-%02i" %
(year, month, day)
>>> sht.range('A1').options(dates=my_date_handler).value
'2017-02-20'
empty
Empty cells are converted per default into None, you can change this as follows:
Range: >>> sht.range('A1').options(empty='NA').value
UDFs: @xw.arg('x', empty='NA')
transpose
This works for reading and writing and allows us to e.g. write a list in column orientation to Excel:
Range: sht.range('A1').options(transpose=True).value = [1, 2, 3]
UDFs:
@xw.arg('x', transpose=True)
@xw.ret(transpose=True)
def myfunction(x):
# x will be returned unchanged as transposed both when reading and writing
return x
expand
This works the same as the Range properties table, vertical and horizontal but is only evaluated
when getting the values of a Range:
>>> import xlwings as xw
>>> sht = xw.Book().sheets[0]
>>> sht.range('A1').value = [[1,2], [3,4]]
>>> rng1 = sht.range('A1').expand()
>>> rng2 = sht.range('A1').options(expand='table')
>>> rng1.value
[[1.0, 2.0], [3.0, 4.0]]
>>> rng2.value
[[1.0, 2.0], [3.0, 4.0]]
>>> sht.range('A3').value = [5, 6]
>>> rng1.value
[[1.0, 2.0], [3.0, 4.0]]
>>> rng2.value
[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]
Note
The expand method is only available on Range objects as UDFs only allow to manipulate the calling cells.
Built-in Converters¶
xlwings offers several built-in converters that perform type conversion to dictionaries, NumPy arrays,
Pandas Series and DataFrames. These build on top of the default converter, so in most cases the options
described above can be used in this context, too (unless they are meaningless, for example the ndim in
the case of a dictionary).
It is also possible to write and register a custom converter for additional types, see below.
The samples below can be used with both xlwings.Range objects and UDFs even though only one
version may be shown.
Dictionary converter¶
The dictionary converter turns two Excel columns into a dictionary. If the data is in row orientation, use
transpose:
# Writing back and changing some of the options, e.g. getting rid of the index:
>>> sht.range('B7').options(index=False).value = df
The same sample for UDF (starting in Range('A13') on screenshot) looks like this:
@xw.func
@xw.arg('x', pd.DataFrame, header=2)
@xw.ret(index=False)
def myfunction(x):
# x is a DataFrame, do something with it
return x
Custom Converter¶
Here are the steps to implement your own converter:
Inherit from xlwings.conversion.Converter
Implement both a read_value and write_value method as static- or classmethod:
In read_value, value is what the base converter returns: hence, if no base has been specified it arrives
in the format of the default converter.
In write_value, value is the original object being written to Excel. It must be returned in the format that
the base converter expects. Again, if no base has been specified, this is the default converter.
The options dictionary will contain all keyword arguments specified in the xw.Range.options method,
e.g. when calling xw.Range('A1').options(myoption='some value') or as specified in the @arg
and @ret decorator when using UDFs. Here is the basic structure:
from xlwings.conversion import Converter
class MyConverter(Converter):
@staticmethod
def read_value(value, options):
myoption = options.get('myoption', default_value)
return_value = value # Implement your conversion here
return return_value
@staticmethod
def write_value(value, options):
myoption = options.get('myoption', default_value)
return_value = value # Implement your conversion here
return return_value
Optional: set a base converter (base expects a class name) to build on top of an existing converter, e.g.
for the built-in ones: DictCoverter, NumpyArrayConverter, PandasDataFrameConverter,
PandasSeriesConverter
Optional: register the converter: you can (a) register a type so that your converter becomes the default for
this type during write operations and/or (b) you can register an alias that will allow you to explicitly call your
converter by name instead of just by class name
The following examples should make it much easier to follow - it defines a DataFrame converter that
extends the built-in DataFrame converter to add support for dropping nan’s:
from xlwings.conversion import Converter, PandasDataFrameConverter
class DataFrameDropna(Converter):
base = PandasDataFrameConverter
@staticmethod
def read_value(builtin_df, options):
dropna = options.get('dropna', False) # set default to False
if dropna:
converted_df = builtin_df.dropna()
else:
converted_df = builtin_df
# This will arrive in Python when using the DataFrameDropna converter
for reading
return converted_df
@staticmethod
def write_value(df, options):
dropna = options.get('dropna', False)
if dropna:
converted_df = df.dropna()
else:
converted_df = df
# This will be passed to the built-in PandasDataFrameConverter when
writing
return converted_df
Now let’s see how the different converters can be applied:
# Fire up a Workbook and create a sample DataFrame
sht = xw.Book().sheets[0]
df = pd.DataFrame([[1.,10.],[2.,np.nan], [3., 30.]])
Default converter for DataFrames:
# Write
sht.range('A1').value = df
# Read
sht.range('A1:C4').options(pd.DataFrame).value
DataFrameDropna converter:
# Write
sht.range('A7').options(DataFrameDropna, dropna=True).value = df
# Read
sht.range('A1:C4').options(DataFrameDropna, dropna=True).value
Register an alias (optional):
DataFrameDropna.register('df_dropna')
# Write
sht.range('A12').options('df_dropna', dropna=True).value = df
# Read
sht.range('A1:C4').options('df_dropna', dropna=True).value
Register DataFrameDropna as default converter for DataFrames (optional):
DataFrameDropna.register(pd.DataFrame)
# Write
sht.range('A13').options(dropna=True).value = df
# Read
sht.range('A1:C4').options(pd.DataFrame, dropna=True).value
These samples all work the same with UDFs, e.g.:
@xw.func
@arg('x', DataFrameDropna, dropna=True)
@ret(DataFrameDropna, dropna=True)
def myfunction(x):
# ...
return x
Note
Python objects run through multiple stages of a transformation pipeline when they are being written to
Excel. The same holds true in the other direction, when Excel/COM objects are being read into Python.
Pipelines are internally defined by Accessor classes. A Converter is just a special Accessor which
converts to/from a particular type by adding an extra stage to the pipeline of the default Accessor. For
example, the PandasDataFrameConverter defines how a list of lists (as delivered by the default
Accessor) should be turned into a Pandas DataFrame.
The Converter class provides basic scaffolding to make the task of writing a new Converter easier. If you
need more control you can subclass Accessor directly, but this part requires more work and is currently
undocumented.