python - xlwings 错误:未打开 excel 工作簿在调用 wb.open 时出现错误
问题描述
xlwings 在我的计算机上工作正常,但是当我尝试将相同的设置传输到另一台计算机时,它似乎无法正确打开给我这个错误
Traceback (most recent call last):
File "C:\Users\base7268\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwings\_xlwindows.py", line 432, in __call__
return Book(xl=self.xl(name_or_index))
File "C:\Users\base7268\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwings\_xlwindows.py", line 152, in __call__
v = self._inner(*args, **kwargs)
File "C:\Users\base7268\AppData\Local\Programs\Python\Python37\lib\site-packages\win32com\client\dynamic.py", line 197, in __call__
return self._get_good_object_(self._oleobj_.Invoke(*allArgs),self._olerepr_.defaultDispatchName,None)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147352565), None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\base7268\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwings\main.py", line 2776, in open
impl = self.impl(name)
File "C:\Users\base7268\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwings\_xlwindows.py", line 434, in __call__
raise KeyError(name_or_index)
KeyError: 'output2019-06-03.11-40timeseries_5-31-2019scrubbed.xlsx'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "Computation.py", line 157, in <module>
xwWb = xw.Book("output" + timeName + os.path.split(file_path)[1])
File "C:\Users\base7268\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwings\main.py", line 488, in __init__
impl = app.books.open(fullname).impl
File "C:\Users\base7268\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwings\main.py", line 2787, in open
impl = self.impl.open(fullname)
File "C:\Users\base7268\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwings\_xlwindows.py", line 443, in open
return Book(xl=self.xl.Open(fullname))
File "C:\Users\base7268\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwings\_xlwindows.py", line 63, in __call__
v = self.__method(*args, **kwargs)
File "<COMObject <unknown>>", line 8, in Open
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', 'Open method of Workbooks class failed', 'xlmain11.chm', 0, -2146827284), None)
这是一些简化的代码,其中没有发生任何数据更改。使用 ospath absolute 对错误没有帮助。两个系统运行相同的 64 位操作系统和 32 位 excel。然而,第二台机器上的错误仍然存在。我能理解没有真正的区别,这会让我在第二个系统上比第一个系统出错。两台机器都是PC
import xlrd
import openpyxl
import xlwings as xw
from xlwings import constants
import os
import tkinter as tk
from tkinter import filedialog
import datetime
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
print(file_path)
start = time.time()
wb = openpyxl.load_workbook(file_path)
Returns = wb['Prices']
newWs = wb.create_sheet()
newWs.title = "NominalDailyReturns"
benchWS = wb.create_sheet()
benchWS.title = "ActiveDailyReturns"
thirdWs = wb.create_sheet()
thirdWs.title = "RawAnalysis"
thirdWs.column_dimensions["A"].width = 32
name4 = thirdWs.title
print("halfway")
print(os.path.split(file_path))
print("output" + timeName + os.path.split(file_path)[1])
wb.save("output" + timeName + os.path.split(file_path)[1])
xwWb = xw.Book(os.path.abspath("output" + timeName + os.path.split(file_path)[1]))
XnewWs = xwWb.sheets['NominalDailyReturns']
xwWb.save()
xwWb.close()
wb = openpyxl.load_workbook("output" + timeName + os.path.split(file_path)[1])
benchWS = wb['ActiveDailyReturns']
wb.save("output" + timeName + os.path.split(file_path)[1])
xwWb = xw.Book("output" + timeName + os.path.split(file_path)[1])
XthirdWs = xwWb.sheets['RawAnalysis']
xwWb.save()
xwWb.close()
解决方案
我发现了我的代码的问题。当我使用 openpyxl 编辑我的 excel 数据时,其中一个公式是从数字中减去一个单词,从而引发错误并损坏 excel 文件。然后 xlwings 试图打开一个损坏的文件,但我失败了。
所以解决方案是不保存损坏的 excel 文件并期望 xlwings 打开它。
推荐阅读
- java - [Android][RecyclerView] 方法不会覆盖或实现超类型中的方法
- android - 取消选中按钮单击时的单选按钮/单选组 - Android
- sqlite - Azure IMobileServiceSyncTable 解析器堆栈溢出
- java - 从格式化的硬盘驱动器恢复后是否可以恢复我的项目。代码编码已更改,我无法更改
- javascript - Javascript 流联合类型缺少用于 redux 操作的属性
- c# - Excel 阅读器(为 sheet1$ 设置一般变量)
- json.net - Newtonsoft Json.Net 中的 MissingMemberHandling.Error 拦截器
- javascript - 对为什么我的 fetch 方法没有发送 JSON 数据感到困惑
- c++ - C++ - 为什么没有合适的构造函数将 MyClass() 转换为 Myclass?
- php - 将产品类别链接按钮添加到 Woocommerce 单个产品页面