How to use the Excel Formula to Python
- Paste an Excel or Google Sheets formula, or pick one from Examples.
- Choose the target language — the page starts with Python, and you can switch at any time.
- Rename the detected cell references to meaningful variable names, for example
B2→price. - Choose a full function or the bare expression, then copy or download the code. Helper functions are added automatically when a formula needs them.
Moving spreadsheet logic into Python
Data teams regularly rebuild Excel models in Python — to automate a monthly report, feed a pandas pipeline or move a calculation behind an API. Re-typing formulas is slow and risky, because Python and Excel disagree on details like rounding and text comparison. This converter parses the formula with Excel's own precedence rules and writes equivalent, readable Python.
=SUMIFS(D2:D100, B2:B100, "North", C2:C100, ">=1000")
def calculate(d2_d100, b2_b100, c2_c100):
return sum_ifs(d2_d100, b2_b100, 'North', c2_c100, '>=1000')
The sum_ifs() helper (and the matches_criteria() function it uses) is included above the function, so the output runs as-is.
How Excel functions map to Python
| Excel | Python |
|---|---|
IF(c, a, b) | a if c else b |
IFS / SWITCH | chained conditional expressions |
AND, OR, NOT | and, or, not |
= and <> | == and != |
a & b | str(a) + str(b) |
a ^ b | a ** b |
SUM(A2:A10) | sum(a2_a10) |
ROUND(x, 2) | excel_round() helper — not round() |
MOD(a, b) | a % b |
SUMIFS / COUNTIF… | sum_ifs(), count_if() helpers |
VLOOKUP / XLOOKUP | vlookup(), xlookup() helpers |
IFERROR(x, y) | if_error(lambda: x, y) |
LEFT / MID / LEN | slicing, len() |
TODAY() / NOW() | date.today(), datetime.now() |
Pitfalls the converter handles
- Banker's rounding. Python's
round(2.5)returns 2 andround(0.125, 2)returns 0.12; Excel gives 3 and 0.13. The output uses anexcel_round()helper based onDecimalwithROUND_HALF_UP. - Concatenation.
"Total: " + 5raises aTypeErrorin Python, so every non-text operand of&is wrapped instr(). - IF is an expression.
IFbecomes Python's conditional expression (a if c else b), and nested IFs chain naturally. - Integer division. Python 3's
/always returns a float, matching Excel. - Errors.
IFERRORbecomesif_error(lambda: …, fallback), which catchesZeroDivisionError,LookupErrorfrom lookups and other exceptions. - Dates.
DATE,TODAYandYEARmap to thedatetimemodule. Excel rolls over invalid dates (month 13); Python raises an error, and the converter warns you.
Working with pandas
Generated functions work on scalars and lists. To apply a row-wise formula to a DataFrame, call it with df.apply(lambda r: calculate(r.price, r.qty), axis=1); for aggregates such as SUMIFS, pandas boolean masks (df.loc[(df.region == 'North') & (df.total >= 1000), 'amount'].sum()) are faster on large data.
Supported functions: IF, IFS, SWITCH, AND, OR, NOT, XOR, IFERROR, IFNA, ISBLANK, ISNUMBER, ISTEXT, SUM, AVERAGE, MIN, MAX, COUNT, COUNTA, COUNTBLANK, PRODUCT, MEDIAN, ROUND, ROUNDUP, ROUNDDOWN, INT, TRUNC, ABS, SIGN, MOD, POWER, SQRT, EXP, LN, LOG, LOG10, PI, CEILING, FLOOR, RAND, RANDBETWEEN, SUMIF, SUMIFS, COUNTIF, COUNTIFS, AVERAGEIF, AVERAGEIFS, SUMPRODUCT, VLOOKUP, XLOOKUP, MATCH, INDEX, CONCAT, CONCATENATE, TEXTJOIN, LEFT, RIGHT, MID, LEN, UPPER, LOWER, PROPER, TRIM, SUBSTITUTE, REPLACE, FIND, SEARCH, REPT, VALUE, EXACT, TEXT (number formats), TODAY, NOW, DATE, YEAR, MONTH, DAY. Load the spreadsheet itself with Excel to JSON and json.load().
Tools that pair well with the Excel Formula to Python
- Excel Formula to JavaScript — Paste a spreadsheet formula and get readable JavaScript: a function with named parameters, correct operator precedence and small helper functions for things JavaScript lacks, such as SUMIFS and VLOOKUP.
- Excel Formula to SQL — Paste an Excel formula and get the equivalent SQL expression or SELECT query — IF and IFS as CASE WHEN, SUMIFS and COUNTIFS as conditional aggregates, and the right string and date functions for your database.
- Excel Formula to PHP — Paste a spreadsheet formula and get a PHP 8 function: parenthesised ternaries, mb_* string functions, and helpers for lookups, conditional sums and Excel-style MOD.
- CSV to JSON — Paste CSV or TSV and get a JSON array of objects.
Frequently asked questions
Why does the output use excel_round() instead of round()?
round(2.5) is 2. Excel rounds halves away from zero. The helper reproduces Excel exactly.Does it work with pandas DataFrames?
.tolist(), or apply a row-wise function with df.apply(…, axis=1).How is VLOOKUP translated?
vlookup(value, table, column, approximate) helper where the table is a list of rows. Exact match (FALSE) compares text case-insensitively like Excel, and a missing value raises LookupError, which IFERROR catches.