The workbook in Python
pz.Workbook holds the sheets, the cells and their formats, and computes the formulas. The
grid only shows it: everything the user does goes into the workbook, and everything your code
writes shows up in the grid at once. It needs no window, so it also works in scripts, servers
and tests, as a small Excel engine.
import pyzed as pz
book = pz.Workbook() # one sheet, "Sheet1"book = pz.Workbook(sheets=["Sales", "Team"]) # named sheetsbook = pz.Workbook.open("report.xlsx") # from a file (see Files)Addresses are written like in the grid: "B2", or "Team!B2" for another sheet. Rows and
columns are 1-based.
book["A1"] = "Name" # textbook["B1"] = 42 # a numberbook["C1"] = "=B1*2" # a formulabook["D1"] = "12%" # read as if typed: the number 0.12, shown as a percentagebook["E1"] = datetime.date(2026, 3, 4)book["F1"] = None # empty (same as del book["F1"])
book["C1"] # 84: the value (a formula gives its result)book.formula("C1") # "=B1*2": what was typedbook.text("D1") # "12%": what the cell showsValues come back as Python types: int or float for numbers (whole numbers are int),
str, bool, datetime.date or datetime.datetime for dates, None for empty cells and
the error text ("#DIV/0!") for errors.
Text you assign is read the way the grid reads typing: numbers, percentages, currencies,
dates and TRUE / FALSE become values, and a leading = makes a formula. Start with an
apostrophe ("'0012") to keep text as it is. book.set("A1", value) and
book.get("A1") are the same as the brackets.
Ranges and records
Section titled “Ranges and records”book.write([ ["Name", "Age"], ["Ada", 36], ["Alan", 41],], at="A1") # rows of values from a cell
book["A1:B3"] # [["Name", "Age"], ["Ada", 36], ["Alan", 41]]book.values() # the whole used range of the active sheetbook.values("A1:B3", formulas=True) # formulas instead of resultsbook.records() # [{"Name": "Ada", "Age": 36}, ...]book.used_range # "A1:B3"
pz.Workbook.from_rows([{"name": "Ada", "age": 36}]) # a book from dicts (header row added)book.write(rows_of_dicts, at="D1") # dicts write a header row toobook.clear("A2:B3") # contents and formats ("contents" or "formats" for one)records() reads the rows below a header row (the first row of the range) as dicts, which is
handy to hand the sheet’s data to the rest of your app, a database or a chart.
Formats and styles
Section titled “Formats and styles”See Formatting for everything. In short:
book.format("A1:B1", bold=True, fill="#e8f0fe")book.format("B2:B9", number_format="$#,##0.00")book.style("A1") # {"bold": True, "fill": "#e8f0fe"}Undo and redo
Section titled “Undo and redo”Every change, from the user or from your code, can be undone.
book.undo() # True if something was undonebook.redo()book.can_undo # for your own buttonsbook.batch(lambda: (book.set("A1", 1), book.set("A2", 2))) # one undo step for bothbatch(fn) runs a function and turns all its changes into one step. Use it for anything
your app does in one click.
The selection
Section titled “The selection”The selection lives in the workbook too, so your code can read it and move it.
book.selection # "B2:D5" (the last range)book.selections # ["B2:D5", "F1"] with Ctrl+clickbook.active_cell # "B2"book.select("A1:C3") # select and scroll therebook.editing # ("B2", "=SU") while the user types, else Nonebook.focused = True # give the grid the keyboardFollowing changes
Section titled “Following changes”book.version # grows with every change: cheap to comparebook.changes() # ["B4", "Team!C2"]: cells changed since the last callA spreadsheet that saves to a database can call changes() from on_change (or a timer)
and write only those cells. book.recalculate() computes all the formulas again, for
NOW(), TODAY(), RAND() and RANDBETWEEN().
Settings
Section titled “Settings”| Attribute | What it does |
|---|---|
book.readonly |
The grid refuses edits (the API still works). |
book.month_first |
Typed dates like 03/04/2026 are month first (True, the default) or day first. |
book.gridlines |
Gridlines of the active sheet. |
book.active |
The active sheet, by index; set it with a name or an index. |
book.size |
(rows, columns) of the active sheet; resize() changes it. |
Helpers
Section titled “Helpers”pz.Workbook.column_name(28) # "AB"pz.Workbook.functions() # [("ABS", "number", "Absolute value."), ...]book.stats("B2:F9") # {"sum": ..., "average": ..., "count": ..., ...}