Python Excel

How to Work with Excel Spreadsheets in Python

Python Excel is the place for these tasks:

  • Read Excel files in Python with openpyxl or xlrd
  • Create workbooks, rename sheets, and write to an Excel file
  • Count, add, or delete sheets in a workbook
  • Change a cell, and write numbers, text, or a formula
  • Save a workbook, save it under a new name, or make a copy
  • Apply fonts, size, bold, and italic text
  • Change row height and column width, and merge or unmerge cells
  • Build charts from worksheet data

Python Excel Automation

If you want to read, write, and manipulate Excel files in Python with short practical examples, start with the openpyxl tutorial on this site, or look at the Python Excel openpyxl course.

Everything you do in Microsoft Excel can be automated with Python. A script can read data, apply rules, and write the result back to a workbook. The sheet stays familiar to the people who use it, while the repetitive work runs in seconds.

A common job is reading rows from one workbook and writing them to a text file, another workbook, or a table you then analyze with pandas. Doing that by hand across hundreds of files takes days. A short Python program does the same pass without skipping a cell.

Python is a widely used programming language, and Excel is still the worksheet most offices share. This site shows how to open, create, read, and modify .xlsx files from Python without keeping Excel open. You will work with workbooks, worksheets, and individual cells.

Office work often means lists of names, amounts, and dates, then copying a slice of that data into another file. Searching, sorting, and cleaning hundreds of workbooks by hand is slow. With openpyxl you write the steps once and rerun them whenever the files change.

The same approach helps data analysis. Spreadsheets are where a lot of source data already lives. Python can read those cells, filter them, and hand the result to pandas or another library. Start with the openpyxl tutorial. It is the usual first library for .xlsx files and the default engine pandas uses to read them.

Python Packages for Excel

Several libraries cover .xls and .xlsx files. These are the ones this site uses.

1. openpyxl

openpyxl, started by Eric Gazoni and Charlie Clark, reads and writes Excel 2010+ files (.xlsx, .xlsm, .xltx, .xltm) without launching Excel. It is open source and is the library most Python Excel tutorials begin with. pandas uses it as the default reader for .xlsx files.

A typical job is opening many workbooks, keeping the rows that match a rule, and writing those rows to a new file. That is a few dozen lines of openpyxl, not a week of copying between windows. Continue with the openpyxl tutorial, then writing cells and charts.

Openpyxl course: read, write, move, copy, and search Excel files

2. xlrd

xlrd reads Excel files on Windows, Linux, and macOS. Current releases read the older .xls format. Use openpyxl or pandas for .xlsx. xlrd does not write workbooks.

3. xlsxwriter

XlsxWriter creates new .xlsx files. It is strong at formatting, formulas, and charts for Excel 2010 and later. It does not open and edit an existing workbook. Use it when you are generating a file from scratch. Use openpyxl when you need to change a file that already exists.

4. xlwt

xlwt writes spreadsheets in the Excel 97–2003 .xls format. It is pure Python. Reach for it only when a downstream system still requires .xls. For new files, write .xlsx with openpyxl or XlsxWriter.

5. xlutils

xlutils is a set of utilities around xlrd and xlwt. The piece people still use is xlutils.copy, which copies an .xls book read by xlrd into a book xlwt can save. It is the older path for editing .xls. For .xlsx, stay with openpyxl.