Coverage for agentos/marketplace/skills/xlsx/xlsx.py: 9%

34 statements  

« prev     ^ index     » next       coverage.py v7.14.3, created at 2026-07-06 23:17 +0800

1""" 

2xlsx — Excel (.xlsx) operations using openpyxl. 

3 

4Actions: read, headers, sheets, to_csv, stats, search 

5""" 

6 

7from typing import Any 

8 

9 

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" 

15 

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}" 

22 

23 ws = wb[sheet] if sheet else wb.active 

24 

25 if action == "sheets": 

26 return "Sheets: " + ", ".join(wb.sheetnames) 

27 

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) 

31 

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}" 

36 

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]) 

42 

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]) 

48 

49 return f"[xlsx] Unknown action: {action}" 

50 

51 

52__all__ = ["run"]