# Your first workbook

In this lesson you will write `sales.xlsx`: a header row, three data rows, formatted revenue and a frozen header.

## The script

```
from xlspy import F, Style, XlsxWriter

rows = [
    ["Product", "Units", "Revenue"],
    ["Coffee", 120, (12500, F.THOUSANDS_SEP)],
    ["Tea", 90, (8400, F.THOUSANDS_SEP)],
    ["Juice", 45, (5100, F.THOUSANDS_SEP)],
]

with XlsxWriter("sales.xlsx") as workbook:
    workbook.add_sheet("Sales")
    workbook.write_sheet(
        rows,
        header_style=Style(bold=True, fill="#183C52", font_color="#FFFFFF"),
        freeze_panes="A2",
    )

print("wrote sales.xlsx")
```

Run it:

```
python lesson.py
```

You now have a workbook with one sheet:

| Product | Units | Revenue |
| :--- | ---: | ---: |
| Coffee | 120 | 12,500 |
| Tea | 90 | 8,400 |
| Juice | 45 | 5,100 |

## What happened

- `XlsxWriter("sales.xlsx")` opens a workbook. The `with` block saves and publishes the file when it exits.
- `add_sheet("Sales")` adds a worksheet.
- `write_sheet(rows)` consumes the rows once, row by row.
- A `(value, format)` tuple applies a number format to that cell. `F.THOUSANDS_SEP` is Excel's `#,##0` format.
- `header_style` styles every cell in the first row.
- `freeze_panes="A2"` keeps row 1 visible while scrolling.

> [!NOTE]
>
> **Plain strings stay text**
>
> A string that starts with `=` is written as text, never as a formula. Use `Formula` when you want a formula; you will do that in [Formulas, tables and charts](<https://justybase.github.io/xlspy/learn/objects/index.md>).

## Common variations

**Write to a file-like object** instead of a path:

```
import io

buffer = io.BytesIO()
with XlsxWriter(buffer) as workbook:
    workbook.add_sheet("Sales")
    workbook.write_sheet(rows)
data = buffer.getvalue()
```

**Add a second sheet**:

```
with XlsxWriter("sales.xlsx") as workbook:
    workbook.add_sheet("Sales")
    workbook.write_sheet(rows)
    workbook.add_sheet("Notes", hidden=True)
    workbook.write_sheet([["Generated by xlspy"]])
```

**Use a smaller file** with inline strings when you have many distinct strings:

```
with XlsxWriter("sales.xlsx", use_shared_strings=False) as workbook:
    workbook.add_sheet("Sales")
    workbook.write_sheet(rows)
```

> [!WARNING]
>
> **A failed write publishes nothing**
>
> If the input raises or a value is refused, the target file is left as it was. See [Failure handling](<https://justybase.github.io/xlspy/guide/writing/index.md#failure-handling>).

[Next: XLSB in one line →](<https://justybase.github.io/xlspy/learn/xlsb/index.md>)
