pywintypes.com_error: (-2147352567,'Exception occured.', (0, None, None, None, 0, -2147352566)

pywintypes.com_error: (-2147352567,'Exception occured.', (0, None, None, None, 0, -2147352566)

背景: 我正在使用 xlwings 打开一个 xlsx 文件并使用一个列表(元组)来接收 table 的所有数据。代码如下

def get_excel_all_data(path: string, input_sheet_name: string):    
    app = xw.App(visible=True, add_book=False)
    app.display_alerts = False
    # path is my file location
    wb = app.books.open(path, update_links=True)
    # Create a tuple to receive data
    list_value = ()
    # Get the number of sheets
    sheet_num = len(wb.sheets)
    # Iterate through the sheet to get the specified name
    for i in range(0, sheet_num):
        sht_list = wb.sheets[i].name
        # using re to match whether the Sheet name is the same as Sheet1
        result = re.search(input_sheet_name, sht_list)

        if result is not None:
            # Set the active worksheet to the found sheet
            act_sht = wb.sheets(input_sheet_name)
            # act_sht.api.ShowAllData()
            # Get all used cells of the sheet
            last_cell = act_sht.used_range.last_cell
            # Get row and column
            row = last_cell.row
            column = last_cell.column
            # Write to tuple
            list_value = act_sht.range((1, 1),(row, column)).value
            break

当代码运行写入元组时,崩溃:

Python告诉我:

Traceback (most recent call last):
  File "d:\pythonProject\create_doc.py", line 12, in <module>
    all_data = universal.get_excel_all_data(r'D:\application\data\test.XLSX', "Sheet1")
  File "d:\pythonProject\Universal\universal.py", line 87, in get_excel_all_data
    list_value = act_sht.range((1, 1),(row, column)).value
  File "C:\Users\sin\anaconda3\lib\site-packages\xlwings\main.py", line 1993, in value
    return conversion.read(self, None, self._options)
  File "C:\Users\sin\anaconda3\lib\site-packages\xlwings\conversion\__init__.py", line 32, in read
    pipeline(ctx)
  File "C:\Users\sin\anaconda3\lib\site-packages\xlwings\conversion\framework.py", line 66, in __call__
    stage(*args, **kwargs)
  File "C:\Users\sin\anaconda3\lib\site-packages\xlwings\conversion\standard.py", line 96, in __call__
    c.value = c.range.raw_value
  File "C:\Users\sin\anaconda3\lib\site-packages\xlwings\main.py", line 1586, in raw_value
    return self.impl.raw_value
  File "C:\Users\sin\anaconda3\lib\site-packages\xlwings\_xlwindows.py", line 835, in raw_value
    return self.xl.Value
  File "C:\Users\sin\anaconda3\lib\site-packages\xlwings\_xlwindows.py", line 126, in __getattr__
    v = getattr(self._inner, item)
  File "C:\Users\sin\anaconda3\lib\site-packages\win32com\client\__init__.py", line 583, in __getattr__
    return self._ApplyTypes_(*args)
  File "C:\Users\sin\anaconda3\lib\site-packages\win32com\client\__init__.py", line 572, in _ApplyTypes_
    self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args),
pywintypes.com_error: (-2147352567, 'Exception occured', (0, None, None, None, 0, -2147352566), None)

一些数据:

xlsx文件已经使用10000行52columns.But程序自动获取行为10000,列为53

这是我试过的。

  1. 尝试读取范围 (1,1),没问题

  2. 读取范围((1,1), (100, 52)),没问题

  3. 手动将行改为10000,列改为53,即range((1,1), (10000, 53)),同样出现上面的错误提示。

  4. 如果读取其他文件(没有那么多数据),没问题

  5. 手动复制整个sheet到新的xlsx文件也是不可读,报错一样

天哪!我知道发生了什么!

我是用二分法来查找具体出现问题的行列

然后我发现有些数据显示为水平线。 Excel告诉我这是日期格式,但是值太大,所以显示为#######。

当我将其更改为正确的格式时,代码运行正常。

在使用xlwings之前,我从来没有想过####上的数据不可用!

谢谢,buddy,@moken @KarlKnechtel.You两位给了我很好的想法