Excel Automation
Create professional Excel files, parse complex financial models, and control Excel on macOS.
Quick Start
Overview
Three capabilities:
Tool Selection Decision Tree
Signals of "complex" Excel: file >1MB, .xlsm extension, from investment bank/broker, contains VBA macros.
IMPORTANT: Always run file <path> first — extensions lie. A .xls file may actually be a ZIP-based xlsx.
Creating Excel Files (openpyxl)
Professional Color Convention (Investment Banking Standard)
Core Formatting Patterns
Number Format Codes
Conditional Formatting (Sensitivity Tables)
Red-to-green gradient for sensitivity analysis:
Execution
Full template script: See scripts/create_formatted_excel.py
Parsing Complex Excel (zipfile + xml)
When openpyxl fails on complex xlsm files (corrupted DefinedNames, complex VBA), use stdlib directly.
XLSX Internal ZIP Structure
Sheet Name Resolution (Two-Step)
Sheet names in workbook.xml link to physical files via _rels/workbook.xml.rels:
Cell Data Extraction
Fixing Corrupted DefinedNames
Investment bank xlsm files often have corrupted <definedName> entries containing "Formula removed":
Full template script: See scripts/parse_complex_excel.py
Controlling Excel on macOS (AppleScript)
All commands verified on macOS with Microsoft Excel.
Verified Commands
Timing and Timeout
Always add sleep 1 between AppleScript commands and subsequent operations (e.g., screenshot) to allow UI rendering.
IMPORTANT: osascript will hang indefinitely if Excel is not running or not responding. Always wrap with timeout:
Common Mistakes
Important Notes
- Execute Python scripts with
uv run --with openpyxl(never use system Python) - LibreOffice (
soffice --headless) can convert formats and recalculate formulas - Detailed formatting reference: See
references/formatting-reference.md

