@tanskong/office-assistant 1.0.3 → 1.0.4
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/requirements.txt +4 -3
- package/src/officer.py +27 -2
package/package.json
CHANGED
package/requirements.txt
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
fastmcp>=2.3.3
|
|
2
|
-
pywin32>=310
|
|
3
|
-
Pillow>=10.0.0
|
|
1
|
+
fastmcp>=2.3.3
|
|
2
|
+
pywin32>=310
|
|
3
|
+
Pillow>=10.0.0
|
|
4
|
+
psutil>=5.9.0
|
package/src/officer.py
CHANGED
|
@@ -147,10 +147,35 @@ class TheOfficer:
|
|
|
147
147
|
self.__dict__[app_name_attr] = app
|
|
148
148
|
return app
|
|
149
149
|
else:
|
|
150
|
+
# 策略1:优先尝试 GetActiveObject
|
|
150
151
|
try:
|
|
151
152
|
app = win32com.client.GetActiveObject(app_full_name)
|
|
152
|
-
|
|
153
|
-
|
|
153
|
+
# 验证可用性
|
|
154
|
+
_ = app.Visible
|
|
155
|
+
self.__dict__[app_name_attr] = app
|
|
156
|
+
return app
|
|
157
|
+
except (pywintypes.com_error, Exception):
|
|
158
|
+
pass
|
|
159
|
+
|
|
160
|
+
# 策略2:通过进程列表查找当前用户的实例(需要 psutil)
|
|
161
|
+
try:
|
|
162
|
+
import psutil
|
|
163
|
+
current_user = psutil.Process().username()
|
|
164
|
+
for proc in psutil.process_iter(['pid', 'name', 'username']):
|
|
165
|
+
if proc.info['name'].lower() == app_name.lower() + '.exe' and proc.info['username'] == current_user:
|
|
166
|
+
pid = proc.info['pid']
|
|
167
|
+
# 使用动态分派绑定到指定进程
|
|
168
|
+
import win32com.client.dynamic
|
|
169
|
+
moniker = f"!{pid}"
|
|
170
|
+
app = win32com.client.dynamic.Dispatch(moniker)
|
|
171
|
+
_ = app.Visible
|
|
172
|
+
self.__dict__[app_name_attr] = app
|
|
173
|
+
return app
|
|
174
|
+
except Exception:
|
|
175
|
+
pass
|
|
176
|
+
|
|
177
|
+
# 策略3:兜底,创建新实例
|
|
178
|
+
app = win32com.client.Dispatch(app_full_name)
|
|
154
179
|
self.__dict__[app_name_attr] = app
|
|
155
180
|
return app
|
|
156
181
|
except Exception as e:
|