读取.xls文件报错:XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b‘MIME-Ver

解决方式:

1、将xls文件后缀转换为csv文件,再进行读取。

这种方式虽然读取后程序不再报错,但文件格式会发生改变,影响操作。

2、 将xls文件另存为xlsx文件

如果读取的文件不多,对于每个文件,以次打开,另存为Microsoft Excel 97-2003文件(*.xls) ,再对另存的.xls文件进行读取即可

如果读取的文件有很多个,需要批量转换为xlsx文件。批量转换方法:

#encoding: utf-8
from ctypes import *
import time
import win32com.client as win32
import os
def transform(parent_path,out_path):
    fileList = os.listdir(parent_path)  #文件夹下面所有的文件
    num = len(fileList)
    for i in range(num):
        file_Name = os.path.splitext(fileList[i])   #文件和格式分开
        print(file_Name)
        if file_Name[1] == '.xls':
            print(file_Name[0])
            transfile1 = parent_path+'/'+fileList[i]  #要转换的excel
            print(transfile1)
            transfile2 = out_path+'/'+file_Name[0]    #转换出来excel
            excel=win32.gencache.EnsureDispatch('excel.application')
            pro=excel.Workbooks.Open(transfile1)   #打开要转换的excel
            pro.SaveAs(transfile2 + ".xlsx", FileFormat=51)  # 另存为xlsx格式
            pro.Close()
            excel.Application.Quit()

if __name__=='__main__':
# 注意:这里要写绝对路径。否则会报错:pywintypes.com_error: (-2147352567, '发生意外。', (0, 'Microsoft Excel', '抱歉,无法找到 *.xlsx。是否可能被移动、重命名或删除?', 'xlmain11.chm', 0, -2146827284), None)
    path1=r"E:\practice\jupyternotebook\DaDong\dataset/2021年1月大东厂进水口"  #待转换文件所在目录
    path2=r"E:\practice\jupyternotebook\DaDong\dataset/111"  #转换文件存放目录
    transform(path1, path2)

转换后再读取.xlsx文件就可以了 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值