In general a formula in Excel can be used directly in the
write_formula() method:
worksheet.write_formula('A1', '=10*B1 + C1')
However, there are a few potential issues and differences that the user should be aware of. These are explained in the following sections.
Excel stores formulas in the format of the US English version, regardless of the language or locale of the end-user’s version of Excel. Therefore all formula function names written using XlsxWriter must be in English:
worksheet.write_formula('A1', '=SUM(1, 2, 3)') # OK
worksheet.write_formula('A2', '=SOMME(1, 2, 3)') # French. Error on load.
Also, formulas must be written with the US style separator/range operator which is a comma (not semi-colon). Therefore a formula with multiple values should be written as follows:
worksheet.write_formula('A1', '=SUM(1, 2, 3)') # OK
worksheet.write_formula('A2', '=SUM(1; 2; 3)') # Semi-colon. Error on load.
If you have a non-English version of Excel you can use the following multi-lingual formula translator to help you convert the formula. It can also replace semi-colons with commas.
Excel 2010 and later added functions which weren’t defined in the original
file specification. These functions are referred to by Microsoft as future
functions. Examples of these functions are ACOT, CHISQ.DIST.RT ,
CONFIDENCE.NORM, STDEV.P, STDEV.S and WORKDAY.INTL.
When written using write_formula() these functions need to be fully
qualified with a _xlfn. (or other) prefix as they are shown the list
below. For example:
worksheet.write_formula('A1', '=_xlfn.STDEV.S(B1:B10)')
They will appear without the prefix in Excel:
The following list is taken from MS XLSX extensions documentation on future functions.
_xlfn.ACOT_xlfn.ACOTH_xlfn.AGGREGATE_xlfn.ARABIC_xlfn.BASE_xlfn.BETA.DIST_xlfn.BETA.INV_xlfn.BINOM.DIST_xlfn.BINOM.DIST.RANGE_xlfn.BINOM.INV_xlfn.BITAND_xlfn.BITLSHIFT_xlfn.BITOR_xlfn.BITRSHIFT_xlfn.BITXOR_xlfn.CEILING.MATH_xlfn.CEILING.PRECISE_xlfn.CHISQ.DIST_xlfn.CHISQ.DIST.RT_xlfn.CHISQ.INV_xlfn.CHISQ.INV.RT_xlfn.CHISQ.TEST_xlfn.COMBINA_xlfn.CONFIDENCE.NORM_xlfn.CONFIDENCE.T_xlfn.COT_xlfn.COTH_xlfn.COVARIANCE.P_xlfn.COVARIANCE.S_xlfn.CSC_xlfn.CSCH_xlfn.DAYS_xlfn.DECIMALECMA.CEILING_xlfn.ERF.PRECISE_xlfn.ERFC.PRECISE_xlfn.EXPON.DIST_xlfn.F.DIST_xlfn.F.DIST.RT_xlfn.F.INV_xlfn.F.INV.RT_xlfn.F.TEST_xlfn.FILTERXML_xlfn.FLOOR.MATH_xlfn.FLOOR.PRECISE_xlfn.FORECAST.ETS_xlfn.FORECAST.ETS.CONFINT_xlfn.FORECAST.ETS.SEASONALITY_xlfn.FORECAST.ETS.STAT_xlfn.FORECAST.LINEAR_xlfn.FORMULATEXT_xlfn.GAMMA_xlfn.GAMMA.DIST_xlfn.GAMMA.INV_xlfn.GAMMALN.PRECISE_xlfn.GAUSS_xlfn.HYPGEOM.DIST_xlfn.IFNA_xlfn.IMCOSH_xlfn.IMCOT_xlfn.IMCSC_xlfn.IMCSCH_xlfn.IMSEC_xlfn.IMSECH_xlfn.IMSINH_xlfn.IMTAN_xlfn.ISFORMULAISO.CEILING_xlfn.ISOWEEKNUM_xlfn.LOGNORM.DIST_xlfn.LOGNORM.INV_xlfn.MODE.MULT_xlfn.MODE.SNGL_xlfn.MUNIT_xlfn.NEGBINOM.DISTNETWORKDAYS.INTL_xlfn.NORM.DIST_xlfn.NORM.INV_xlfn.NORM.S.DIST_xlfn.NORM.S.INV_xlfn.NUMBERVALUE_xlfn.PDURATION_xlfn.PERCENTILE.EXC_xlfn.PERCENTILE.INC_xlfn.PERCENTRANK.EXC_xlfn.PERCENTRANK.INC_xlfn.PERMUTATIONA_xlfn.PHI_xlfn.POISSON.DIST_xlfn.QUARTILE.EXC_xlfn.QUARTILE.INC_xlfn.QUERYSTRING_xlfn.RANK.AVG_xlfn.RANK.EQ_xlfn.RRI_xlfn.SEC_xlfn.SECH_xlfn.SHEET_xlfn.SHEETS_xlfn.SKEW.P_xlfn.STDEV.P_xlfn.STDEV.S_xlfn.T.DIST_xlfn.T.DIST.2T_xlfn.T.DIST.RT_xlfn.T.INV_xlfn.T.INV.2T_xlfn.T.TEST_xlfn.UNICHAR_xlfn.UNICODE_xlfn.VAR.P_xlfn.VAR.S_xlfn.WEBSERVICE_xlfn.WEIBULL.DISTWORKDAY.INTL_xlfn.XOR_xlfn.Z.TESTWorksheet tables can be added with XlsxWriter using the add_table()
method:
worksheet.add_table('B3:F7', {options})
By default tables are named Table1, Table2, etc., in the order that
they are added. However it can also be set by the user using the name parameter:
worksheet.add_table('B3:F7', {'name': 'SalesData'})
If you need to know the name of the table, for example to use it in a formula, you can get it as follows:
table = worksheet.add_table('B3:F7')
table_name = table.name
When used in a formula a table name such as TableX should be referred to
as TableX[] (like a Python list):
worksheet.write_formula('A5', '=VLOOKUP("Sales", Table1[], 2, FALSE')
If there is an error in the syntax of a formula it is usually displayed in
Excel as #NAME?. Alternatively you may get a warning from Excel when the
file is loaded. If you encounter an error like this you can debug it as
follows:
Finally if you have completed all the previous steps and still get a
#NAME? error you can examine a valid Excel file to see what the correct
syntax should be. To do this you should create a valid formula in Excel and
save the file. You can then examine the XML in the unzipped file.
The following shows how to do that using Linux unzip and libxml’s xmllint to format the XML for clarity:
$ unzip myfile.xlsx -d myfile
$ xmllint --format myfile/xl/worksheets/sheet1.xml | grep '<f>'
<f>SUM(1, 2, 3)</f>
XlsxWriter doesn’t calculate the result of a formula and instead stores the value 0 as the formula result. It then sets a global flag in the XLSX file to say that all formulas and functions should be recalculated when the file is opened.
This is the method recommended in the Excel documentation and in general it works fine with spreadsheet applications. However, applications that don’t have a facility to calculate formulas will only display the 0 results. Examples of such applications are Excel Viewer, PDF Converters, and some mobile device applications.
If required, it is also possible to specify the calculated result of the
formula using the optional value parameter for write_formula():
worksheet.write_formula('A1', '=2+2', num_format, 4)
The value parameter can be a number, a string, a bool or one of the
following Excel error codes:
#DIV/0!
#N/A
#NAME?
#NULL!
#NUM!
#REF!
#VALUE!
It is also possible to specify the calculated result of an array formula
created with write_array_formula():
# Specify the result for a single cell range.
worksheet.write_array_formula('A1:A1', '{=SUM(B1:C1*B2:C2)}', format, 2005)
However, using this parameter only writes a single value to the upper left
cell in the result array. For a multi-cell array formula where the results are
required, the other result values can be specified by using write_number()
to write to the appropriate cell:
# Specify the results for a multi cell range.
worksheet.write_array_formula('A1:A3', '{=TREND(C1:C3,B1:B3)}', format, 15)
worksheet.write_number('A2', 12, format)
worksheet.write_number('A3', 14, format)