Folder-Level Excel Report Generator
Purpose
Scan a directory recursively (up to N levels) and produce a polished Excel workbook where:
- Each column represents a folder depth level (第1级, 第2级, …)
- Only leaf directories (or deepest-level folders) appear as rows — parent folder names flow alongside their first child
- Consecutive cells with the same value are merged, creating a clean tree-like layout
- Column A is a serial number, merged per Level-1 folder (1, 2, 3, …)
- All cells have thin black borders and level-specific color backgrounds
Usage
When the user requests a folder-to-Excel export:
- Confirm parameters — root path to scan (default
D:\), max depth (default 6), output location. - Run the script:
bash python scripts/generate_folder_tree.py <ROOT_PATH> --output <OUTPUT_PATH> --max-depth <N>
The script requiresopenpyxl. Install it if not present:
bash pip install openpyxl - Present the resulting .xlsx file to the user.
Script Arguments
| Argument | Default | Description |
|---|---|---|
root_path |
D:\ |
Root directory to scan |
--output / -o |
文件夹层级目录.xlsx (cwd) |
Output Excel file path |
--max-depth |
6 |
Maximum depth to scan (1–10) |
Example
python scripts/generate_folder_tree.py "D:\" --output "D:\output\folder_tree.xlsx" --max-depth 6
Design Principles
Row Selection
Only directories that have no subfolders (within the scanned depth) or are at the maximum depth become rows. This eliminates intermediate "spacer" rows and makes every row meaningful.
Cell Merging
- Merge is determined by actual values (the full path matrix), not display values.
- When consecutive rows share the same value in a column and share the same left-side parent path, they are merged into a single cell.
- The Level-1 folder column (B) and serial number column (A) are merged together so each primary folder block shows one number.
Styling
| Element | Style |
|---|---|
| Headers | White text, colored backgrounds per level, bold |
| Level columns | Light-colored backgrounds (blue → green → orange → yellow → light blue → gray) |
| Empty cells | White background, black borders |
| All cells | Thin black borders, left-aligned with 1-char indent |
| Serial column | Gray background, bold, centered |
| Row 1 | Frozen for scrolling |
| Auto-filter | Enabled on all columns |
Troubleshooting
- PermissionError: The script skips inaccessible directories and prints a warning. This is expected for system-protected paths.
- Large trees: Scanning deep directories (e.g.,
C:\) may take a minute. The script reports progress. - openpyxl not found:
pip install openpyxlresolves this.
Resources
scripts/generate_folder_tree.py— The main generation script. Run directly with Python.