Excel analysis skill
Analyze Excel spreadsheets, create pivot tables, generate charts, and perform data analysis.
by davila7·MIT license·★ 32,299 Stars on the repo·GitHub ↗
Use now
npx degit davila7/claude-code-templates/cli-tool/components/skills/enterprise-communication/excel-analysis#main ~/.claude/skills/excel-analysisChecked ·commit main
Files of Excel analysis
SKILL.md
Show the full text248 lines
Excel Analysis
Quick start
Read Excel files with pandas:
import pandas as pd
# Read Excel file
df = pd.read_excel("data.xlsx", sheet_name="Sheet1")
# Display first few rows
print(df.head())
# Basic statistics
print(df.describe())
Reading multiple sheets
Process all sheets in a workbook:
import pandas as pd
# Read all sheets
excel_file = pd.ExcelFile("workbook.xlsx")
for sheet_name in excel_file.sheet_names:
df = pd.read_excel(excel_file, sheet_name=sheet_name)
print(f"\n{sheet_name}:")
print(df.head())
Data analysis
Perform common analysis tasks:
import pandas as pd
df = pd.read_excel("sales.xlsx")
# Group by and aggregate
sales_by_region = df.groupby("region")["sales"].sum()
print(sales_by_region)
# Filter data
high_sales = df[df["sales"] > 10000]
# Calculate metrics
df["profit_margin"] = (df["revenue"] - df["cost"]) / df["revenue"]
# Sort by column
df_sorted = df.sort_values("sales", ascending=False)
Creating Excel files
Write data to Excel with formatting:
import pandas as pd
df = pd.DataFrame({
"Product": ["A", "B", "C"],
"Sales": [100, 200, 150],
"Profit": [20, 40, 30]
})
# Write to Excel
writer = pd.ExcelWriter("output.xlsx", engine="openpyxl")
df.to_excel(writer, sheet_name="Sales", index=False)
# Get worksheet for formatting
worksheet = writer.sheets["Sales"]
# Auto-adjust column widths
for column in worksheet.columns:
max_length = 0
column_letter = column[0].column_letter
for cell in column:
if len(str(cell.value)) > max_length:
max_length = len(str(cell.value))
worksheet.column_dimensions[column_letter].width = max_length + 2
writer.close()
Pivot tables
Create pivot tables programmatically:
import pandas as pd
df = pd.read_excel("sales_data.xlsx")
# Create pivot table
pivot = pd.pivot_table(
df,
values="sales",
index="region",
columns="product",
aggfunc="sum",
fill_value=0
)
print(pivot)
# Save pivot table
pivot.to_excel("pivot_report.xlsx")
Charts and visualization
Generate charts from Excel data:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_excel("data.xlsx")
# Create bar chart
df.plot(x="category", y="value", kind="bar")
plt.title("Sales by Category")
plt.xlabel("Category")
plt.ylabel("Sales")
plt.tight_layout()
plt.savefig("chart.png")
# Create pie chart
df.set_index("category")["value"].plot(kind="pie", autopct="%1.1f%%")
plt.title("Market Share")
plt.ylabel("")
plt.savefig("pie_chart.png")
Data cleaning
Clean and prepare Excel data:
import pandas as pd
df = pd.read_excel("messy_data.xlsx")
# Remove duplicates
df = df.drop_duplicates()
# Handle missing values
df = df.fillna(0) # or df.dropna()
# Remove whitespace
df["name"] = df["name"].str.strip()
# Convert data types
df["date"] = pd.to_datetime(df["date"])
df["amount"] = pd.to_numeric(df["amount"], errors="coerce")
# Save cleaned data
df.to_excel("cleaned_data.xlsx", index=False)
Merging and joining
Combine multiple Excel files:
import pandas as pd
# Read multiple files
df1 = pd.read_excel("sales_q1.xlsx")
df2 = pd.read_excel("sales_q2.xlsx")
# Concatenate vertically
combined = pd.concat([df1, df2], ignore_index=True)
# Merge on common column
customers = pd.read_excel("customers.xlsx")
sales = pd.read_excel("sales.xlsx")
merged = pd.merge(sales, customers, on="customer_id", how="left")
merged.to_excel("merged_data.xlsx", index=False)
Advanced formatting
Apply conditional formatting and styles:
import pandas as pd
from openpyxl import load_workbook
from openpyxl.styles import PatternFill, Font
# Create Excel file
df = pd.DataFrame({
"Product": ["A", "B", "C"],
"Sales": [100, 200, 150]
})
df.to_excel("formatted.xlsx", index=False)
# Load workbook for formatting
wb = load_workbook("formatted.xlsx")
ws = wb.active
# Apply conditional formatting
red_fill = PatternFill(start_color="FF0000", end_color="FF0000", fill_type="solid")
green_fill = PatternFill(start_color="00FF00", end_color="00FF00", fill_type="solid")
for row in range(2, len(df) + 2):
cell = ws[f"B{row}"]
if cell.value < 150:
cell.fill = red_fill
else:
cell.fill = green_fill
# Bold headers
for cell in ws[1]:
cell.font = Font(bold=True)
wb.save("formatted.xlsx")
Performance tips
- Use
read_excelwithusecolsto read specific columns only - Use
chunksizefor very large files - Consider using
engine='openpyxl'orengine='xlrd'based on file type - Use
dtypeparameter to specify column types for faster reading
Available packages
- pandas - Data analysis and manipulation (primary)
- openpyxl - Excel file creation and formatting
- xlrd - Reading older .xls files
- xlsxwriter - Advanced Excel writing capabilities
- matplotlib - Chart generation
| 1 | |
| 2 | name Excel Analysis |
| 3 | description Analyze Excel spreadsheets, create pivot tables, generate charts, and perform data analysis. Use when analyzing Excel files, spreadsheets, tabular data, or .xlsx files. |
| 4 | |
| 5 | |
| 6 | # Excel Analysis |
| 7 | |
| 8 | ## Quick start |
| 9 | |
| 10 | Read Excel files with pandas: |
| 11 | |
| 12 | |
| 13 | import pandas as pd |
| 14 | |
| 15 | # Read Excel file |
| 16 | df = pd.read_excel("data.xlsx", sheet_name="Sheet1") |
| 17 | |
| 18 | # Display first few rows |
| 19 | print(df.head()) |
| 20 | |
| 21 | # Basic statistics |
| 22 | print(df.describe()) |
| 23 | |
| 24 | |
| 25 | ## Reading multiple sheets |
| 26 | |
| 27 | Process all sheets in a workbook: |
| 28 | |
| 29 | |
| 30 | import pandas as pd |
| 31 | |
| 32 | # Read all sheets |
| 33 | excel_file = pd.ExcelFile("workbook.xlsx") |
| 34 | |
| 35 | for sheet_name in excel_file.sheet_names: |
| 36 | df = pd.read_excel(excel_file, sheet_name=sheet_name) |
| 37 | print(f"\n{sheet_name}:") |
| 38 | print(df.head()) |
| 39 | |
| 40 | |
| 41 | ## Data analysis |
| 42 | |
| 43 | Perform common analysis tasks: |
| 44 | |
| 45 | |
| 46 | import pandas as pd |
| 47 | |
| 48 | df = pd.read_excel("sales.xlsx") |
| 49 | |
| 50 | # Group by and aggregate |
| 51 | sales_by_region = df.groupby("region")["sales"].sum() |
| 52 | print(sales_by_region) |
| 53 | |
| 54 | # Filter data |
| 55 | high_sales = df[df["sales"] > 10000] |
| 56 | |
| 57 | # Calculate metrics |
| 58 | df["profit_margin"] = (df["revenue"] - df["cost"]) / df["revenue"] |
| 59 | |
| 60 | # Sort by column |
| 61 | df_sorted = df.sort_values("sales", ascending=False) |
| 62 | |
| 63 | |
| 64 | ## Creating Excel files |
| 65 | |
| 66 | Write data to Excel with formatting: |
| 67 | |
| 68 | |
| 69 | import pandas as pd |
| 70 | |
| 71 | df = pd.DataFrame({ |
| 72 | "Product": ["A", "B", "C"], |
| 73 | "Sales": [100, 200, 150], |
| 74 | "Profit": [20, 40, 30] |
| 75 | }) |
| 76 | |
| 77 | # Write to Excel |
| 78 | writer = pd.ExcelWriter("output.xlsx", engine="openpyxl") |
| 79 | df.to_excel(writer, sheet_name="Sales", index=False) |
| 80 | |
| 81 | # Get worksheet for formatting |
| 82 | worksheet = writer.sheets["Sales"] |
| 83 | |
| 84 | # Auto-adjust column widths |
| 85 | for column in worksheet.columns: |
| 86 | max_length = 0 |
| 87 | column_letter = column[0].column_letter |
| 88 | for cell in column: |
| 89 | if len(str(cell.value)) > max_length: |
| 90 | max_length = len(str(cell.value)) |
| 91 | worksheet.column_dimensions[column_letter].width = max_length + 2 |
| 92 | |
| 93 | writer.close() |
| 94 | |
| 95 | |
| 96 | ## Pivot tables |
| 97 | |
| 98 | Create pivot tables programmatically: |
| 99 | |
| 100 | |
| 101 | import pandas as pd |
| 102 | |
| 103 | df = pd.read_excel("sales_data.xlsx") |
| 104 | |
| 105 | # Create pivot table |
| 106 | pivot = pd.pivot_table( |
| 107 | df, |
| 108 | values="sales", |
| 109 | index="region", |
| 110 | columns="product", |
| 111 | aggfunc="sum", |
| 112 | fill_value=0 |
| 113 | ) |
| 114 | |
| 115 | print(pivot) |
| 116 | |
| 117 | # Save pivot table |
| 118 | pivot.to_excel("pivot_report.xlsx") |
| 119 | |
| 120 | |
| 121 | ## Charts and visualization |
| 122 | |
| 123 | Generate charts from Excel data: |
| 124 | |
| 125 | |
| 126 | import pandas as pd |
| 127 | import matplotlib.pyplot as plt |
| 128 | |
| 129 | df = pd.read_excel("data.xlsx") |
| 130 | |
| 131 | # Create bar chart |
| 132 | df.plot(x="category", y="value", kind="bar") |
| 133 | plt.title("Sales by Category") |
| 134 | plt.xlabel("Category") |
| 135 | plt.ylabel("Sales") |
| 136 | plt.tight_layout() |
| 137 | plt.savefig("chart.png") |
| 138 | |
| 139 | # Create pie chart |
| 140 | df.set_index("category")["value"].plot(kind="pie", autopct="%1.1f%%") |
| 141 | plt.title("Market Share") |
| 142 | plt.ylabel("") |
| 143 | plt.savefig("pie_chart.png") |
| 144 | |
| 145 | |
| 146 | ## Data cleaning |
| 147 | |
| 148 | Clean and prepare Excel data: |
| 149 | |
| 150 | |
| 151 | import pandas as pd |
| 152 | |
| 153 | df = pd.read_excel("messy_data.xlsx") |
| 154 | |
| 155 | # Remove duplicates |
| 156 | df = df.drop_duplicates() |
| 157 | |
| 158 | # Handle missing values |
| 159 | df = df.fillna(0) # or df.dropna() |
| 160 | |
| 161 | # Remove whitespace |
| 162 | df["name"] = df["name"].str.strip() |
| 163 | |
| 164 | # Convert data types |
| 165 | df["date"] = pd.to_datetime(df["date"]) |
| 166 | df["amount"] = pd.to_numeric(df["amount"], errors="coerce") |
| 167 | |
| 168 | # Save cleaned data |
| 169 | df.to_excel("cleaned_data.xlsx", index=False) |
| 170 | |
| 171 | |
| 172 | ## Merging and joining |
| 173 | |
| 174 | Combine multiple Excel files: |
| 175 | |
| 176 | |
| 177 | import pandas as pd |
| 178 | |
| 179 | # Read multiple files |
| 180 | df1 = pd.read_excel("sales_q1.xlsx") |
| 181 | df2 = pd.read_excel("sales_q2.xlsx") |
| 182 | |
| 183 | # Concatenate vertically |
| 184 | combined = pd.concat([df1, df2], ignore_index=True) |
| 185 | |
| 186 | # Merge on common column |
| 187 | customers = pd.read_excel("customers.xlsx") |
| 188 | sales = pd.read_excel("sales.xlsx") |
| 189 | |
| 190 | merged = pd.merge(sales, customers, on="customer_id", how="left") |
| 191 | |
| 192 | merged.to_excel("merged_data.xlsx", index=False) |
| 193 | |
| 194 | |
| 195 | ## Advanced formatting |
| 196 | |
| 197 | Apply conditional formatting and styles: |
| 198 | |
| 199 | |
| 200 | import pandas as pd |
| 201 | from openpyxl import load_workbook |
| 202 | from openpyxl.styles import PatternFill, Font |
| 203 | |
| 204 | # Create Excel file |
| 205 | df = pd.DataFrame({ |
| 206 | "Product": ["A", "B", "C"], |
| 207 | "Sales": [100, 200, 150] |
| 208 | }) |
| 209 | |
| 210 | df.to_excel("formatted.xlsx", index=False) |
| 211 | |
| 212 | # Load workbook for formatting |
| 213 | wb = load_workbook("formatted.xlsx") |
| 214 | ws = wb.active |
| 215 | |
| 216 | # Apply conditional formatting |
| 217 | red_fill = PatternFill(start_color="FF0000", end_color="FF0000", fill_type="solid") |
| 218 | green_fill = PatternFill(start_color="00FF00", end_color="00FF00", fill_type="solid") |
| 219 | |
| 220 | for row in range(2, len(df) + 2): |
| 221 | cell = ws[f"B{row}"] |
| 222 | if cell.value < 150: |
| 223 | cell.fill = red_fill |
| 224 | else: |
| 225 | cell.fill = green_fill |
| 226 | |
| 227 | # Bold headers |
| 228 | for cell in ws[1]: |
| 229 | cell.font = Font(bold=True) |
| 230 | |
| 231 | wb.save("formatted.xlsx") |
| 232 | |
| 233 | |
| 234 | ## Performance tips |
| 235 | |
| 236 | Use `read_excel` with `usecols` to read specific columns only |
| 237 | Use `chunksize` for very large files |
| 238 | Consider using `engine='openpyxl'` or `engine='xlrd'` based on file type |
| 239 | Use `dtype` parameter to specify column types for faster reading |
| 240 | |
| 241 | ## Available packages |
| 242 | |
| 243 | **pandas** - Data analysis and manipulation (primary) |
| 244 | **openpyxl** - Excel file creation and formatting |
| 245 | **xlrd** - Reading older .xls files |
| 246 | **xlsxwriter** - Advanced Excel writing capabilities |
| 247 | **matplotlib** - Chart generation |
| 248 |
Discussion
Browse more free Claude skills.