Automate Excel operations including reading, writing, formatting, and formula manipulation
This skill provides comprehensive Excel automation capabilities using Python's openpyxl library.
The skill requires Python 3.7+ with the following packages:
openpyxl - Excel file manipulationpandas - Data analysis and manipulation (optional but recommended)When a user requests Excel automation, follow this process:
Clarify:
The scripts/ directory contains ready-to-use utilities:
excel_reader.py - Read data from Excel filesexcel_writer.py - Write data to new or existing filesexcel_formatter.py - Apply formatting and stylesexcel_formulas.py - Insert and calculate formulasUse the appropriate script with the user's requirements:
# Read data from a worksheet
python scripts/excel_reader.py --file "data.xlsx" --sheet "Sheet1" --range "A1:D10"
# Write data to Excel
python scripts/excel_writer.py --file "output.xlsx" --data "data.json"
# Format cells
python scripts/excel_formatter.py --file "report.xlsx" --format-header
import openpyxl
# Load workbook
wb = openpyxl.load_workbook('data.xlsx')
ws = wb['Sheet1']
# Read data
data = []
for row in ws.iter_rows(min_row=2, values_only=True):
data.append(row)
# Analyze
print(f"Total rows: {len(data)}")
import openpyxl
from openpyxl.styles import Font, Alignment
# Create workbook
wb = openpyxl.Workbook()
ws = wb.active
ws.title = "Report"
# Add header
ws['A1'] = "Report Title"
ws['A1'].font = Font(size=14, bold=True)
ws['A1'].alignment = Alignment(horizontal='center')
# Add data
data = [["Name", "Value"], ["Item 1", 100], ["Item 2", 200]]
for row in data:
ws.append(row)
wb.save('report.xlsx')
import openpyxl
wb = openpyxl.load_workbook('budget.xlsx')
ws = wb['Budget']
# Add SUM formula
ws['D10'] = '=SUM(D2:D9)'
# Add formula for each row
for row in range(2, 10):
ws[f'E{row}'] = f'=C{row}*D{row}'
wb.save('budget.xlsx')
Always handle common errors:
read_only=TrueUser Request: "Read the quarterly sales data from Q4_Sales.xlsx and create a summary report"
Your Response:
excel_reader.py