4

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.

stratis
  • 7,750
  • 13
  • 53
  • 94

3 Answers3

5

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

import win32process
import win32com
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.

James Wong
  • 83
  • 1
  • 6
Sama Tori
  • 121
  • 1
  • 5
0

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.

haraprasadj
  • 1,059
  • 1
  • 8
  • 17
  • I only need the process id (PID) of wordApp. – stratis Apr 01 '14 at 15:53
  • 1
    You can always filter on process name (winword.exe). Please see this link, related to your question: http://stackoverflow.com/questions/550653/cross-platform-way-to-get-pids-by-process-name-in-python – haraprasadj Apr 01 '14 at 16:20
0

application = CreateObject("application_name")

print("You've got your process id %d" % application.ProcessID)

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 26 '23 at 11:12