首页 > 解决方案 > 修复损坏的 excelfile 并显示有关缺少 BOM 的消息

问题描述

所以我的电脑在我修改excel文件的脚本运行期间崩溃了。崩溃后,excelfile 已损坏,我似乎无法让它再次工作。我没有想法,在谷歌上找不到有用的东西,但我真的需要这个文件。希望任何人都可以提供帮助!

这是我尝试过的:

import io
filename = f'TODO/Product_database.xlsm'
file1 = io.open(filename, "r", encoding="utf-16")
data = file1.readlines()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Kees\AppData\Local\Programs\Python\Python37\lib\codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
  File "C:\Users\Kees\AppData\Local\Programs\Python\Python37\lib\encodings\utf_16.py", line 67, in _buffer_decode
    raise UnicodeError("UTF-16 stream does not start with BOM")
UnicodeError: UTF-16 stream does not start with BOM
import win32com.client
import os
o = win32com.client.Dispatch("Excel.Application")
o.Visible = False
filename = os.getcwd() + '/' + 'TODO/Product_database.xlsm'
wb = o.Workbooks.Open(filename)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<COMObject <unknown>>", line 8, in Open
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', 'Het bestand Product_database.xlsm kan niet worden geopend omdat de bestandsindeling of de bestandextensie niet geldig is. Controleer of het bestand niet is beschadigd en of de bestandsextensie overeenkomt met de indeling van het bestand.', 'xlmain11.chm', 0, -2146827284), None)```

标签: pythonexcel

解决方案


推荐阅读