gpt4 book ai didi

python - xlwings打开错误: not opening excel workbook getting an error upon call wb.

转载 作者:行者123 更新时间:2023-12-01 07:46:34 31 4
gpt4 key购买 nike

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 打开它。

关于python - xlwings打开错误: not opening excel workbook getting an error upon call wb.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56434336/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com