gpt4 book ai didi

python - 使用 xlrd 读取 excel 文件

转载 作者:太空狗 更新时间:2023-10-29 21:01:31 27 4
gpt4 key购买 nike

我在读取由我无法控制的 Perl 脚本编写的 .xls 文件时遇到问题。这些文件在单元格中包含一些格式和换行符。

filename = '/home/shared/testfile.xls'
book = xlrd.open_workbook(filename)
sheet = book.sheet_by_index(0)
for rowIndex in xrange(1, sheet.nrows):
row = sheet.row(rowIndex)

这是抛出以下错误:

_locate_stream(Workbook): seen
0 5 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
20 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
172480= 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
172500 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 3 2
172520 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
173840= 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
173860 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1
173880 1 1 1 1 1 1 1 1
Traceback (most recent call last):
File "/home/shared/xlrdtest.py", line 5, in <module>
book = xlrd.open_workbook(filename)
File "/usr/local/lib/python2.7/site-packages/xlrd/__init__.py", line 443, in open_workbook
ragged_rows=ragged_rows,
File "/usr/local/lib/python2.7/site-packages/xlrd/book.py", line 84, in open_workbook_xls
ragged_rows=ragged_rows,
File "/usr/local/lib/python2.7/site-packages/xlrd/book.py", line 616, in biff2_8_load
self.mem, self.base, self.stream_len = cd.locate_named_stream(qname)
File "/usr/local/lib/python2.7/site-packages/xlrd/compdoc.py", line 393, in locate_named_stream
d.tot_size, qname, d.DID+6)
File "/usr/local/lib/python2.7/site-packages/xlrd/compdoc.py", line 421, in _locate_stream
raise CompDocError("%s corruption: seen[%d] == %d" % (qname, s, self.seen[s]))
xlrd.compdoc.CompDocError: Workbook corruption: seen[2] == 4

我找不到任何关于 CompDocError 或 Workbook 损坏的信息,更不用说 seen[2] == 4 部分了。

最佳答案

来自 pkm 评论(link),问题出在复合文件二进制上

#pip install OleFileIO-PL
import OleFileIO_PL
import pandas as pd

path = 'file.xls'
with open(path,'rb') as file:
ole = OleFileIO_PL.OleFileIO(file)
if ole.exists('Workbook'):
d = ole.openstream('Workbook')
x=pd.read_excel(d,engine='xlrd')
print(x.head())

关于python - 使用 xlrd 读取 excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12705527/

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