xlwings / xlwings

xlwings is a Python library that makes it easy to call Python from Excel and vice versa. It works with Excel on Windows and macOS as well as with Google Sheets and Excel on the web.
https://www.xlwings.org
Other
3k stars 503 forks source link

no active app? #2520

Closed lgxcxd closed 1 month ago

lgxcxd commented 2 months ago

OS (e.g. Windows 10 or macOS Sierra)

windows 11

Versions of xlwings, Excel and Python (e.g. 0.11.8, Office 365, Python 3.7)

0.32.2

Describe your issue (incl. Traceback!)

# Your traceback here
XlwingsError                              Traceback (most recent call last)
Cell In[3], line 10
      8 print(app.pid)
      9 print(xw.apps)
---> 10 wb1 = xw.books.add()  # 否则这句报错
     11 print(wb1)
     12 input('')

File [E:\miniforge3\envs\python\Lib\site-packages\xlwings\main.py:5112](file:///E:/miniforge3/envs/python/Lib/site-packages/xlwings/main.py#line=5111), in Books.add(self)
   5108 def add(self):
   5109     """
   5110     Creates a new Book. The new Book becomes the active Book. Returns a Book object.
   5111     """
-> 5112     return Book(impl=self.impl.add())

File [E:\miniforge3\envs\python\Lib\site-packages\xlwings\main.py:5292](file:///E:/miniforge3/envs/python/Lib/site-packages/xlwings/main.py#line=5291), in ActiveAppBooks.impl(self)
   5289 @property
   5290 def impl(self):
   5291     if not apps:
-> 5292         raise XlwingsError("Couldn't find any active App!")
   5293     return apps.active.books.impl

XlwingsError: Couldn't find any active App!

Include a minimal code sample to reproduce the issue (and attach a sample workbook if required!)

# Your code here
# 以下代码说明了" xw.books.add()"如何应用;
import xlwings as xw
with xw.apps.add() as app:
    print(app.visible)
    app.visible = True
    wb = app.books.add()  #The required statement: Without this, you would have to manually run Excel first and create a workbook.
    print(app.visible)
    print(app.pid)
    print(xw.apps)
    wb1 = xw.books.add()  # Otherwise, this line will throw an error:   “XlwingsError: Couldn't find any active App!”
    print(wb1)
fzumstein commented 2 months ago

xw.apps.add() should probably not be there. Use this instead:

import xlwings as xw
with xw.App() as app:  # <==
    print(app.books.active)
lgxcxd commented 2 months ago

xw.apps.add() should probably not be there. Use this instead:

import xlwings as xw
with xw.App() as app:  # <==
    print(app.books.active)

Thanks alot! I was learning when to use xw.books.add(), and I realized that you must first establish a workbook (either through code or manually) for this line of code to take effect. Also, when Excel is not running, even after executing xw.apps.add(), running print(xw.apps) still outputs an empty set: Apps([]). However, the established app can print out the PID value. This seems incorrect; after creating an Excel app, the apps collection should have a member.

fzumstein commented 2 months ago

You're correct, this is a bug. Most likely introduced with the engine refactor. However, I'd consider it more Pythonic anyways to use xw.App(), so you can work around it.

lgxcxd commented 2 months ago

You're correct, this is a bug. Most likely introduced with the engine refactor. However, I'd consider it more Pythonic anyways to use xw.App(), so you can work around it.

I am learning to use xlwings, so I don't want to ignore the information provided on the xlwings official help site, including how to use xw.books.add(). Now, I am more inclined to use wb = xw.Book() to create or link to a workbook.

fzumstein commented 1 month ago

xw.Book() should be your default, yes. The only reason you'd use books.add() would be to open it in a specific app via myapp.books.add().