本文介绍了错误:不支持的格式,或损坏的文件:预期的 BOF 记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打开一个 xlsx 文件并只打印其中的内容.我一直遇到这个错误:

I am trying to open a xlsx file and just print the contents of it. I keep running into this error:

import xlrd
book = xlrd.open_workbook("file.xlsx")
print "The number of worksheets is", book.nsheets
print "Worksheet name(s):", book.sheet_names()
print

sh = book.sheet_by_index(0)

print sh.name, sh.nrows, sh.ncols
print

print "Cell D30 is", sh.cell_value(rowx=29, colx=3)
print

for rx in range(5):
    print sh.row(rx)
    print

它打印出这个错误

raise XLRDError('Unsupported format, or corrupt file: ' + msg)
xlrd.biffh.XLRDError: Unsupported format, or corrupt file: Expected BOF record; found    'xffxfeTx00ix00mx00'

谢谢

推荐答案

该错误消息与 XLS 文件的 BOF(文件开头)记录有关.但是,该示例显示您正在尝试读取 XLSX 文件.

The error message relates to the BOF (Beginning of File) record of an XLS file. However, the example shows that you are trying to read an XLSX file.

有两个可能的原因:

  1. 您的 xlrd 版本较旧,不支持读取 xlsx 文件.
  2. XLSX 文件经过加密,因此以 OLE 复合文档格式存储,而不是 zip 格式,因此在 xlrd 看来它是旧格式的 XLS 文件.

仔细检查您实际上使用的是最新版本的 xlrd.在一个单元格中打开一个包含数据的新 XLSX 文件应该可以验证这一点.

Double check that you are in fact using a recent version of xlrd. Opening a new XLSX file with data in just one cell should verify that.

但是,我猜您遇到了第二种情况并且文件已加密,因为您在上面声明您已经在使用 xlrd 版本 0.9.2.

However, I would guess the you are encountering the second condition and that the file is encrypted since you state above that you are already using xlrd version 0.9.2.

如果您明确应用工作簿密码以及密码保护某些工作表元素,则 XLSX 文件将被加密.因此,即使您不需要密码来打开它,也可以拥有加密的 XLSX 文件.

XLSX files are encrypted if you explicitly apply a workbook password but also if you password protect some of the worksheet elements. As such it is possible to have an encrypted XLSX file even if you don't need a password to open it.

更新:请参阅@BStew 的第三个更可能的答案,该文件是由 Excel 打开的.

Update: See @BStew's, third, more probable, answer, that the file is open by Excel.

这篇关于错误:不支持的格式,或损坏的文件:预期的 BOF 记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-19 07:38