# Formatting

> Format cells in a Pyzed spreadsheet — bold, colors, borders, alignment, wrapping, merged cells, Excel number formats for currency, percentages and dates, and conditional formatting with color scales and data bars.

Source: https://albakore.github.io/pyzed/spreadsheet/formatting/

(Image: Cells being formatted from the toolbar: bold, a fill color, currency, borders and a merged title)

Everything on the toolbar of [`ui.Spreadsheet`](https://albakore.github.io/pyzed/spreadsheet/component/) is also one
call of `book.format()`, so your code can prepare a sheet that looks finished before the user
sees it. Formats are saved to `.xlsx` files and read back from them.

## Cell styles

```python
book.format("A1:G1", bold=True, fill="#e8f0fe", borders="bottom")
book.format("E2:F12", number_format="$#,##0.00", align="right")
book.format("A14", italic=True, color="#5f6368", size=9, wrap=True)
```

| Argument | Values |
| --- | --- |
| `bold`, `italic`, `underline`, `strike` | `True` / `False`. |
| `color` | The text color, `"#rrggbb"` (or a color name such as `"red"`). |
| `fill` | The background color. |
| `size` | The font size in points (10 is the default, like Sheets). |
| `mono` | A monospaced font, for codes and IDs. |
| `align` | `"left"`, `"center"` or `"right"`. Without it, numbers go right, text left and `TRUE` / `FALSE` center, like in every spreadsheet. |
| `valign` | `"top"`, `"middle"` or `"bottom"` (the default). |
| `wrap` | `True` breaks long text into lines (double-click the border under the row number to make the row fit them, or set `book.row_height()`). Without it, text spills over empty cells to the right. |
| `number_format` | An Excel format code (next section). `"General"` removes it. |
| `borders` | `"all"`, `"outer"`, `"inner"`, `"none"` or sides like `"top bottom"`. |
| `border_color` | The color of the borders. |

Arguments you leave out are not touched, so you can add bold without losing the fill. Read a
cell's style back with `book.style("A1")` (a dict with only what is set) and clear formats
with `book.clear("A1:C9", "formats")`.

Text without its own color is drawn in the theme's text color, or in black on a light fill
when the theme is dark, so a light header row stays readable in both themes.

## Number formats

A number format decides how a value is **shown**; the value itself does not change. The
examples show 1234.5 unless they say otherwise.
`book["B2"]` is still `0.5` when the cell shows `$0.50`.

| Format code | Shows | Used for |
| --- | --- | --- |
| `#,##0.00` | `1,234.50` | Numbers with thousands separators. |
| `0` / `0.0` | `1235` / `1234.5` | Fixed decimals. |
| `$#,##0.00` | `$1,234.50` | Currency (any symbol: `€#,##0.00`, `#,##0.00 €`). |
| `0%` / `0.00%` | `25%` / `25.00%` for 0.25 | Percentages. |
| `0.00E+00` | `1.23E+03` | Scientific. |
| `#,##0.00;(#,##0.00)` | `1,234.50`, and `(1,234.50)` when negative | Accounting: a second section for negatives. |
| `0.00;[Red]-0.00` | `1234.50`, negatives in red | Colors per section: `[Red]`, `[Blue]`, `[Green]`... |
| `yyyy-mm-dd` | `2026-03-04` for that date | Dates (`dd/mm/yyyy`, `d mmm yyyy`, `mmmm d`...). |
| `h:mm AM/PM`, `hh:mm:ss` | `2:30 PM`, `14:30:00` for 14:30 | Times. |
| `[h]:mm:ss` | `30:00:00` for 1.25 days | Durations over 24 hours. |
| `"Total: "0` | `Total: 1235` | Text around the number. |
| `@` | as typed | Plain text: what is typed is never turned into a number. |

Typing `$12`, `12%` or a date sets a matching format by itself, like Excel. The toolbar
buttons <kbd>$</kbd>, <kbd>%</kbd> and the decimals buttons apply the common ones, and
`book.change_decimals("B2:B9", 1)` adds or removes a decimal.

## Merged cells

```python
book["A1"] = "Quarterly report"
book.merge("A1:F1")
book.format("A1", align="center", bold=True, size=14)
book.merges()     # ["A1:F1"]
book.unmerge("A1:F1")
```

A merged range shows the content of its top-left cell across all of it. Selecting any part
of it selects all of it, and the arrows jump over it.

## Conditional formatting

Conditional formats color cells by their value, and update when the values change.

```python
book.conditional_format("F2:F9", ">", 150, fill="#fce8b2")
book.conditional_format("C2:C9", "contains", "Pears", color="#188038", bold=True)
book.conditional_format("D2:D9", "data_bar", fill="#a8dab5")
book.conditional_format("E2:E9", "color_scale", colors=["#f4c7c3", "#ffffff", "#b7e1cd"])
book.conditional_format("A2:G9", "formula", "=$G2=FALSE", color="#9aa0a6")
```

| Rule | Colors the cells that |
| --- | --- |
| `">"`, `"<"`, `">="`, `"<="`, `"="`, `"<>"` (or `"greater"`, `"less"`...) | Compare with `value`. |
| `"between"`, `"not_between"` | Are between `value` and `value2`. |
| `"contains"`, `"not_contains"`, `"starts_with"`, `"ends_with"` | Have a text (case does not matter). |
| `"empty"`, `"not_empty"` | Are empty or not. |
| `"duplicate"`, `"unique"` | Repeat a value of the range, or don't. |
| `"formula"` | Make a formula TRUE. Write it for the first cell of the range; it moves to the others like a copied formula (`$G2` keeps the column). |
| `"color_scale"` | All of them: a color from low to high, with `colors=[low, high]` or `[low, middle, high]`. |
| `"data_bar"` | All of them: a bar as long as the value, in the `fill` color. |

The style of a rule can set `color`, `fill`, `bold`, `italic`, `underline` and `strike`.
When several rules match a cell, the first one added wins for each property.
`book.conditional_formats()` lists them as `(range, rule)` and
`book.clear_conditional_formats("F2:F9")` removes the ones that touch a range.

In [`ui.Spreadsheet`](https://albakore.github.io/pyzed/spreadsheet/component/) they are edited in the
**Conditional formatting** panel (the brush on the toolbar).

## Gridlines

`book.gridlines = False` hides the gridlines of the active sheet, to make a form or a report
that doesn't look like a grid.
