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

32 statements  

« prev     ^ index     » next       coverage.py v7.14.3, created at 2026-07-09 10:19 +0800

1""" 

2docx — Word document (.docx) operations using python-docx. 

3 

4Actions: read, paragraphs, tables, metadata, stats 

5""" 

6 

7from typing import Any 

8 

9 

10def run(action: str = "read", file_path: str = "", **kwargs: Any) -> str: 

11 try: 

12 from docx import Document 

13 except ImportError: 

14 return "[docx] python-docx not installed. Run: pip install python-docx" 

15 

16 try: 

17 doc = Document(file_path) 

18 except FileNotFoundError: 

19 return f"[docx] File not found: {file_path}" 

20 except Exception as e: 

21 return f"[docx] Error: {e}" 

22 

23 if action == "metadata": 

24 props = doc.core_properties 

25 return ( 

26 f"Title: {props.title or 'N/A'}\n" 

27 f"Author: {props.author or 'N/A'}\n" 

28 f"Modified: {props.modified or 'N/A'}\n" 

29 f"Paragraphs: {len(doc.paragraphs)}, Tables: {len(doc.tables)}" 

30 ) 

31 

32 if action == "paragraphs": 

33 lines = [p.text for p in doc.paragraphs if p.text.strip()] 

34 return f"Paragraphs ({len(lines)}):\n" + "\n".join(lines[:30]) 

35 

36 if action == "tables": 

37 result = [] 

38 for i, table in enumerate(doc.tables): 

39 headers = [cell.text for cell in table.rows[0].cells] 

40 result.append(f"Table {i+1}: {len(table.rows)} rows, Headers: {headers}") 

41 return "\n".join(result) if result else "[docx] No tables found." 

42 

43 if action == "stats": 

44 para_count = len(doc.paragraphs) 

45 table_count = len(doc.tables) 

46 word_count = sum(len(p.text.split()) for p in doc.paragraphs) 

47 return f"Paragraphs: {para_count}, Tables: {table_count}, Words: ~{word_count}" 

48 

49 # Default: read 

50 text = "\n".join(p.text for p in doc.paragraphs[:50]) 

51 return text[:3000] 

52 

53 

54__all__ = ["run"]