Skip to content

Excel

Polars can read and write to Excel files from Python. From a performance perspective, we recommend using other formats if possible, such as Parquet or CSV files.

Read

Polars does not have a native Excel reader. Instead, it uses external libraries to parse Excel files into objects that Polars can parse. To read Excel files, we must install either the (default) xlsx2csv library or one of the alternatives as an additional dependency.

$ pip install xlsx2csv openpyxl pyxlsb

The default Excel reader is xlsx2csv. It is a Python library which parses the Excel file into a CSV file which Polars then reads with the native CSV reader. We read an Excel file with read_excel:

read_excel

df = pl.read_excel("docs/data/path.xlsx")

We can specify the sheet name to read with the sheet_name argument. If we do not specify a sheet name, the first sheet will be read.

read_excel

df = pl.read_excel("docs/data/path.xlsx", sheet_name="Sales")

Write

We need the xlswriter library installed as an additional dependency to write to Excel files.

$ pip install xlsxwriter

Writing to Excel files is not currently available in Rust Polars, though it is possible to use this crate to write to Excel files from Rust.

Writing a DataFrame to an Excel file is done with the write_excel method:

write_excel

df = pl.DataFrame({"foo": [1, 2, 3], "bar": [None, "bak", "baz"]})
df.write_excel("docs/data/path.xlsx")

The name of the worksheet can be specified with the worksheet argument.

write_excel

df = pl.DataFrame({"foo": [1, 2, 3], "bar": [None, "bak", "baz"]})
df.write_excel("docs/data/path.xlsx", worksheet="Sales")

Polars can create rich Excel files with multiple sheets and formatting. For more details, see the API docs for write_excel.