简体   繁体   中英

How to get the PID of a PyWin32 process

I'm using the PyWin32 extensions to do some Microsoft Office calculations through my Python code.

This is how I start Word:

import win32com.client

wordApp = win32com.client.Dispatch("Word.Application")
# calculations...
# Now, how do I get its PID?

What I am trying to figure out here is how to get the wordApp process ID (PID) since I am not using the subprocess module here and I can't just type wordApp.pid .

Thanks in advance.

I'm doing a similar thing, but dispatching excel object. In order to get the process Id, I do the following:

self.application = win32com.client.DispatchEx('Excel.Application')
t, p = win32process.GetWindowThreadProcessId(self.application.Hwnd)

From these variables, p is the process id (the one that shows in task manager).

Hope this helps, despite the time passed.

I think myProcess is a reference to the Word object dispatched and cannot fetch you process information.

You might have to connect to WMI for getting process details like:

objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")

I hope I understood the question right and this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM