OpenPyXL in Python: Read and Write Excel Files

An `import openpyxl` that succeeds still doesn’t tell you whether a new row made it into `orders.xlsx`. I like that OpenPyXL keeps edits tied to a worksheet instead of treating the job as detached table analysis.

The example takes an order row from Python through a save and reload, then prints the values read from disk. That result makes more sense once you know how OpenPyXL represents an Excel workbook and its sheets.

What OpenPyXL does

OpenPyXL is a Python library for reading and writing Excel workbooks saved in Office Open XML formats. A workbook contains worksheets, and each worksheet contains cells addressed by a column letter and row number.

Choose OpenPyXL when your script needs to change cells, rows, sheet names, or workbook formatting. The AskPython examples for reading Excel with Python cover that next step, and the Pandas guide to reading Excel files follows the DataFrame path.

Workbook termWhat it names
WorkbookThe Excel file, which can contain several worksheets.
WorksheetOne named tab inside the workbook.
CellOne address in a worksheet, such as A1.

Install OpenPyXL in the Python environment you use

Install the package with the same Python interpreter that will run your script. If an import reports “No module named ‘openpyxl’”, check that both commands use the same environment.

python -m pip install openpyxl

Before editing a workbook you need to keep, make a copy and check the path your script will open. Saving writes the in-memory workbook to that path, and an existing file with the same name can be overwritten.

  • Run the installer and the script with the same Python environment.
  • Use a copy when you are testing changes to an existing workbook.

Create, read, and append workbook rows

This example creates an Orders worksheet, saves it, reopens the file, and appends a second item. The reload makes the final output come from the workbook on disk.

1. Create and save a workbook

Start with a new workbook when there is no file to load. Workbook() creates a workbook with an active worksheet, and append() places each list across the next row.

from openpyxl import Workbook, load_workbook

workbook = Workbook()
sheet = workbook.active
sheet.title = "Orders"
sheet.append(["Item", "Quantity", "Unit price"])
sheet.append(["Notebook", 4, 3.5])
workbook.save("orders.xlsx")

The first append writes the header row, so the next append fills its columns in the same order. I chose Orders as the worksheet title so the next step can select that tab directly.

2. Reopen the file before appending

Load an existing workbook with load_workbook(), then select the worksheet by its title. Append adds a row after the sheet’s current rows, and save writes that change to the file.

workbook = load_workbook("orders.xlsx")
sheet = workbook["Orders"]
sheet.append(["Pen", 10, 1.25])
workbook.save("orders.xlsx")

saved = load_workbook("orders.xlsx", read_only=True)
for row in saved["Orders"].iter_rows(values_only=True):
    print(row)
saved.close()

I ran the script with OpenPyXL 3.1.5 under Python 3.14.7, and the reopened workbook printed the three tuples shown. Because the script reloads orders.xlsx after saving, those rows come from the file on disk.

Terminal output from a tested OpenPyXL workbook example
Running the example prints the rows reopened from orders.xlsx.

Workbook formats and formulas have boundaries

OpenPyXL works with Office Open XML files. The official format overview and tutorial document the file types and load options, and the formula documentation explains which values Python can read.

Workbook featureWhat OpenPyXL does
.xlsx, .xlsm, .xltx, .xltmThese are supported Office Open XML workbook or template formats. Legacy binary .xls files are outside this list, so convert one to .xlsx before loading it.
Formula cellsOpenPyXL can store a formula such as =SUM(B2:B5), but it does not calculate the result. load_workbook(data_only=True) reads the cached value saved by a spreadsheet application.
Macros in .xlsmLoad with keep_vba=True and save with an .xlsm filename to preserve VBA content. OpenPyXL does not let you edit the macros.
Existing workbook featuressave() overwrites the target without a warning. The docs also warn that unsupported shapes can be lost when a workbook is opened and saved.
Large workbooksMemory use can be high. read_only=True supports read-only iteration, and the workbook needs close() afterward. Use write_only=True for new files written one row at a time.

The OpenPyXL formula documentation is explicit that formulas are not evaluated. Use Excel or another calculation engine when your task needs fresh formula results, and check unsupported workbook features before saving over an important file.

The saved workbook is the handoff

A worksheet title determines which tab your code changes, so inspect the workbook you intend to edit before selecting a sheet. Start with a copy when the original file needs to stay intact.

python -c "from openpyxl import load_workbook; book = load_workbook('orders.xlsx', read_only=True); print(tuple(book.sheetnames)); book.close()"

OpenPyXL questions

These questions cover the install and workbook boundaries that are not visible in the short example.

Why can Python fail to import OpenPyXL after installation?

The package may be installed into a different Python environment from the one that runs your script. Run python -m pip install openpyxl with the interpreter used to launch the script.

Does OpenPyXL calculate formulas?

OpenPyXL stores formula text but does not evaluate it. With data_only=True, load_workbook reads a cached value last saved by a spreadsheet application, which can be missing if the file has no cached result.

Can OpenPyXL open a .xls file?

The package supports Office Open XML files such as .xlsx and .xlsm. Convert a legacy .xls workbook to .xlsx before using these examples.

How do I preserve macros in an .xlsm workbook?

Pass keep_vba=True to load_workbook() and save the result with an .xlsm filename to preserve VBA content. OpenPyXL does not let you edit the macros.

Isha Bansal
Isha Bansal

Hey there stranger!
Do check out my blogs if you are a keen learner!

Hope you like them!

Articles: 185