报错代码如下:
filePath='test.xls'
data=pd.read_excel(filePath)
print(data.head())
报错内容如下:
Traceback (most recent call last):
File "e:/PyCharm/Demo/TestFileDirectory/test.py", line 18, in <module>
data=pd.read_excel(filePath,data__only=True)
File "D:Program FilesPython37libsite-packagespandasutil\_decorators.py", line 208, in wrapper
return func(*args, **kwargs)
File "D:Program FilesPython37libsite-packagespandasioexcel\_base.py", line 310, in read_excel
io = ExcelFile(io, engine=engine)
File "D:Program FilesPython37libsite-packagespandasioexcel\_base.py", line 819, in __init__
self._reader = self._engines[engine](self._io)
File "D:Program FilesPython37libsite-packagespandasioexcel\_xlrd.py", line 21, in __init__
super().__init__(filepath_or_buffer)
File "D:Program FilesPython37libsite-packagespandasioexcel\_base.py", line 359, in __init__
self.book = self.load_workbook(filepath_or_buffer)
File "D:Program FilesPython37libsite-packagespandasioexcel\_xlrd.py", line 36, in load_workbook
return open_workbook(filepath_or_buffer)
File "D:Program FilesPython37libsite-packagesxlrd\__init__.py", line 157, in open_workbook
ragged_rows=ragged_rows,
File "D:Program FilesPython37libsite-packagesxlrdook.py", line 88, in open_workbook_xls
ragged_rows=ragged_rows,
File "D:Program FilesPython37libsite-packagesxlrdook.py", line 636, in biff2_8_load
cd.locate_named_stream(UNICODE_LITERAL(qname))
File "D:Program FilesPython37libsite-packagesxlrdcompdoc.py", line 399, in locate_named_stream
d.tot_size, qname, d.DID+6)
File "D:Program FilesPython37libsite-packagesxlrdcompdoc.py", line 434, in _locate_stream
% (qname, found_limit * sec_size)
xlrd.compdoc.CompDocError: Workbook: size exceeds expected 17920 bytes; corrupt?
已试用但未成功方案:
import xlrd
import pandas as pd
#方式一:
data=pd.read_excel(filePath)
print(data.head())
#方式二:
x1 = xlrd.open_workbook(filePath)
mySheet = x1.sheets()[0]
nrows = mySheet.nrows
print("row num:", nrows)
#方式三:
f = open(filePath, 'rb')
lines = f.readlines()
for line in lines:
line = line.decode('gbk').encode('utf8')
print (line)
最后的解决方案如下:
from win32com.client import Dispatch
filePath='test.xls'
xl = Dispatch('Excel.Application')
wb = xl.Workbooks.Open(filePath)
ws = wb.Worksheets(1)
info = ws.UsedRange
rows = info.Rows.Count
print (rows)