PyExcel
PyExcel is a wrapper library that provides a unified API to read, manipulate, and write data in various Excel formats such as CSV, XLS, XLSX, and ODS. It focuses purely on data processing, enabling easy conversion of Excel data into Python arrays or dictionaries and vice versa, without supporting features like fonts, colors, or charts. The library is actively maintained, with frequent minor releases.
Warnings
- breaking Python 2 compatibility was permanently removed in v0.6.3.
- breaking Explicit imports like `import pyexcel.ext.*` were removed. PyExcel now auto-loads installed plugins.
- gotcha PyExcel explicitly does NOT support fonts, styles, formulas, or charts. Saving an Excel file will strip these elements.
- gotcha Core `pyexcel` only handles CSV and TSV natively. Support for other formats (XLS, XLSX, ODS, etc.) requires installing additional `pyexcel-xxx` plugin libraries.
- gotcha If multiple plugins are installed for the same file format (e.g., `pyexcel-ods` and `pyexcel-ods3`), you may need to explicitly specify which plugin to use.
Install
-
pip install pyexcel -
pip install pyexcel-xls pyexcel-xlsx pyexcel-ods3
Imports
- pyexcel
import pyexcel
Quickstart
import pyexcel
from collections import OrderedDict
# --- Writing data to an Excel file (e.g., 'your_file.xlsx') ---
data = OrderedDict()
data.update({"Sheet 1": [[1, 2, 3], [4, 5, 6]]})
data.update({"Sheet 2": [['a', 'b', 'c'], ['d', 'e', 'f']]})
# Ensure pyexcel-xlsx is installed: pip install pyexcel-xlsx
pyexcel.save_book_as(bookdict=data, dest_file_name="your_file.xlsx")
print("Data written to your_file.xlsx")
# --- Reading data from an Excel file ---
# Ensure pyexcel-xlsx is installed: pip install pyexcel-xlsx
sheet = pyexcel.get_sheet(file_name="your_file.xlsx")
print(f"Content of Sheet 1 (as array): {sheet.array}")
# You can also get the whole book as a dictionary of sheets
book_dict = pyexcel.get_book_dict(file_name="your_file.xlsx")
print(f"Content of the entire book: {book_dict}")