Skip to content

Spreadsheet (pz.Workbook, ui.Spreadsheet)

A spreadsheet like Google Sheets or Excel. pz.Workbook holds the data and computes the formulas, pz.sheet draws the grid and ui.Spreadsheet adds a toolbar, menus and sheet tabs. See the Spreadsheet section.

book = pz.Workbook()
book["A1"], book["A2"], book["A3"] = 2, 3, "=A1*A2"
ui.Spreadsheet(book=book)
class Workbook

A spreadsheet workbook: sheets of cells with values, formulas and formats.

Read and write cells by address (book["B2"] = "=SUM(A1:A9)"; book["A1:C3"] gives rows of values), format them, sort, filter, undo, and open or save CSV and Excel files. Show it (and edit it) with ui.Spreadsheet(book=book) or pz.sheet(book). Addresses may name a sheet ("Sales!A1"); without one they use the active sheet. Row and column numbers are 1-based, as in the grid; columns are also accepted as letters ("C").

book = pz.Workbook(sheets=["Sales", "Team"])
book["Sales!B2"] = "=SUM(C2:C9)"
book.format("A1:G1", bold=True, fill="#e8f0fe")
book.save("report.xlsx")

Every method is described in The workbook in Python, Formatting, Sort, filter and validate, Rows, columns and sheets and Files; help(pz.Workbook) lists them with their docstrings.

Methods and properties

@staticmethod
Workbook.open(path)

Opens a file: .xlsx, .xlsm, .xls, .ods, .csv, .tsv or .txt.

@staticmethod
Workbook.from_bytes(data)

A workbook from an Excel or OpenDocument file’s bytes.

@staticmethod
Workbook.from_csv(text, delimiter=None)

A workbook from CSV (or TSV) text. delimiter=None guesses it.

@staticmethod
Workbook.from_rows(rows, *, header=None, sheet=None)

A workbook from rows of values (lists, or dicts whose keys become the header row).

@staticmethod
Workbook.functions()

The functions formulas can use: [(name, arguments, description), ...].

@staticmethod
Workbook.column_name(col)

The column letters of a 1-based number (column_name(28) == "AB").

class Spreadsheet

A component: the whole spreadsheet, with the grid and its formula bar, a toolbar, right-click menus, a find bar, panels for conditional formatting, validation and notes, sheet tabs and a status bar with the selection’s sum. Everything the user does goes to book. See The Spreadsheet component.

Parameter Description
book The pz.Workbook to show and edit (a new empty one when None). Keep it in your app so its data survives view rebuilds.
toolbar Shows the toolbar.
formula_bar Shows the name box and the formula bar.
tabs Shows the sheet tabs and the status bar.
headers Shows the column letters and row numbers.
readonly Users can select, copy and find, but not change anything.
text_size Pixel size of 10 pt text.
width Width: pixels, "fill", "shrink" or "fill:N" (proportion).
height Height: pixels, "fill", "shrink" or "fill:N" (proportion).
on_change Receives event.value with action, range and sheet when the user changes something.
on_select Receives event.value with range and sheet when the selection changes.
ui.Spreadsheet(book=self.book, on_change=lambda e: print(e.value.action, e.value.range))
sheet(
book,
*,
colors=None,
text_size=13.0,
formula_bar=True,
headers=True,
width=None,
height=None,
readonly=False,
on_change=None,
on_select=None,
on_context=None,
on_dropdown=None,
on_filter=None,
on_find=None,
on_invalid=None,
on_edit=None,
)

The spreadsheet grid on its own: cells, headers and formula bar, with selection, editing, formulas, the fill handle, copy and paste and the keyboard shortcuts of Sheets and Excel. It draws only the cells in view. The grid doesn’t draw menus: its events say where the user clicked. See The grid on its own.

Parameter Description
book The pz.Workbook to show.
colors A dict of colors (background, grid, header, text, accent, selection…); ui.sheet_colors(theme) makes one from a theme.
text_size Pixel size of 10 pt text.
formula_bar Shows the name box and the formula bar.
headers Shows the column letters and row numbers.
width Width: pixels, "fill", "shrink" or "fill:N" (proportion).
height Height: pixels, "fill", "shrink" or "fill:N" (proportion).
readonly Select and copy only.
on_change Called with a dict when the user changes cells, formats or the structure: action ("edit", "paste", "fill", "clear", "format", "sort", "undo"…), range and sheet.
on_select Called with a dict when the selection changes (read book.selection).
on_context Called on a right click with kind ("cell", "row" or "column"), cell, x and y (relative to the grid): show a menu there.
on_dropdown Called when a list cell’s dropdown should open: cell, options, x, y, width.
on_filter Called when a filter button of the header is clicked: column, x, y.
on_find Called on Ctrl+F or Ctrl+H: mode ("find" or "replace").
on_invalid Called when a typed value breaks a strict validation rule: cell, value, message.
on_edit Called when typing in a cell starts or ends: editing.
pz.sheet(self.book, formula_bar=False, on_change=lambda e: print(e["action"], e["range"]))
sheet_colors(theme=None)

The colors ui.Spreadsheet gives its grid for a theme, as the dict pz.sheet(colors=...) takes.

Parameter Description
theme A theme (the current one by default).