gunardilin
gunardilin

Reputation: 369

Activate (switch to) another opened excel book using Python xlwings

I would like to switch to another already opened Excel book. My scenario:

  1. Firstly I create a new book with:
xw.Book()
xw.books.active

The output: <Book [Book7]>

  1. Now I create another new Excel book with:
xw.Book()
xw.books.active

The ouput: <Book [Book8]>

  1. Next I would like to activate previous book i.e. Book7 I tried:
xw.books["Book7"]
xw.books.active

But it outputs: <Book [Book8]> !!!

How could I reactivate previous book (Book7)? Thank you:-)

Upvotes: 1

Views: 856

Answers (1)

Felix Zumstein
Felix Zumstein

Reputation: 7070

Assign your new workkbok to a variable, then use the activate method on it, see: https://docs.xlwings.org/en/stable/api.html#xlwings.Book.activate.

book1 = xw.Book()
book2 = xw.Book()
book1.activate(steal_focus=True)

Up to you if you want steal_focus to be True or False (default).

Upvotes: 2

Related Questions