Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Engines available for to_excel function in pandas

Whenever we save a dataframe to an excel file (.xlsx) in pandas using to_excel function, we are given an option to specify the engine using which we want can complete the process. My question is what all are the engines available for this?. For e.g. one engine is "xlsxwriter"; similarly what are the other options available?

like image 381
Aishwary Shukla Avatar asked Jul 28 '18 11:07

Aishwary Shukla


People also ask

How do I write multiple sheets in pandas?

To write to multiple sheets it is necessary to create an ExcelWriter object with a target file name, and specify a sheet in the file to write to. Multiple sheets may be written to by specifying unique sheet_name . With all data written to the file it is necessary to save the changes.

How can I read XLSX file in pandas?

pandas. read_excel() function is used to read excel sheet with extension xlsx into pandas DataFrame. By reading a single sheet it returns a pandas DataFrame object, but reading two sheets it returns a Dict of DataFrame. Can load excel files stored in a local filesystem or from an URL.

What is PD ExcelWriter?

pandas ExcelWriter() class is used to save DataFrame to Excel sheet. This class is mainly used when you wanted to save multiple sheets and append data to an existing Excel sheet. pandas ExcelWriter Key Points. By default, it uses xlsxwriter if it is installed otherwise it uses openpyxl.

What does writer Save () do?

writer. save has no arguments (the 1 argument is self ). Calling it will save to the earlier specified filename.


1 Answers

Pandas defines default writers for Excel, see pandas/io/excel/_util.py:

def _get_default_writer(ext):
    """
    Return the default writer for the given extension.
    Parameters
    ----------
    ext : str
        The excel file extension for which to get the default engine.
    Returns
    -------
    str
        The default engine for the extension.
    """
    _default_writers = {
        "xlsx": "openpyxl",
        "xlsm": "openpyxl",
        "xls": "xlwt",
        "ods": "odf",
    }
    xlsxwriter = import_optional_dependency(
        "xlsxwriter", raise_on_missing=False, on_version="warn"
    )
    if xlsxwriter:
        _default_writers["xlsx"] = "xlsxwriter"
    return _default_writers[ext]

So the default for extension xlsx will be xlsxwriter, if you have the module installed, otherwise openpyxl.

like image 72
jpp Avatar answered Oct 17 '22 18:10

jpp