Skip to content

Instantly share code, notes, and snippets.

@jerilkuriakose
Last active May 20, 2024 07:19
Show Gist options
  • Save jerilkuriakose/d127e86b75a165938f2e9b11b125cea5 to your computer and use it in GitHub Desktop.
Save jerilkuriakose/d127e86b75a165938f2e9b11b125cea5 to your computer and use it in GitHub Desktop.
Recover corrupt excel file using Python
# Changing the data types of all strings in the module at once
from __future__ import unicode_literals
# Used to save the file as excel workbook
# Need to install this library
from xlwt import Workbook
# Used to open to corrupt excel file
import io
filename = r'SALEJAN17.xls'
# Opening the file using 'utf-16' encoding
file1 = io.open(filename, "r", encoding="utf-16")
data = file1.readlines()
# Creating a workbook object
xldoc = Workbook()
# Adding a sheet to the workbook object
sheet = xldoc.add_sheet("Sheet1", cell_overwrite_ok=True)
# Iterating and saving the data to sheet
for i, row in enumerate(data):
# Two things are done here
# Removeing the '\n' which comes while reading the file using io.open
# Getting the values after splitting using '\t'
for j, val in enumerate(row.replace('\n', '').split('\t')):
sheet.write(i, j, val)
# Saving the file as an excel file
xldoc.save('myexcel.xls')
@thefugitivekind
Copy link

Hello @jerilkuriakose, I'm using this code but it raise the following error: 'utf-16-le' codec can't decode bytes in position 86-87: illegal encoding

Do you have any idea how I can fix this error?

Did you ever find a solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment