如果文件已经打开,如何关闭它?
import xlwings as xw
wb = xw.Book(folderpath + 'Metrics - auto.xlsx')
使用 try:except: 但需要一种方法来关闭文件以便打开它,或者找到文件并使用它?
如果它已经打开,我会收到此错误:
wb = xw.Book(folderpath + 'Metrics - auto.xlsx')
Traceback (most recent call last):
File "<ipython-input-34-85b6fd35627b>", line 1, in <module>
wb = xw.Book(folderpath + 'Metrics - auto.xlsx')
File "C:\Users\ReDimLearning\AppData\Local\Continuum\anaconda2\lib\site-packages\xlwings\main.py", line 480, in __init__
impl = app.books.open(fullname).impl
File "C:\Users\ReDimLearning\AppData\Local\Continuum\anaconda2\lib\site-packages\xlwings\main.py", line 2751, in open
"Cannot open two workbooks named '%s', even if they are saved in different locations." % name
ValueError: Cannot open two workbooks named 'metrics - auto.xlsx', even if they are saved in different locations.
最佳答案
您可以使用
检查工作簿集合import xlwings as xw
xw.books
并检查您的全名是否已打开,使用如下内容:
if myworkbook in [i.fullname for i in xw.books]:
...
关于python - 如果文件已经打开,xlwings.book 会挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49883687/