← All ivy-nodes
IVYXSTUDIO · IVY NODE
E

Excel Read

ivy.node.excel-read · v0.1.0

ivyx✓

Reads one sheet of an Excel workbook into rows with the column names, row count and the workbook's sheet names. Dates come back as ISO text and empty cells as null.

#excel#xlsx#data#table#read

Inputs

FieldTypeDescription
pathrequiredstringPath to the .xlsx file.
sheetstringThe sheet to read. The first one when omitted.

Outputs

FieldTypeDescription
rowsrequiredarrayOne object per row, keyed by column.
columnsrequiredarrayColumn names.
row_countrequiredintegerHow many rows were read.
sheetsrequiredarrayEvery sheet name in the workbook.

Source

python

inp = __ivy_ctx__["nodes"][__ivy_node_id__]["input"]

import json as _json

def frame_rows(frame):
    """A DataFrame as JSON rows: NaN becomes null, dates ISO, numpy plain."""
    return _json.loads(frame.to_json(orient="records", date_format="iso"))

import pandas as pd

path = inp["path"]
sheet = inp.get("sheet")
book = pd.ExcelFile(path)
sheets = [str(s) for s in book.sheet_names]
if sheet is not None and sheet not in sheets:
    raise ValueError(f"No sheet named {sheet!r}; the workbook has {', '.join(sheets)}.")
frame = book.parse(sheet if sheet is not None else sheets[0])

out = __ivy_ctx__["nodes"][__ivy_node_id__]["output"]
out["rows"] = frame_rows(frame)
out["columns"] = [str(c) for c in frame.columns]
out["row_count"] = int(len(frame))
out["sheets"] = sheets

Tests

Requires: python:3.9, pandas, openpyxl

  • second-sheet

    A named sheet reads with its dates as ISO text.

  • unknown-sheet

    A sheet the workbook does not have is named with the ones it has.