0

I have created below context manager to operate with excel files by xlwings. The reason why i am not using xw.Book(), are events and visible variable , with I am working with before opening file. My problem occurs, when I want to open some wb with visible=True. It shows application and 2 wb, first is empty, which has been created with app, and 2nd (correct one) is blank. I need to see this 2nd workbook, to give a chance to modify it in run time(after few automatic actions, user need to provide some extra data, and after that i want script to continue running).

Below my context manager:

class ExcelApplicationSetup:

def __init__(self, app=None, visible=False):
    self.app = app
    self.visible = visible
    if app is None:
        self.new_application = True
    else:
        self.new_application = False

def __enter__(self):
    if self.new_application:
        self.app = xw.App(visible=self.visible)
    else:
        self.app.visible = self.visible
    self.app.api.DisplayAlerts = False
    self.app.api.AskToUpdateLinks = False
    self.app.api.ScreenUpdating = False
    self.app.api.EnableEvents = False
    return self.app

def __exit__(self, exception_type, exception_value, traceback):
    self.app.api.DisplayAlerts = True
    self.app.api.AskToUpdateLinks = True
    self.app.api.ScreenUpdating = True
    self.app.api.EnableEvents = True
    if self.new_application:
        self.app.kill()

Below my code how I am trying to open excel-file with it:

with ExcelApplicationSetup(None, True) as xw_app:
    workbook = xw_app.api.Workbooks.Open(path, False, False)
    wb = xw_app.books(workbook.Name)

Any ideas how could I solve my problem? I also tried to create Excel Application by win32com.client and open file by it, but i don't know how to turn-off events before open file, and how later take opened wb with xlwings.

import win32.com.client as client
xl_app = client.DispatchEx("Excel.Application")
wb = xl_app.Workbooks.Open(path, 0, 0)  # events shows up :/

After that i tried to find excel apps by xlwings and to work on one, below code + error:

import xlwings as xw
my_app = xw.apps[0]

Traceback (most recent call last):
File "C:\Program Files\Python37\lib\site-packages\IPython\core\interactiveshell.py", line 3326, in 
run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-37-0ca6b75919cd>", line 1, in <module>
xw.apps[0]
File "C:\Program Files\Python37\lib\site-packages\xlwings\main.py", line 150, in __getitem__
return App(impl=self.impl[item])
File "C:\Program Files\Python37\lib\site-packages\xlwings\_xlwindows.py", line 286, in __getitem__
raise KeyError('Could not find an Excel instance with this PID.')
KeyError: 'Could not find an Excel instance with this PID.'

PS: I am using xw__version__ = 0.15.2, without possibility to update

Piotr Kotarba
  • 25
  • 1
  • 6

1 Answers1

0

If anyone in the future will be interested:

Screen_updating argument cause this problem. If you want have it on visible u need to change also xl.app.api.ScreenUpdating as True.

Piotr Kotarba
  • 25
  • 1
  • 6