Amin Noor
Amin Noor

Reputation: 75

How to bring xlwings excel file foreground using Python

I am using xlwings to modify an excel file in Python 3.6. xlwings opens the excel instance and runs my python script, as a results the Excel file would go to the background of my pycharm window.

Is there any straightforward way to bring the excel file to the foreground at the end of the program?

My code is similar to this:

import xlwings as xw

wb = xw.Book('Excel.xlsx')
sheet_ranges = wb.sheets['Sheet1']

def updatingExcel(input):
    value = 'Hello World'
    sheet_ranges.range('H' + str(i + 3)).value = value
return value

    print(updatingExcel(anyinput)

I understand that if I run the.py file outside pycharm this issue most probably won't occur as the screen terminates itself. However I would appreciate your input.

Upvotes: 2

Views: 1725

Answers (1)

Felix Zumstein
Felix Zumstein

Reputation: 7070

Use app.activate with steal_focus=True, see: http://docs.xlwings.org/en/stable/api.html#xlwings.App.activate

for example:

import xlwings as xw
wb = xw.Book()
wb.app.activate(steal_focus=True)

Upvotes: 2

Related Questions