@tanskong/office-assistant 1.0.6 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/officer.py +29 -21
- package/src/server.py +27 -10
package/package.json
CHANGED
package/src/officer.py
CHANGED
|
@@ -29,7 +29,7 @@ class TheOfficer:
|
|
|
29
29
|
|
|
30
30
|
# 使用用户目录,避免硬编码 D 盘
|
|
31
31
|
self._default_folder = os.path.join(os.path.expanduser("~"), "OfficeMCP_Data")
|
|
32
|
-
self.Version = "1.0.
|
|
32
|
+
self.Version = "1.0.7"
|
|
33
33
|
self.ComObjects = {}
|
|
34
34
|
self._printable = False
|
|
35
35
|
self.Data = OfficerData()
|
|
@@ -229,30 +229,38 @@ class TheOfficer:
|
|
|
229
229
|
return "Demo succeeded"
|
|
230
230
|
|
|
231
231
|
def DemonstrateExcel(self) -> bool:
|
|
232
|
-
|
|
233
|
-
|
|
232
|
+
try:
|
|
233
|
+
excel = self.Application("Excel", asNewInstance=True)
|
|
234
|
+
if excel is None:
|
|
235
|
+
return False
|
|
236
|
+
book = excel.Workbooks.Add()
|
|
237
|
+
sheet = excel.ActiveSheet
|
|
238
|
+
excel.Visible = True
|
|
239
|
+
sheet.Cells(1, 1).Value = "Hello, World From Office Assistant!"
|
|
240
|
+
sheet.Cells(1, 1).Font.Size = 20
|
|
241
|
+
sheet.Cells(1, 1).Font.Bold = True
|
|
242
|
+
sheet.Cells(2, 1).Value = "This is a demonstration of the Office Assistant server."
|
|
243
|
+
sheet.Cells(3, 1).Value = "You can use run_python tool to control all Microsoft Office Applications."
|
|
244
|
+
return True
|
|
245
|
+
except Exception as e:
|
|
246
|
+
print(f"DemonstrateExcel error: {e}")
|
|
234
247
|
return False
|
|
235
|
-
book = excel.Workbooks.Add()
|
|
236
|
-
sheet = excel.ActiveSheet
|
|
237
|
-
excel.Visible = True
|
|
238
|
-
sheet.Cells(1, 1).Value = "Hello, World From Office Assistant!"
|
|
239
|
-
sheet.Cells(1, 1).Font.Size = 20
|
|
240
|
-
sheet.Cells(1, 1).Font.Bold = True
|
|
241
|
-
sheet.Cells(2, 1).Value = "This is a demonstration of the Office Assistant server."
|
|
242
|
-
sheet.Cells(3, 1).Value = "You can use run_python tool to control all Microsoft Office Applications."
|
|
243
|
-
return True
|
|
244
248
|
|
|
245
249
|
def DemonstratePowerPoint(self) -> bool:
|
|
246
|
-
|
|
247
|
-
|
|
250
|
+
try:
|
|
251
|
+
ppt = self.Application("PowerPoint", asNewInstance=True)
|
|
252
|
+
if ppt is None:
|
|
253
|
+
return False
|
|
254
|
+
ppt.Visible = True
|
|
255
|
+
presentation = ppt.Presentations.Add()
|
|
256
|
+
slide = presentation.Slides.Add(1, 12)
|
|
257
|
+
shape = slide.Shapes.AddTextbox(1, 100, 20, 800, 100)
|
|
258
|
+
shape.TextFrame.TextRange.Text = "Hello, World From Office Assistant!"
|
|
259
|
+
shape.TextFrame.TextRange.Font.Bold = True
|
|
260
|
+
return True
|
|
261
|
+
except Exception as e:
|
|
262
|
+
print(f"DemonstratePowerPoint error: {e}")
|
|
248
263
|
return False
|
|
249
|
-
ppt.Visible = True
|
|
250
|
-
presentation = ppt.Presentations.Add()
|
|
251
|
-
slide = presentation.Slides.Add(1, 12)
|
|
252
|
-
shape = slide.Shapes.AddTextbox(1, 100, 20, 800, 100)
|
|
253
|
-
shape.TextFrame.TextRange.Text = "Hello, World From Office Assistant!"
|
|
254
|
-
shape.TextFrame.TextRange.Font.Bold = True
|
|
255
|
-
return True
|
|
256
264
|
|
|
257
265
|
def FilePath(self, file_name: str = None, subfolder: str = None) -> str:
|
|
258
266
|
# 优先使用智能办公区路径
|
package/src/server.py
CHANGED
|
@@ -67,21 +67,29 @@ def available_apps() -> list:
|
|
|
67
67
|
|
|
68
68
|
|
|
69
69
|
@mcp.tool()
|
|
70
|
-
def launch_app(app_name: str = "Excel", visible: bool = True) ->
|
|
70
|
+
def launch_app(app_name: str = "Excel", visible: bool = True) -> dict:
|
|
71
71
|
"""Launch a Microsoft Office application."""
|
|
72
72
|
try:
|
|
73
73
|
app = Officer.Application(app_name)
|
|
74
|
-
app
|
|
75
|
-
|
|
74
|
+
if app is None:
|
|
75
|
+
return {"success": False, "error": f"Failed to start {app_name}"}
|
|
76
|
+
try:
|
|
77
|
+
app.Visible = visible
|
|
78
|
+
except Exception:
|
|
79
|
+
pass
|
|
80
|
+
return {"success": True, "app": app_name, "name": app.Name}
|
|
76
81
|
except Exception as e:
|
|
77
|
-
|
|
78
|
-
return False
|
|
82
|
+
return {"success": False, "error": str(e)}
|
|
79
83
|
|
|
80
84
|
|
|
81
85
|
@mcp.tool()
|
|
82
|
-
def quit_app(app_name: str = "Excel", force: bool = False) ->
|
|
86
|
+
def quit_app(app_name: str = "Excel", force: bool = False) -> dict:
|
|
83
87
|
"""Quit a Microsoft Office application."""
|
|
84
|
-
|
|
88
|
+
try:
|
|
89
|
+
result = Officer.Quit(app_name, force)
|
|
90
|
+
return {"success": result}
|
|
91
|
+
except Exception as e:
|
|
92
|
+
return {"success": False, "error": str(e)}
|
|
85
93
|
|
|
86
94
|
|
|
87
95
|
# ========== File Operation Tools ==========
|
|
@@ -137,6 +145,8 @@ def save_file(file_path: str = None) -> dict:
|
|
|
137
145
|
for app_name in ['Excel', 'Word', 'PowerPoint']:
|
|
138
146
|
try:
|
|
139
147
|
app = Officer.Application(app_name)
|
|
148
|
+
if app is None:
|
|
149
|
+
continue
|
|
140
150
|
if app_name == 'Excel':
|
|
141
151
|
doc = app.ActiveWorkbook
|
|
142
152
|
elif app_name == 'Word':
|
|
@@ -150,7 +160,7 @@ def save_file(file_path: str = None) -> dict:
|
|
|
150
160
|
else:
|
|
151
161
|
doc.Save()
|
|
152
162
|
return {"success": True, "file": doc.FullName}
|
|
153
|
-
except:
|
|
163
|
+
except Exception:
|
|
154
164
|
continue
|
|
155
165
|
return {"success": False, "error": "No active document found"}
|
|
156
166
|
|
|
@@ -337,9 +347,16 @@ def activate_window(window_name: str) -> dict:
|
|
|
337
347
|
# ========== System Tools ==========
|
|
338
348
|
|
|
339
349
|
@mcp.tool()
|
|
340
|
-
def download(url: str, save_name: str = None) ->
|
|
350
|
+
def download(url: str, save_name: str = None) -> dict:
|
|
341
351
|
"""Download a file from URL to the download folder."""
|
|
342
|
-
|
|
352
|
+
try:
|
|
353
|
+
result = Officer.DownloadURL(url, save_name)
|
|
354
|
+
if result:
|
|
355
|
+
return {"success": True, "file_path": result}
|
|
356
|
+
else:
|
|
357
|
+
return {"success": False, "error": "Download failed or returned empty path"}
|
|
358
|
+
except Exception as e:
|
|
359
|
+
return {"success": False, "error": str(e)}
|
|
343
360
|
|
|
344
361
|
|
|
345
362
|
@mcp.tool()
|