Skip to content

hertzforge.io.excel

excel

Read and write channel tables as Excel workbooks (.xlsx, .xlsm).

A channel table is rows of text for each channel, which is what a spreadsheet holds, so it has a spreadsheet format. Every column is written in order under the header the window and the report use, the 'channel' column and other integer columns as numbers and the rest as text, with drop-down lists for the columns that have fixed choices and a unit list that follows each channel's type. Units in a channel table are labels, not values, so there is nothing to convert, and unit_system is accepted and not used.

Reading is as important as writing, because a channel table more often arrives as a spreadsheet than as anything else: a calibration lab sends one, and a controller writes one.

Functions:

Name Description
save

Write a channel table to path as a one-sheet workbook.

sniff

True for an .xlsx or .xlsm workbook holding a channel table.

load

Read the active sheet of a workbook as a ChannelTable.

read_columns

The columns load makes its table from: each headed column's text

Classes

Functions:

save

save(table: ChannelTable, path: str | PathLike, unit_system: UnitSystem | None = None, **_kwargs: Any) -> None

Write a channel table to path as a one-sheet workbook.

Every column goes out under the header the window uses, integer columns as numbers, with drop-down lists on the columns that have fixed choices and each column widened to its longest entry. unit_system is accepted and not used: a channel table's units are labels.

sniff

sniff(path: str | PathLike) -> bool

True for an .xlsx or .xlsm workbook holding a channel table.

The suffix is not enough, since a workbook may hold anything, so this opens the file and looks in the first rows of the active sheet for a header row naming a channel column. A file that cannot be opened is not a channel table. This is asked of every file dropped on the window, so a wrong answer is costly.

load

load(path: str | PathLike, **_kwargs: Any) -> ChannelTable

Read the active sheet of a workbook as a ChannelTable.

The header is the first of the top ten rows with a cell naming the channel column (such as 'Channel', 'Channel Number' or 'Ch'), so a controller's sheet with group headings above the column names reads too. Each header is matched to the channel table's column names by canonical_name (case, spacing, a parenthetical unit and the known aliases do not matter), and a column the channel table does not have is dropped, as it is from any source. A column named twice keeps the first. Values are read as text, because that is what the object stores: a serial number of all digits is not a number, and one with a leading zero would read back as a different string if it were.

Blank rows are skipped, and a column with a blank header is left out. A node, direction or unit column the sheet does not have is created blank.

A header with no channels under it reads as an empty table, which is what an empty table writes.

Raises ValueError when no header row is found.

read_columns

read_columns(path: str | PathLike) -> dict[str, list[str]]

The columns load makes its table from: each headed column's text under its canonical_name, whether the channel table has it or not, and the four the table needs blank where the sheet does not have them.

Raises ValueError when no header row is found.