---
name: anthropic-xlsx
description: Read, create, and edit Excel spreadsheets using openpyxl. Supports reading/writing data, formatting cells, creating charts, working with formulas, and managing worksheets.
license: Complete terms in LICENSE.txt
---

# XLSX Skill

A skill for reading, creating, and editing Excel (.xlsx) files using openpyxl.

## Installation

```bash
pip install openpyxl
```

## Creating Workbooks

```python
from openpyxl import Workbook
from openpyxl.styles import Font, PatternFill, Alignment, Border, Side
from openpyxl.chart import BarChart, Reference
from openpyxl.utils import get_column_letter

wb = Workbook()
ws = wb.active
ws.title = "Data Sheet"

# Header styling
header_font = Font(bold=True, color="FFFFFF", size=12)
header_fill = PatternFill(start_color="366092", end_color="366092", fill_type="solid")
header_alignment = Alignment(horizontal="center", vertical="center")

# Add headers
headers = ["Name", "Value", "Category"]
for col, header in enumerate(headers, 1):
    cell = ws.cell(row=1, column=col, value=header)
    cell.font = header_font
    cell.fill = header_fill
    cell.alignment = header_alignment

# Add data
data = [
    ["Item A", 100, "Category 1"],
    ["Item B", 200, "Category 2"],
    ["Item C", 150, "Category 1"],
]
for row_idx, row_data in enumerate(data, 2):
    for col_idx, value in enumerate(row_data, 1):
        ws.cell(row=row_idx, column=col_idx, value=value)

# Add a chart
chart = BarChart()
chart.title = "Values by Item"
chart.y_axis.title = "Value"
data_ref = Reference(ws, min_col=2, min_row=1, max_row=len(data) + 1)
cats_ref = Reference(ws, min_col=1, min_row=2, max_row=len(data) + 1)
chart.add_data(data_ref, titles_from_data=True)
chart.set_categories(cats_ref)
ws.add_chart(chart, "E2")

# Auto-adjust column widths
for col in range(1, len(headers) + 1):
    ws.column_dimensions[get_column_letter(col)].width = 15

wb.save("workbook.xlsx")
```

## Reading Workbooks

```python
from openpyxl import load_workbook

wb = load_workbook("workbook.xlsx", data_only=True)
for sheet_name in wb.sheetnames:
    ws = wb[sheet_name]
    print(f"=== {sheet_name} ===")
    for row in ws.iter_rows(values_only=True):
        print(row)
```

## Best Practices

- Use `data_only=True` to read computed values instead of formulas
- Use `write_only=True` for creating large workbooks (lower memory usage)
- Always close workbooks after editing
- Use styles consistently for professional output
- For large datasets, consider using pandas with openpyxl as the engine
