Skip to content

xl

Read and Write Excel Files.

The vendored xlrd reads both .xlsx and .xls workbooks and xlsxwriter writes them, on IronPython and CPython alike, so both engines return the same values.

Functions:

load(xlfile, sheets=[], columns=[], datatype=None, headers=True)

Read data from Excel file.

Parameters:

Name Type Description Default
xlfile str

full path of the excel file to read

required
sheets list

worksheets to read. Defaults to all the sheets.

[]
columns list

Names to give to the columns. It builds a dictionary for each row with the column name and value. If none given (default), it returns a simple list of values.

[]
datatype type

Type of the data. Defaults to None.

None
headers bool

Whether to use the first row as headers. Defaults to True.

True

Returns:

Type Description
dict[str, dict[str, Any]]

Excel data grouped by worksheet. Each worksheet is a dictionary with headers and rows.

Note

The file is read into memory and closed before parsing, so it can be renamed or deleted as soon as this returns. Opening it by path would leave xlrd's zip handle on .xlsx files open until IronPython's GC runs.

Source code in pyrevitlib/pyrevit/interop/xl.py
def load(xlfile, sheets=[], columns=[], datatype=None, headers=True):
    """Read data from Excel file.

    Args:
        xlfile (str): full path of the excel file to read
        sheets (list, optional): worksheets to read. Defaults to all the sheets.
        columns (list, optional): Names to give to the columns.
            It builds a dictionary for each row with the column name and value.
            If none given (default), it returns a simple list of values.
        datatype (type, optional): Type of the data. Defaults to None.
        headers (bool, optional): Whether to use the first row as headers.
            Defaults to True.

    Returns:
        (dict[str, dict[str, Any]]): Excel data grouped by worksheet.
            Each worksheet is a dictionary with `headers` and `rows`.

    Note:
        The file is read into memory and closed before parsing, so it can be
        renamed or deleted as soon as this returns. Opening it by path would
        leave xlrd's zip handle on .xlsx files open until IronPython's GC runs.
    """
    xldata = {}
    with open(xlfile, "rb") as xlstream:
        xlcontents = xlstream.read()
    xlwb = xlrd.open_workbook(file_contents=xlcontents)
    for xlsheet in xlwb.sheets():
        if sheets:
            if xlsheet.name in sheets:
                xldata[xlsheet.name] = _read_xlsheet(
                    xlsheet, columns=columns, datatype=datatype, headers=headers
                )
        else:
            xldata[xlsheet.name] = _read_xlsheet(
                xlsheet, columns=columns, datatype=datatype, headers=headers
            )
    return xldata

dump(xlfile, datadict)

Write data to Excel file.

Creates a worksheet for each item of the input dictionary.

Parameters:

Name Type Description Default
xlfile str

full path of the target excel file

required
datadict dict[str, list]

dictionary of worksheets names and data

required
Source code in pyrevitlib/pyrevit/interop/xl.py
def dump(xlfile, datadict):
    """Write data to Excel file.

    Creates a worksheet for each item of the input dictionary.

    Args:
        xlfile (str): full path of the target excel file
        datadict (dict[str, list]): dictionary of worksheets names and data
    """
    xlwb = xlsxwriter.Workbook(xlfile)
    # bold = xlwb.add_format({'bold': True})
    for xlsheetname, xlsheetdata in datadict.items():
        xlsheet = xlwb.add_worksheet(xlsheetname)
        for idx, data in enumerate(xlsheetdata):
            xlsheet.write_row(idx, 0, data)
    xlwb.close()