Python之xlrd读Excel文件问题解决 (python xlrd unsupported format, or corrupt file.)

最近使用PyChram创建xls文件导致不能读取写入该文件
解决:在桌面上手动创建Excel文件即可解决。

import xlrd
import xlutils.copy
#打开一个workbook
filename = r'H:\Timed_scheduling\data.xls'
rb = xlrd.open_workbook(filename, encoding_override="utf-8")
wb = xlutils.copy.copy(rb)
#获取sheet对象,通过sheet_by_index()获取的sheet对象没有write()方法
ws = wb.get_sheet(0)
#写入数据
ws.write(1, 1, 'GG')
#添加sheet页
wb.add_sheet('sheetnnn2',cell_overwrite_ok=True)
#利用保存时同名覆盖达到修改excel文件的目的,注意未被修改的内容保持不变
wb.save(filename)

问题一
引发xlrderrror(“文件大小为0字节”)
xlrd.biffh.xlrd错误:文件大小为0字节

The error:


Traceback (most recent call last):
File “H://Timed_scheduling/testcase.py”, line 21, in
rb = xlrd.open_workbook(filename, encoding_override=“utf-8”)
File “C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\xlrd_init_.py”, line 157, in open_workbook
ragged_rows=ragged_rows,
File “C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\xlrd\book.py”, line 88, in open_workbook_xls
ragged_rows=ragged_rows,
File “C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\xlrd\book.py”, line 616, in biff2_8_load
raise XLRDError(“File size is 0 bytes”)
xlrd.biffh.XLRDError: File size is 0 bytes

问题二
raise xlrderor(‘不支持的格式,或损坏的文件:’+msg)
xlrd.biffh.xlrderrror:不支持的格式,或损坏的文件:需要的BOF记录;找到B’11\r\n’

The error:


Traceback (most recent call last):
File “H:/Timed_scheduling/testcase.py”, line 21, in
rb = xlrd.open_workbook(filename, encoding_override=“utf-8”)
File “C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\xlrd_init_.py”, line 157, in open_workbook
ragged_rows=ragged_rows,
File “C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\xlrd\book.py”, line 92, in open_workbook_xls
biff_version = bk.getbof(XL_WORKBOOK_GLOBALS)
File “C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\xlrd\book.py”, line 1278, in getbof
bof_error(‘Expected BOF record; found %r’ % self.mem[savpos:savpos+8])
File “C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\xlrd\book.py”, line 1272, in bof_error
raise XLRDError(‘Unsupported format, or corrupt file: ’ + msg)
xlrd.biffh.XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b’11\r\n’

解决方法
在桌面上手动创建Excel文件即可解决!!!

你可能感兴趣的:(python)