Working with Excel spreadsheets.
Read cells as records, validate them, and write a new workbook deliberately.
Start here.
A spreadsheet is a grid of cells grouped into worksheets inside a workbook. A cell has a coordinate such as A1 and may contain text, a number, a date-like value, or a formula. Python libraries can read and write the workbook file, but the file is not a simple table unless you decide which row contains headings and which rows contain records. That decision is part of the data model, not just the library call.
openpyxl is a third-party package for .xlsx workbooks. Loading a workbook creates Python objects for its sheets and cells. Selecting sheet['A1'] refers to one cell; iterating rows gives a sequence of cell values. A formula cell may store formula text or a cached calculated result depending on how the file was loaded and saved. openpyxl does not act as a full Excel calculation engine, so writing a formula does not automatically evaluate it.
Before changing a workbook, preserve an original copy and decide how blank rows, merged cells, and dates should be interpreted. A number that appears formatted as currency is still a numeric value; a cell containing '12' as text is not the same kind. A reliable automation reads the data, validates the expected shape, makes the intended changes, and saves to a clearly named output file so the source remains understandable.
Build the model.
A spreadsheet is a grid, but most automation tasks are about records. Decide which row contains headers, which columns matter, and what one row represents. A library such as openpyxl can read and write .xlsx workbooks without opening Excel. The programming challenge is not merely addressing a cell; it is turning uneven rows into validated data with a clear meaning.
Start by reading a workbook and a worksheet, then inspect a small range before transforming anything. Spreadsheet cells may contain text, numbers, dates, empty values, or formulas. If a cell contains a formula, the library may expose the formula expression rather than a freshly calculated result; it does not act as a full spreadsheet calculation engine.
How it works.
openpyxl uses one-based row and column positions, while Python lists use zero-based indexes. This difference is easy to miss when turning a row tuple into a list. iter_rows(values_only=True) gives cell values and avoids forcing your processing function to understand cell objects. Convert types at the edge: a displayed amount may be stored as a number or as text.
A workbook is a document with more than your data: styles, formulas, named ranges, and charts may matter to its owner. Load an original file, write a separate output file, and compare the result before replacing anything. For large workbooks, read-only and write-only modes can reduce memory, but they change which operations are available.
Avoid building a row by concatenating display strings and writing them into cells. Keep values typed while calculating, then set number formatting for presentation. Also decide what a blank row means: skip it, report it, or treat it as the end of a table. The same rule should apply every time the script runs.
Read it line by line.
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws.append(['Item', 'Quantity'])
ws.append(['Notebook', 3])
ws.append(['Pen', 5])
wb.save('inventory.xlsx')Workbook creates a new document with an active worksheet. Each append writes one row of typed values. The first row names the columns; later rows contain text and integer quantities. save writes inventory.xlsx and can overwrite a file with that name. The example depends on the optional openpyxl package.
See the rule again.
from openpyxl import load_workbook
wb = load_workbook('inventory.xlsx', read_only=True)
ws = wb.active
rows = ws.iter_rows(min_row=2, values_only=True)
total = sum(int(quantity) for _, quantity in rows if quantity is not None)
print(total)The output is 8 for the example workbook. min_row=2 skips the header. values_only returns plain cell values. The None check skips blank quantity cells; a stricter tool might report such rows instead of ignoring them.
A mistake to avoid.
Do not assume a formula cell will be recalculated by openpyxl. If your output depends on formula results, decide which application or service calculates them and verify the cached values you read.