[Python] XLS Old File Format Python Read Issue

Discussão em 'Python' iniciado por Stack, Fevereiro 17, 2025.

  1. Stack

    Stack Membro Participativo

    Good Day!

    I am having an old file format xls file (Microsoft Excel 97-2003 Worksheet)

    This file is opening in the excel app without any issues. But when i try to read with python xlrd engine.It throws the below error,

    " xlrd.biffh.XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'\x03|\x0b\x0e\xfb\x9a\x00\x00' "

    Code:

    df = pd.read_excel('my_file.xls', engine='xlrd')


    When i try to debug the error, i found out that commonly xls file will follow the conventional hexadecimal bytes convention (⁠D0 CF 11 E0 A1 B1 1A E1⁠) at the BOF (beginning of the file). But in my file the bytes are (03 7C 0B 0E FB 9A 00 00 21)

    I don't know the root cause of this formatting, i need to convert/read this file programmatically.

    I found a solution in windows using win32 excel api

    Windows Conversion code:

    import win32com.client as win32
    fname = "my_file.xls"
    excel = win32.gencache.EnsureDispatch('Excel.Application')
    wb = excel.Workbooks.Open(fname)
    wb.SaveAs(fname+"x", FileFormat = 51)
    wb.Close()
    excel.Application.Quit()


    But when i try to do the same in linux, it is not working since win32 api will not work in linux i guess.

    Kindly let me know if there are any solutions to handle this in linux

    (**Note: I dont think my_file is a xml file also since i have tried with beautifulSoup also)

    Continue reading...

Compartilhe esta Página