class pandas.ExcelWriter(path, engi
class pandas.ExcelWriter(path, engi
Default is to use:
The writer should be used as a context manager. Otherwise, call close() to save and
close any opened file handles.
Parameters:
pathstr or typing.BinaryIO
Path to xls or xlsx or ods file.
enginestr (optional)
Engine to use for writing. If None, defaults to io.excel.<extension>.writer. NOTE:
can only be passed as a keyword argument.
storage_optionsdict, optional
Extra options that make sense for a particular storage connection, e.g. host, port,
username, password, etc. For HTTP(S) URLs the key-value pairs are forwarded to
urllib.request.Request as header options. For other URLs (e.g. starting with
“s3://”, and “gcs://”) the key-value pairs are forwarded to fsspec.open. Please see
fsspec and urllib for more details, and for more examples on storage options refer
here.
overlay: Write contents to the existing sheet without first removing, but possibly
over top of, the existing contents.
engine_kwargsdict, optional
Keyword arguments to be passed into the engine. These will be passed to the
following functions of the respective engines:
odswriter: odf.opendocument.OpenDocumentSpreadsheet(**engine_kwargs)
Notes
For compatibility with CSV writers, ExcelWriter serializes lists and dicts to
strings before writing.
Examples
Default usage:
with ExcelWriter(
"path_to_file.xlsx",
mode="a",
engine="openpyxl",
if_sheet_exists="replace",
) as writer:
df.to_excel(writer, sheet_name="Sheet1")
You can also write multiple DataFrames to a single sheet. Note that the
if_sheet_exists parameter needs to be set to overlay:
with ExcelWriter("path_to_file.xlsx",
mode="a",
engine="openpyxl",
if_sheet_exists="overlay",
) as writer:
df1.to_excel(writer, sheet_name="Sheet1")
df2.to_excel(writer, sheet_name="Sheet1", startcol=3)
You can store Excel file in RAM:
import io
df = pd.DataFrame([["ABC", "XYZ"]], columns=["Foo", "Bar"])
buffer = io.BytesIO()
with pd.ExcelWriter(buffer) as writer:
df.to_excel(writer)
You can pack Excel file into zip archive:
import zipfile
df = pd.DataFrame([["ABC", "XYZ"]], columns=["Foo", "Bar"])
with zipfile.ZipFile("path_to_file.zip", "w") as zf:
with zf.open("filename.xlsx", "w") as buffer:
with pd.ExcelWriter(buffer) as writer:
df.to_excel(writer)
You can specify additional arguments to the underlying engine:
with pd.ExcelWriter(
"path_to_file.xlsx",
engine="xlsxwriter",
engine_kwargs={"options": {"nan_inf_to_errors": True}}
) as writer:
df.to_excel(writer)
In append mode, engine_kwargs are passed through to openpyxl’s load_workbook:
with pd.ExcelWriter(
"path_to_file.xlsx",
engine="openpyxl",
mode="a",
engine_kwargs={"keep_vba": True}
) as writer:
df.to_excel(writer, sheet_name="Sheet2")
Attributes
book
Book instance.
date_format
Format string for dates written into Excel files (e.g. 'YYYY-MM-DD').
datetime_format
Format string for dates written into Excel files (e.g. 'YYYY-MM-DD').
engine
Name of engine.
if_sheet_exists
How to behave when writing to a sheet that already exists in append mode.
sheets
supported_extensions
Methods
check_extension(ext)
close()