Excel File Manipulation
Create, edit, and manipulate Excel spreadsheets programmatically using openpyxl
Manually updating 50 Excel files with new formulas, formatting, and data transformations would take all week. This playbook uses openpyxl to programmatically create, edit, and manipulate Excel spreadsheets — from bulk formula insertion to cross-workbook data merging.
Who it's for: data analysts automating repetitive Excel file transformations and report updates, finance teams programmatically generating formatted Excel reports from raw data, operations managers merging data from multiple Excel files into consolidated views, business analysts building automated Excel reporting pipelines, administrative staff batch-updating Excel templates with new data each period
Example
"Merge data from 30 regional Excel files into one summary workbook" → Excel manipulation pipeline: multi-file ingestion reading all regional workbooks, data extraction and normalization across different column layouts, merged summary worksheet with aggregated metrics by region, formatted output with headers, borders, and conditional highlighting, and automated chart generation from the consolidated data
CLAUDE.md Template
New here? 3-minute setup guide → | Already set up? Copy the template below.
npx ccplaybooks add xlsx-manipulation# XLSX Manipulation
## Overview
This workflow enables programmatic creation, editing, and manipulation of Microsoft Excel (.xlsx) spreadsheets using the **openpyxl** library. Create professional spreadsheets with formulas, formatting, charts, and data validation without manual editing.
## How to Use
1. Describe the spreadsheet you want to create or modify
2. Provide data, formulas, or formatting requirements
3. I'll generate openpyxl code and execute it
Get new playbooks like this one
One email a week with new Claude Code workflows. Free, like everything here.
No spam. Unsubscribe anytime.
What This Does
This workflow enables programmatic creation, editing, and manipulation of Microsoft Excel (.xlsx) spreadsheets using the openpyxl library. Create professional spreadsheets with formulas, formatting, charts, and data validation without manual editing.
Quick Start
Step 1: Create a Project Folder
mkdir -p ~/Documents/XlsxManipulation
Step 2: Download the Template
Click Download above, then:
mv ~/Downloads/CLAUDE.md ~/Documents/XlsxManipulation/
Step 3: Start Working
cd ~/Documents/XlsxManipulation
claude
How to Use
- Describe the spreadsheet you want to create or modify
- Provide data, formulas, or formatting requirements
- I'll generate openpyxl code and execute it
Example prompts:
- "Create a budget spreadsheet with monthly tracking"
- "Add conditional formatting to highlight values above threshold"
- "Generate a pivot-table-like summary from this data"
- "Create a dashboard with charts and KPIs"
Best Practices
- Use Templates: Start with a .xlsx template for complex formatting
- Batch Operations: Minimize cell-by-cell operations for speed
- Named Ranges: Use defined names for clearer formulas
- Data Validation: Add validation to prevent input errors
- Save Incrementally: For large files, save periodically
Examples
Example 1: Budget Tracker
from openpyxl import Workbook
from openpyxl.styles import Font, PatternFill, Alignment, Border, Side
from openpyxl.utils import get_column_letter
wb = Workbook()
ws = wb.active
ws.title = "Budget 2024"
# Headers
months = ['Category', 'Jan', 'Feb', 'Mar', 'Q1 Total']
ws.append(months)
# Categories and data
budget_data = [
['Salary', 5000, 5000, 5000],
['Rent', -1500, -1500, -1500],
['Utilities', -200, -180, -220],
['Food', -400, -450, -380],
['Transport', -150, -160, -140],
['Entertainment', -200, -250, -200],
]
for row in budget_data:
ws.append(row + [f'=SUM(B{ws.max_row + 1}:D{ws.max_row + 1})'])
# Total row
ws.append(['TOTAL',
f'=SUM(B2:B{ws.max_row})',
f'=SUM(C2:C{ws.max_row})',
f'=SUM(D2:D{ws.max_row})',
f'=SUM(E2:E{ws.max_row})'
])
# Formatting
header_fill = PatternFill('solid', fgColor='366092')
header_font = Font(bold=True, color='FFFFFF')
for cell in ws[1]:
cell.fill = header_fill
cell.font = header_font
cell.alignment = Alignment(horizontal='center')
# Currency format
for row in ws.iter_rows(min_row=2, min_col=2, max_col=5):
for cell in row:
cell.number_format = '$#,##0.00'
# Column widths
ws.column_dimensions['A'].width = 15
for col in range(2, 6):
ws.column_dimensions[get_column_letter(col)].width = 12
wb.save('budget_2024.xlsx')
Example 2: Sales Dashboard
from openpyxl import Workbook
from openpyxl.chart import BarChart, PieChart, Reference
from openpyxl.styles import Font, PatternFill
wb = Workbook()
ws = wb.active
ws.title = "Sales Dashboard"
# Data
ws.append(['Region', 'Q1', 'Q2', 'Q3', 'Q4'])
data = [
['North', 150000, 165000, 180000, 195000],
['South', 120000, 125000, 140000, 155000],
['East', 180000, 190000, 210000, 225000],
['West', 95000, 110000, 125000, 140000],
]
for row in data:
ws.append(row)
# Bar Chart
data_ref = Reference(ws, min_col=2, min_row=1, max_col=5, max_row=5)
cats_ref = Reference(ws, min_col=1, min_row=2, max_row=5)
bar = BarChart()
bar.type = "col"
bar.title = "Quarterly Sales by Region"
bar.add_data(data_ref, titles_from_data=True)
bar.set_categories(cats_ref)
bar.height = 10
bar.width = 15
ws.add_chart(bar, "A8")
# Pie Chart - Q4 breakdown
pie_data = Reference(ws, min_col=5, min_row=1, max_row=5)
pie = PieChart()
pie.title = "Q4 Market Share"
pie.add_data(pie_data, titles_from_data=True)
pie.set_categories(cats_ref)
ws.add_chart(pie, "J8")
wb.save('sales_dashboard.xlsx')
Limitations
- Cannot execute VBA macros
- Complex pivot tables not fully supported
- Limited sparkline support
- External data connections not supported
- Some advanced chart types unavailable
Installation
pip install openpyxl