In this post you can find how to recover corrupted .xls or .xlsx files, using Python. Corrupt in the sense we are able to view the file in excel, but not using Python. The following was the error that is displayed while opening the file using excel.

And this is the error when you trying to open it through python: XLRDError: Unsupported format, or corrupt file: Expected BOF record; found ‘\xff\xfe\r\x00\n\x00\r\x00’

But don’t worry, the solution follows!

import win32com.client
import os

file_dir = r"""C:\Users\Heyo\Desktop\python usefull scripts\ConvertCorruptedFiles\CorruptedFiles""" 
for filename in os.listdir(file_dir):
    print(filename)
    file= os.path.splitext(filename)[0]
    o = win32com.client.Dispatch("Excel.Application")
    o.Visible = False
    wb = o.Workbooks.Open
        ("C:\\Users\\Heyo\\Desktop\\python usefull scripts\\ConvertCorruptedFiles
          \\CorruptedFiles\\" + filename) 
    wb.ActiveSheet.SaveAs
        ("C:\\Users\\Heyo\\Desktop\\python usefull scripts\\ConvertCorruptedFiles
          \\RestoredFiles\\" + file + ".xlsx", 51) 
    o.Application.Quit()
Author

Write A Comment