You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python读取XLS遇格式不支持错误,求PyExcel/Pandas外的解决方案

解决XLS文件读取失败的替代Python包

问题重现

尝试用pyexcel读取XLS文件时触发以下错误:

import pyexcel as pe
def convert_xls_to_xlsx(input_path, output_path):
    # Read the XLS file using pyexcel_xls
    data = pe.read(file_name = input_path)
    print("read ok")

报错信息:

xlrd.biffh.XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'\xff\xfe\x8f^\xf7S\t\x00'

可用替代包(排除pandas、openpyxl)

  • xlwings:通过调用本地安装的Excel程序读取文件,依赖Office Excel(Windows/macOS),能处理多数特殊或损坏的XLS文件,支持读写转换。
    示例代码:
    import xlwings as xw
    
    def convert_xls_to_xlsx(input_path, output_path):
        with xw.App(visible=False) as app:
            wb = xw.Book(input_path)
            wb.save(output_path, format_='xlsx')
            wb.close()
    
  • pywin32(仅Windows):直接调用Windows的Excel COM接口,功能类似xlwings但更底层,适合深度定制操作。
    示例代码:
    import win32com.client
    
    def convert_xls_to_xlsx(input_path, output_path):
        excel = win32com.client.Dispatch("Excel.Application")
        excel.Visible = False
        wb = excel.Workbooks.Open(input_path)
        wb.SaveAs(output_path, FileFormat=51)  # 51对应xlsx格式
        wb.Close()
        excel.Quit()
    
  • olefile:如果文件是OLE容器格式的特殊XLS,可以用这个包解析底层结构,适合处理非常规的XLS文件,但需要自行处理数据提取逻辑。
    示例代码片段:
    import olefile
    
    with olefile.OleFileIO(input_path) as ole:
        # 查看OLE文件内的流结构
        print(ole.listdir())
        # 读取具体流数据,需自行解析内容
        if ole.exists('Workbook'):
            workbook_data = ole.openstream('Workbook').read()
    

内容的提问来源于stack exchange,提问作者yuriamao

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 07:47:15