Need to bring a windows application(Excel in my case) to foreground – python

Question:

I am creating a windows automation script. I need to bring Excel application to foreground. I am able to open excel application and get list of subprocess running. But i am not sure how to Excel application to foreground. Please help

import subprocess
from win32com.client import Dispatch

xl = Dispatch("Excel.Application")
xl.Visible = True 

cmd = 'WMIC PROCESS get Caption,Commandline,Processid'
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
for line in proc.stdout:
    print(line)

I am getting list of applications open, but i need to bring Excel to foreground. Please help me how to do it.

b’EXCEL.EXE “C:Program FilesMicrosoft OfficerootOffice16EXCEL.EXE”
b’SearchFilterHost.exe 740 rrn’
b’python.exe C:UsersarvinAppDataLocalProgramsPythonPython37-32python.exe rrn’
b’conhost.exe ??C:WINDOWSsystem32conhost.exe 0x4 15724 rrn’
b’cmd.exe C:WINDOWSsystem32cmd.exe /c “WMIC PROCESS get Caption,Commandline,Processid” 18084 rrn’

Asked By: Arvinth

||

Answers:

If you are using all Microsoft Office applications, chances are object.Activate() method will work. This is part of Microsoft VBA and C# application calls that are also available to 3rd party languages. Code below was tested on my Windows 10 environment, python 3.7 with pywin32, and MS-Office 2013.

####### Activate one excel file ##############
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb1 = excel.Workbooks.Open(r'C:pythonsosheet1.xlsx')
wb2 = excel.Workbooks.Open(r'C:pythonsosheet2.xlsx')
wb3 = excel.Workbooks.Open(r'C:pythonsosheet3.xlsx')    
excel.Visible = True

# This will open 3 workbooks with wb1 on top.

excel.Application.ActiveWorkbook.Name
# Prints name of currently active workbook, i.e., the one on top.

wb3.Activate()  # puts workbook3 on top
wb2.Activate()  # puts workbook2 on top

### You may need to install pywin32.
### pip install pywin32
Answered By: Jennifer Yoon

If you really need to bring the excel window to foreground you can do this:

from win32com.client import Dispatch
from win32gui import SetForegroundWindow

xl = Dispatch("Excel.Application")
xl.Visible = True
SetForegroundWindow(xl.hwnd)

Where hwnd is the excel’s window id. More information about hwnd

Answered By: Carlos Porta

If anyone else also stumbles across this post and is looking for a solution for Microsoft Word (works slightly differently than Excel):

import win32com.client
import win32gui
import time

word = win32com.client.gencache.EnsureDispatch("Word.Application")
word.Visible = True
word.Documents.Open(r'C:TempmyDocument.docx', ReadOnly=True)

win32gui.SetForegroundWindow(word.ActiveWindow.Hwnd)
print('Click another window in the foreground, Word will regain focus in 10s.')
time.sleep(10)
win32gui.SetForegroundWindow(word.ActiveWindow.Hwnd)
print('Click another window in the foreground, Word will regain focus in 10s.')
time.sleep(10)
win32gui.SetForegroundWindow(word.ActiveWindow.Hwnd)

Answered By: phibel
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.