Coverage for agentos/marketplace/skills/xlsx/xlsx.py: 26%
34 statements
« prev ^ index » next coverage.py v7.14.3, created at 2026-07-06 08:01 +0800
« prev ^ index » next coverage.py v7.14.3, created at 2026-07-06 08:01 +0800
1"""
2xlsx — Excel (.xlsx) operations using openpyxl.
4Actions: read, headers, sheets, to_csv, stats, search
5"""
7from typing import Any
10def run(action: str = "read", file_path: str = "", sheet: str = "", **kwargs: Any) -> str:
11 try:
12 import openpyxl
13 except ImportError:
14 return "[xlsx] openpyxl not installed. Run: pip install openpyxl"
16 try:
17 wb = openpyxl.load_workbook(file_path, data_only=True)
18 except FileNotFoundError:
19 return f"[xlsx] File not found: {file_path}"
20 except Exception as e:
21 return f"[xlsx] Error: {e}"
23 ws = wb[sheet] if sheet else wb.active
25 if action == "sheets":
26 return "Sheets: " + ", ".join(wb.sheetnames)
28 if action == "headers":
29 headers = [cell.value for cell in ws[1]]
30 return f"Headers ({len(headers)}): " + ", ".join(str(h) for h in headers if h)
32 if action == "stats":
33 rows = ws.max_row - 1
34 cols = ws.max_column
35 return f"Sheet: {ws.title}, Rows: {rows}, Columns: {cols}"
37 if action == "read":
38 lines = []
39 for row in ws.iter_rows(min_row=1, max_row=min(ws.max_row, 100), values_only=True):
40 lines.append("\t".join(str(c) if c is not None else "" for c in row))
41 return "\n".join(lines[:50])
43 if action == "to_csv":
44 rows_list = []
45 for row in ws.iter_rows(values_only=True):
46 rows_list.append(",".join(f'"{c}"' if c is not None else "" for c in row))
47 return "\n".join(rows_list[:500])
49 return f"[xlsx] Unknown action: {action}"
52__all__ = ["run"]