Python openpyxl Formulas That Show Stale Values After Writing, Fix it

June 23, 2026 4 min read

Python has become one of the most popular tools for Excel automation.

Using:

openpyxl

developers can:

  • Generate reports
  • Create dashboards
  • Build financial models
  • Export analytics
  • Automate spreadsheet workflows
  • Populate templates

A common workflow looks like:

from openpyxl import load_workbook

wb = load_workbook("report.xlsx")

sheet = wb.active

sheet["A1"] = 100
sheet["B1"] = 200
sheet["C1"] = "=SUM(A1:B1)"

wb.save("report.xlsx")

Everything appears correct.

However, when reopening the workbook, developers often encounter a confusing problem:

Formula Exists
↓
Displayed Value Is Wrong

or:

Formula Exists
↓
Old Calculation Remains

or:

Formula Appears Blank

The workbook saves successfully.

The formulas are present.

Yet the values appear stale.

This frequently leads developers to assume:

openpyxl Failed

In reality:

openpyxl writes formulas but does not calculate them.

Understanding this distinction is critical for building reliable Excel automation systems.

In this guide, you'll learn why stale formula values occur, how Excel handles calculations, and the best ways to ensure your spreadsheets always contain accurate results.


What You Will Learn From This Article

After reading this guide, you'll understand:

  • How openpyxl handles formulas.
  • Why formula values become stale.
  • The difference between formulas and calculated values.
  • Excel's recalculation process.
  • Common mistakes developers make.
  • Practical solutions for production systems.
  • Best practices for spreadsheet automation.

Understanding How Excel Stores Formulas

When Excel saves a workbook, it often stores:

Formula
+
Cached Result

Example:

Formula:

=SUM(A1:B1)

Cached result:

300

The workbook may contain both.

This improves performance when opening large spreadsheets.


What openpyxl Actually Writes

When you assign:

sheet["C1"] = "=SUM(A1:B1)"

openpyxl stores:

Formula Definition

but does not evaluate:

SUM(A1:B1)

No calculation engine is involved.


Why This Surprises Developers

Many assume:

Write Formula
↓
Save Workbook
↓
Formula Automatically Calculated

That is not how openpyxl works.

Instead:

Write Formula
↓
Save Workbook
↓
Excel Calculates Later

if Excel is opened.


Common Symptom #1

Old Values Remain

Example:

Original workbook:

A1 = 100
B1 = 200
C1 = 300

Formula:

=SUM(A1:B1)

Python updates:

A1 = 500
B1 = 600

Expected:

1100

Actual cached value:

300

until Excel recalculates.


Common Symptom #2

Reading Formulas Returns Unexpected Values

Example:

wb = load_workbook(
    "report.xlsx",
    data_only=True
)

You expect:

Latest Formula Result

Instead you receive:

Old Cached Value

or:

None

because recalculation never occurred.


Understanding data_only=True

Example:

load_workbook(
    "report.xlsx",
    data_only=True
)

returns:

Cached Formula Values

not:

Live Formula Evaluation

This distinction causes many debugging sessions.


Why openpyxl Doesn't Calculate Formulas

openpyxl is:

Excel File Library

not:

Excel Calculation Engine

Implementing full Excel calculation support would require handling:

  • Thousands of functions
  • Dependency chains
  • Array formulas
  • Dynamic ranges
  • Pivot calculations
  • Financial functions

This is far beyond the scope of the library.


How Excel Normally Handles Recalculation

Workflow:

Workbook Opens
↓
Excel Detects Changes
↓
Recalculates Formulas
↓
Updates Cached Values

The recalculation happens inside Excel itself.


Solution #1

Force Excel Recalculation

A common approach:

wb.calculation.fullCalcOnLoad = True

or:

wb.calculation.forceFullCalc = True

depending on workbook structure.

This signals Excel:

Recalculate Everything
When Opened

Example

from openpyxl import load_workbook

wb = load_workbook(
    "report.xlsx"
)

wb.calculation.forceFullCalc = True

wb.save("report.xlsx")

When Excel opens:

Workbook
↓
Full Recalculation

occurs automatically.


Solution #2

Open and Save Through Excel

If Excel exists on the machine:

Python
↓
Generate Workbook
↓
Excel Opens File
↓
Excel Recalculates
↓
Excel Saves

Now cached values become accurate.


Solution #3

Use Microsoft Excel Automation

Windows environments often use:

win32com.client

Example:

import win32com.client

Workflow:

Open Workbook
↓
Calculate
↓
Save
↓
Close

This uses Excel's native calculation engine.


Solution #4

Calculate Values in Python

Sometimes formulas are simple.

Example:

Instead of:

=SUM(A1:B1)

use:

sheet["C1"] = (
    sheet["A1"].value +
    sheet["B1"].value
)

Benefits:

  • No recalculation dependency
  • Faster processing
  • Predictable results

Suitable for simple reports.


Common Mistake #1

Assuming Save Triggers Calculation

Many developers expect:

wb.save()

to trigger:

Formula Recalculation

It does not.

Only file serialization occurs.


Common Mistake #2

Using data_only=True Immediately

Example:

wb.save()

wb2 = load_workbook(
    "file.xlsx",
    data_only=True
)

Expected:

Updated Results

Actual:

Old Cached Results

because no recalculation happened.


Common Mistake #3

Confusing Formula Storage With Formula Execution

These are different operations.

Store Formula

and:

Execute Formula

are separate concerns.

openpyxl handles the first.

Excel handles the second.


Dynamic Formula Challenges

Modern Excel supports:

  • FILTER()
  • UNIQUE()
  • SORT()
  • XLOOKUP()
  • LET()
  • LAMBDA()

These formulas often require:

Excel Calculation Engine

and cannot be reliably evaluated outside Excel.


Production Reporting Systems

Many reporting platforms follow:

Database
↓
Python
↓
Excel Template
↓
Formula Injection
↓
User Opens Workbook
↓
Excel Recalculates

This workflow is generally sufficient.


Server-Side Reporting Considerations

If users never open Excel:

Python
↓
Generate Workbook
↓
Send Directly

stale values become problematic.

In these cases:

  • Calculate values manually
  • Use Excel automation
  • Generate static reports

depending on requirements.


Diagnosing Stale Formula Problems

Check:

Does Excel Show Correct Results?

If yes:

Calculation Works

Does data_only=True Return Old Values?

If yes:

Cached Values Stale

Was Excel Ever Opened?

If not:

Recalculation Never Happened

The cause becomes obvious.


Real-World Example

A finance team generates monthly reports.

Template contains:

=SUM(B2:B100)

Python updates:

Revenue Rows

Workbook saved.

Finance users receive:

Incorrect Totals

because:

Cached Values
↓
Not Updated

Solution:

Force Recalculation
↓
Excel Opens File
↓
Totals Update Correctly

Problem resolved.


Best Practices Checklist

When working with openpyxl formulas:

βœ… Understand that openpyxl does not calculate formulas

βœ… Force recalculation when appropriate

βœ… Test with Excel directly

βœ… Verify cached values carefully

βœ… Use data_only=True correctly

βœ… Automate Excel when necessary

βœ… Calculate simple values in Python

βœ… Document workbook dependencies

βœ… Test production workflows

βœ… Validate generated reports before distribution


Common Mistakes to Avoid

Avoid:

❌ Assuming formulas recalculate automatically

❌ Treating stale values as file corruption

❌ Using data_only=True incorrectly

❌ Expecting openpyxl to behave like Excel

❌ Ignoring cached formula values

❌ Sending unvalidated reports to users

❌ Forgetting recalculation requirements


Performance Considerations

Large workbooks may contain:

50,000+
Formulas

or:

500,000+
Formula Dependencies

Forcing recalculation can increase:

Workbook Open Time

Therefore:

Calculate Only When Necessary

is often the best strategy.


Wrapping Summary

One of the most common misconceptions about openpyxl is that writing formulas automatically updates their results. In reality, openpyxl stores formula definitions but does not contain Excel's calculation engine. As a result, workbooks may contain stale cached values until Excel or another compatible calculation engine performs a recalculation.

This behavior frequently leads to confusing issues where formulas appear correct but displayed values remain outdated. Understanding the distinction between formula storage and formula evaluation is the key to diagnosing these problems. Whether you choose to force recalculation, automate Excel, calculate values directly in Python, or redesign reporting workflows, the right solution depends on how the workbook will be used.

By understanding Excel's calculation model and openpyxl's responsibilities, developers can build reliable spreadsheet automation systems that produce accurate, predictable results even in large-scale production environments.

πŸ“€ Share this article

Sign in to save

Comments (0)

No comments yet. Be the first!

Leave a Comment

Sign in to comment with your profile.

πŸ“¬ Weekly Newsletter

Stay ahead of the curve

Get the best programming tutorials, data analytics tips, and tool reviews delivered to your inbox every week.

No spam. Unsubscribe anytime.