An .xlsx file is, structurally, a ZIP archive of XML documents describing sheets, formulas, styles, and formatting — Excel’s binary .xls format is legacy; the modern format is genuinely an XML-based container, which is exactly why tools like Python’s openpyxl can manipulate spreadsheets programmatically without Excel installed.
Reading and Writing XLSX in Python
import openpyxl
# Reading
wb = openpyxl.load_workbook('data.xlsx')
sheet = wb.active
for row in sheet.iter_rows(min_row=2, values_only=True):
print(row)
# Writing
wb = openpyxl.Workbook()
sheet = wb.active
sheet['A1'] = 'Name'
sheet['B1'] = 'Score'
sheet.append(['Alice', 95])
wb.save('output.xlsx')
For bulk data manipulation rather than cell-level formatting, pandas‘ read_excel/to_excel functions are usually more efficient — reach for openpyxl directly when you need formulas, cell styling, or fine-grained control that pandas abstracts away.
A Key Gotcha: Formulas vs. Values
openpyxl writes formulas as literal strings — a cell set to '=SUM(A1:A10)' stores that formula text but has no cached calculated value until the file is actually opened and recalculated by Excel or a tool like LibreOffice run headlessly. Reading a freshly-written formula cell back with data_only=True before recalculation returns None, a common source of confusion when generating spreadsheets programmatically that need to display calculated values immediately.
Practical Uses Beyond Manual Spreadsheet Editing
- Automated report generation — populating a templated spreadsheet with fresh data on a schedule, avoiding manual data entry.
- Bulk data validation — scripting checks across large spreadsheets that would be impractical to verify manually cell by cell.
- Data migration — programmatically extracting data from legacy spreadsheet-based systems into a database or other format.
Frequently Asked Questions
Can I edit an existing spreadsheet’s formulas without breaking its formatting?
Yes, openpyxl preserves existing formatting and formulas in cells you don’t explicitly modify — the key risk is inadvertently overwriting formatting when writing to cells, not a general limitation of the library.
Conclusion
Modern .xlsx files are XML-based ZIP containers, which is what makes programmatic manipulation via Python straightforward without Excel installed. The main gotcha to know upfront: formulas written by openpyxl have no cached value until recalculated by actual spreadsheet software, which trips up anyone expecting immediate calculated output.
📑 About the author: I also build Digital Bizz Card — hosted digital business cards you can share with a QR code, no app required.


