Add worksheet selection to excel-to-markdown skill
Add --sheet parameter to specify worksheets by name or index (1-based). Change default behavior to use first worksheet instead of active sheet, with proper error messages for invalid worksheet references. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -19,6 +19,18 @@ Convert an entire Excel file to Markdown:
|
|||||||
python3 scripts/excel_to_markdown.py input.xlsx -o output.md
|
python3 scripts/excel_to_markdown.py input.xlsx -o output.md
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Specify Worksheet
|
||||||
|
|
||||||
|
Convert a specific worksheet by name or index:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# By worksheet name
|
||||||
|
python3 scripts/excel_to_markdown.py input.xlsx -o output.md --sheet "Sheet2"
|
||||||
|
|
||||||
|
# By worksheet index (1-based)
|
||||||
|
python3 scripts/excel_to_markdown.py input.xlsx -o output.md --sheet 2
|
||||||
|
```
|
||||||
|
|
||||||
### Convert with Row/Column Numbers
|
### Convert with Row/Column Numbers
|
||||||
|
|
||||||
Display Excel row and column identifiers for reference:
|
Display Excel row and column identifiers for reference:
|
||||||
@@ -51,6 +63,7 @@ python3 scripts/excel_to_markdown.py input.xlsx -o output.md --start-row 2 --sta
|
|||||||
| `--show-cols` | Display column letters (A, B, C...) in the first row |
|
| `--show-cols` | Display column letters (A, B, C...) in the first row |
|
||||||
| `--start-row` | Starting row number (default: 1) |
|
| `--start-row` | Starting row number (default: 1) |
|
||||||
| `--start-col` | Starting column number (default: 1) |
|
| `--start-col` | Starting column number (default: 1) |
|
||||||
|
| `--sheet` | Worksheet name or index (default: first worksheet) |
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
@@ -67,7 +80,12 @@ The converter automatically scans the spreadsheet to identify the actual data ra
|
|||||||
|
|
||||||
### Multi-Sheet Support
|
### Multi-Sheet Support
|
||||||
|
|
||||||
By default, the converter processes the active (last selected) sheet in the workbook.
|
By default, the converter processes the first worksheet in the workbook. You can specify a different worksheet using the `--sheet` parameter:
|
||||||
|
|
||||||
|
- **By name**: `--sheet "Sheet2"` or `--sheet "Data"`
|
||||||
|
- **By index**: `--sheet 2` (1-based indexing)
|
||||||
|
|
||||||
|
If the specified worksheet is not found, an error will display the list of available worksheets.
|
||||||
|
|
||||||
## Common Use Cases
|
## Common Use Cases
|
||||||
|
|
||||||
|
|||||||
@@ -14,15 +14,34 @@ import argparse
|
|||||||
class ExcelToMarkdown:
|
class ExcelToMarkdown:
|
||||||
"""Excel转Markdown转换器"""
|
"""Excel转Markdown转换器"""
|
||||||
|
|
||||||
def __init__(self, excel_file: str):
|
def __init__(self, excel_file: str, sheet_name: Optional[str] = None):
|
||||||
"""
|
"""
|
||||||
初始化转换器
|
初始化转换器
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
excel_file: Excel文件路径
|
excel_file: Excel文件路径
|
||||||
|
sheet_name: 工作表名称或索引(None表示使用第一个工作表)
|
||||||
"""
|
"""
|
||||||
self.workbook = openpyxl.load_workbook(excel_file, data_only=True)
|
self.workbook = openpyxl.load_workbook(excel_file, data_only=True)
|
||||||
self.sheet = self.workbook.active
|
|
||||||
|
# 获取指定的工作表
|
||||||
|
if sheet_name is None:
|
||||||
|
# 使用第一个工作表
|
||||||
|
self.sheet = self.workbook.worksheets[0]
|
||||||
|
elif isinstance(sheet_name, int) or sheet_name.isdigit():
|
||||||
|
# 按索引获取(从1开始)
|
||||||
|
sheet_index = int(sheet_name) - 1
|
||||||
|
if 0 <= sheet_index < len(self.workbook.worksheets):
|
||||||
|
self.sheet = self.workbook.worksheets[sheet_index]
|
||||||
|
else:
|
||||||
|
raise ValueError(f"工作表索引 {sheet_name} 超出范围(1-{len(self.workbook.worksheets)})")
|
||||||
|
else:
|
||||||
|
# 按名称获取
|
||||||
|
if sheet_name in self.workbook.sheetnames:
|
||||||
|
self.sheet = self.workbook[sheet_name]
|
||||||
|
else:
|
||||||
|
available_sheets = ", ".join(self.workbook.sheetnames)
|
||||||
|
raise ValueError(f"找不到工作表 '{sheet_name}',可用的工作表: {available_sheets}")
|
||||||
|
|
||||||
def detect_data_range(self, max_scan_rows: int = 100, max_scan_cols: int = 100) -> Tuple[int, int]:
|
def detect_data_range(self, max_scan_rows: int = 100, max_scan_cols: int = 100) -> Tuple[int, int]:
|
||||||
"""
|
"""
|
||||||
@@ -163,18 +182,24 @@ def main():
|
|||||||
示例:
|
示例:
|
||||||
# 自动检测范围并转换
|
# 自动检测范围并转换
|
||||||
python excel_to_markdown.py input.xlsx -o output.md
|
python excel_to_markdown.py input.xlsx -o output.md
|
||||||
|
|
||||||
# 指定转换前10行、前5列
|
# 指定转换前10行、前5列
|
||||||
python excel_to_markdown.py input.xlsx -o output.md -r 10 -c 5
|
python excel_to_markdown.py input.xlsx -o output.md -r 10 -c 5
|
||||||
|
|
||||||
# 显示行号和列号
|
# 显示行号和列号
|
||||||
python excel_to_markdown.py input.xlsx -o output.md --show-rows --show-cols
|
python excel_to_markdown.py input.xlsx -o output.md --show-rows --show-cols
|
||||||
|
|
||||||
# 从第2行第2列开始,转换5行3列
|
# 从第2行第2列开始,转换5行3列
|
||||||
python excel_to_markdown.py input.xlsx -o output.md -r 5 -c 3 --start-row 2 --start-col 2
|
python excel_to_markdown.py input.xlsx -o output.md -r 5 -c 3 --start-row 2 --start-col 2
|
||||||
|
|
||||||
|
# 指定工作表(按名称)
|
||||||
|
python excel_to_markdown.py input.xlsx -o output.md --sheet "Sheet2"
|
||||||
|
|
||||||
|
# 指定工作表(按索引)
|
||||||
|
python excel_to_markdown.py input.xlsx -o output.md --sheet 2
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
|
||||||
parser.add_argument('input_file', help='输入的Excel文件路径')
|
parser.add_argument('input_file', help='输入的Excel文件路径')
|
||||||
parser.add_argument('-o', '--output', help='输出的Markdown文件路径(不指定则输出到控制台)')
|
parser.add_argument('-o', '--output', help='输出的Markdown文件路径(不指定则输出到控制台)')
|
||||||
parser.add_argument('-r', '--max-rows', type=int, help='最大转换行数(不指定则自动检测)')
|
parser.add_argument('-r', '--max-rows', type=int, help='最大转换行数(不指定则自动检测)')
|
||||||
@@ -183,12 +208,13 @@ def main():
|
|||||||
parser.add_argument('--show-cols', action='store_true', help='显示列号(字母形式)')
|
parser.add_argument('--show-cols', action='store_true', help='显示列号(字母形式)')
|
||||||
parser.add_argument('--start-row', type=int, default=1, help='起始行号(默认1)')
|
parser.add_argument('--start-row', type=int, default=1, help='起始行号(默认1)')
|
||||||
parser.add_argument('--start-col', type=int, default=1, help='起始列号(默认1)')
|
parser.add_argument('--start-col', type=int, default=1, help='起始列号(默认1)')
|
||||||
|
parser.add_argument('--sheet', help='工作表名称或索引(不指定则使用第一个工作表)')
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 创建转换器
|
# 创建转换器
|
||||||
converter = ExcelToMarkdown(args.input_file)
|
converter = ExcelToMarkdown(args.input_file, args.sheet)
|
||||||
|
|
||||||
# 执行转换
|
# 执行转换
|
||||||
markdown_text = converter.convert(
|
markdown_text = converter.convert(
|
||||||
|
|||||||
Reference in New Issue
Block a user