Automate Excel aggregation in Python: openpyxl on an 8GB mini PC
A minimal example of automating monthly Excel aggregation with Python's openpyxl. Shows how to keep it light without pandas and how to avoid breaking existing files.
Excel aggregation work takes more time as the number of entries grows. You change filters and check the total, then transcribe to another sheet. That repetition can be replaced with a small Python script. This article uses openpyxl, which runs lightly even on an 8GB mini PC without pandas.
Bottom line: openpyxl is very light for just reading and writing Excel files. You don't need pandas. Try first "read / aggregate / write to another sheet" — just these three.
Prep
python -m venv .venv
.venv\Scripts\Activate.ps1
pip install openpyxl
Using a virtual environment keeps you from affecting other Python projects. If PowerShell throws an execution policy error, switch to the Command Prompt.
Shape of the source data
For example, in the data sheet of sales.xlsx, assume these columns.
| Column A | Date |
|---|---|
| Column B | Store |
| Column C | Product |
| Column D | Amount |
The aggregation script
from openpyxl import load_workbook
from collections import defaultdict
wb = load_workbook("sales.xlsx")
ws = wb["data"]
totals = defaultdict(int)
for row in ws.iter_rows(min_row=2, values_only=True):
branch, amount = row[1], row[3]
if branch and isinstance(amount, (int, float)):
totals[branch] += amount
out = wb.create_sheet("Totals")
out.append(["Store", "Total"])
for branch, total in sorted(totals.items()):
out.append([branch, total])
wb.save("sales_totals.xlsx")
print("Done")
The script reads from row 2 onward, sums the amounts per store, and writes to a new sheet at the end. As a safety practice, save to a new file rather than overwriting the original.
Estimated execution time
On the verification machine, reading 4 columns of 10,000 rows, aggregating per store, and saving took about 2 seconds. That's just a few seconds including opening the file — much faster than the Excel filter operation repeated every time.
Practical uses
- Aggregate by product too: Just change the key from "store" to "store + product" and the same code works.
- Break down by month: You can extract the month from the date and write to a monthly sheet.
- Export as CSV: You don't need to open Excel, so it suits handing data to other systems.
Note: openpyxl is not a formula calculation engine. For formula cells, it reads the cached values saved by Excel. For recalculation, open and save in Excel, or use the computed values.
Notes
- Always take a backup of the original file, and output to a different name.
- For
.xlsm(macro-enabled), specifykeep_vba=Trueso macros aren't lost. - If text is mixed into the amount column, those rows are left out of the totals. Add a check for the value type.
- When handling the company's files, check the storage destination, sharing scope, and information management rules.
Summary
You don't need a huge library to automate Excel aggregation. openpyxl's "read / aggregate / write to another sheet" alone already saves a lot of monthly work. Try replacing one aggregation first.