2

I am trying to use xlwings to link Python to Excel. With good success, but I can not link to an already opened (and modified) excel workbook. So the workbook is already open in Excel, I want to link to that and use that specific workbook.

Using wb = Workbook(filename) that works fine, except when I already modified the excel spreadsheet in Excel. When I issue the command wb = Workbook(filename) Excel returns with "filename is already open. Reopening will cause any changes you made will be discarded. Do you want to continue Y/N ?"

So, instead of using the workbook already available in Excel, it tries to reopen the original workbook in Excel. I need to use the current (with all my non-saved changes) copy as it now lives in Excel.

So the question boils down to: How do I live-link to an already opened workbook in excel?

Thanks, Willem

Wtower
  • 18,848
  • 11
  • 103
  • 80
Willem
  • 61
  • 4

2 Answers2

0

You're doing everything correctly, which means that the behaviour you're seeing is a subtle bug that only materialises under certain circumstances. A first improvement was made in v0.3.1 (see this question). However, I have confidence that it will finally be resolved in the next version (v0.4.0). In short, issues could be caused by untrusted document locations, a unicode bug or if you're using multiple instances.

UPDATE: xlwings v0.4.0 has just been released and should finally fix this bug reliably.

Community
  • 1
  • 1
Felix Zumstein
  • 6,737
  • 1
  • 30
  • 62
0

When I don't know beforehand whether the Excel workbook will be opened while running the Python snippet, I simply write something along:

        try:
            wb = Workbook('[FILENAME]')
        except:
            wb = Workbook('[COMPLETE PATH + FILENAME]') 

(I use xlwings 0.5.0)

tagoma
  • 3,896
  • 4
  • 38
  • 57