Contact

PY Function in Microsoft Excel: Python Editor, xl() and Return Types

Python Intermediate Microsoft Excel

Understand Excel's editor-generated PY function with original arithmetic and worksheet-data examples, cloud requirements and return-type guidance.

Verification status: See the evidence and test scope described in this guide. How formula examples are checked

Quick answer: PY is Excel’s Python-cell function. Start by typing =PY and selecting PY from AutoComplete; Excel switches to its Python editor. Enter Python code there, rather than pasting a complete PY call into the ordinary formula bar. Excel generates the underlying function for you. Microsoft’s PY reference

Verification: The examples below are original. Their arithmetic was checked in ordinary Python, independently of Excel. No Excel Python runtime, cloud calculation, subscription or add-on was activated for this guide. The xl() bridge and Excel editor workflow are documented, not native-tested here.

Availability and version boundaries

Checked October 19, 2026. Microsoft lists these availability boundaries:

  • Enterprise/Business Windows: Current Channel 2408 (17928.20114), Monthly Enterprise 2408 (17928.20216), Semi-Annual Enterprise 2502 (18526.20472), or later supported builds; also Excel on the web.
  • Enterprise/Business Mac: Current Channel 16.96 (25041326) onward.
  • Personal/Family: preview on web and Windows Current Channel 2405 (17628.20164); Mac preview requires the Beta Channel, starting 16.95 (25021921).
  • Education Windows: preview availability starting 2406 (17726.20016); consult the current channel requirements.
  • iPad, iPhone and Android cannot recalculate Python cells. A perpetual Excel license alone does not qualify.

Eligibility requires a qualifying paid Microsoft 365 license with desktop-app rights. Free, device-based and shared-computer-activation licenses are excluded. These are feature-specific rollout boundaries, not a claim that PY began with every listed Excel edition. No retirement is announced on the reviewed page. Recheck the availability matrix before deployment.

Standard compute is included with eligible subscriptions. The optional add-on provides premium compute and additional calculation modes; this tutorial does not require buying it. Availability and entitlements depend on the account. See Microsoft’s licensing FAQ.

Cloud processing and privacy

Python runs in an isolated Microsoft Cloud container, so this is not an offline local Python installation. Referenced workbook data is processed there. Microsoft says Python code has no network access and cannot access the user’s computer, account or token; data is not persisted in the container between sessions. Inputs come through workbook references or supported Power Query connections. The code can return values or images, not macros or formulas. These boundaries do not remove your responsibility to follow your organization’s rules for cloud processing. Use invented practice data until that permission is clear. Protected View does not execute Python formulas from untrusted workbooks. Do not disable protections to make an example run. Microsoft’s security documentation

Underlying syntax and return types

=PY(python_code, return_type)

This is a description of the stored function, not an ordinary formula-bar entry recipe.

Argument Meaning Constraint
python_code Python source evaluated by the cloud runtime Required static text
return_type How the result is represented in Excel Required static number: 0 or 1

Return type 0 means an Excel value. Return type 1 means a Python object. A scalar total intended for another worksheet calculation is usually easier to use as an Excel value. A DataFrame retained for subsequent Python analysis can remain a Python object. Choose the output mode in the Python-cell controls.

Both arguments are static: do not try to construct the source by concatenating ordinary worksheet formulas or passing a cell reference as the PY argument itself. Instead, write fixed Python source and obtain changing input values through xl(). PY cannot be nested within other Excel functions in the same formula. Once a Python cell returns an Excel value, a separate ordinary worksheet formula can refer to that output cell.

FORMULATEXT can expose the underlying PY formula, and add-ins can read or insert it. The normal formula-bar experience displays the Python editor instead. PY argument and integration reference

Example 1: a small arithmetic calculation

In an eligible practice workbook, select an empty cell outside any input range. Type =PY, select the autocomplete entry, then put this source into the Python editor:

(18 + 6) / 3

Choose Excel value output and commit the Python cell. The independently calculated expected numeric result is 8: first 18 + 6 = 24, then 24 / 3 = 8. Python’s division produces 8.0; Excel’s number formatting may display 8. Formatting is not a different result.

This example isolates editor entry and output mode. If it fails before any workbook reference is involved, inspect account eligibility, build, network connection and the displayed error before changing the arithmetic.

Example 2: read a small worksheet dataset with xl()

Enter this invented dataset in A1:B4, preserving numeric values in B2:B4:

Cell row A: Item B: Units
1 Item Units
2 Notebook 12
3 Folder 18
4 Marker 9

Create a Python cell in D2 and enter:

data = xl("A1:B4", headers=True)
data["Units"].sum()

Select Excel value output. The expected result is 39, because 12 + 18 + 9 = 39. headers=True makes the first row column names, allowing data["Units"] to select the numeric column. The expression on the final line produces the result. The independent check used the literal list [12, 18, 9]; it did not simulate or execute Excel’s xl() implementation.

For comparison, SUM applied as =SUM(B2:B4) in another cell has the same mathematical total. That comparison helps detect a wrong range or edited input; it does not establish that the cloud environment was tested.

To inspect the whole dataset instead of its total, use the same data = xl(...) line followed by data as the final expression, and choose Python object output. Expect a DataFrame object, not a prescribed scalar or a guaranteed spill layout. Object rendering is environment-dependent and is not a screenshot claim.

Troubleshooting and careful interpretation

  • No Python editor: check account, platform and release eligibility. Installing local Python does not add PY to an unsupported Excel edition.
  • Wrong total: check the exact range, the Units heading and whether the three inputs are numeric. Do not include the output cell in its own input range.
  • A Python object instead of a worksheet number: inspect output mode before rewriting the calculation.
  • A formula cannot be entered: do not nest PY inside SUM or construct its arguments dynamically. Use a Python cell and reference its Excel-value output separately.
  • An unfamiliar service or Python error: retain the original error and consult Microsoft help. Replacing every failure with zero would make missing or failed calculations appear valid.

Use AVERAGE or SUM directly when ordinary worksheet arithmetic is sufficient. PY is useful when the analysis genuinely benefits from Python libraries or DataFrame transformations, with the added cloud and compatibility requirements understood.