← 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
| Field | Type | Description |
|---|---|---|
| pathrequired | string | Path to the .xlsx file. |
| sheet | string | The sheet to read. The first one when omitted. |
Outputs
| Field | Type | Description |
|---|---|---|
| rowsrequired | array | One object per row, keyed by column. |
| columnsrequired | array | Column names. |
| row_countrequired | integer | How many rows were read. |
| sheetsrequired | array | Every 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"] = sheetsTests
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.