python - 修复损坏的 excelfile 并显示有关缺少 BOM 的消息
问题描述
所以我的电脑在我修改excel文件的脚本运行期间崩溃了。崩溃后,excelfile 已损坏,我似乎无法让它再次工作。我没有想法,在谷歌上找不到有用的东西,但我真的需要这个文件。希望任何人都可以提供帮助!
这是我尝试过的:
在 Excel 中,“打开并修复”。这会导致出现有关错误扩展名的消息。
在 Excel 中,使用公式访问特定的单元格值。给出一个值错误
在 python 中使用 io.open 和 readlines
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
- 在 python 中,使用 win32com
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)```
解决方案
推荐阅读
- haskell - Haskell:对函数列表参数中的每个元素执行函数
- python - 如何将 pandas 数据框写入使用 Kerberos 身份验证的 HIVE 数据库
- java - 给定 AffineTransform,我如何确定旋转、镜像、缩放等?
- c++ - 模板:类专业化
- python - Scrapy + python:csv文件未按正确顺序导出
- angular - 如何在非本地主机的任何其他地方运行 Angular 应用程序(例如)
- javascript - 如何在各个地方呈现指令内的内容
- sql - 基于过去 1 年的 SQL 中的 Z 分数
- c# - 使用 JSON.NET 反序列化 C# 类中缺少默认构造函数的 json
- javascript - 从 `async` 函数返回 `void`