Last active
May 20, 2024 07:19
-
-
Save jerilkuriakose/d127e86b75a165938f2e9b11b125cea5 to your computer and use it in GitHub Desktop.
Recover corrupt excel file using Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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') |
Hello! I have tried everything recommended here. but it throws an error:
Traceback (most recent call last): File "...venv\main.py", line 12, in data = file1.readlines() ^^^^^^^^^^^^^^^^^ File "", line 322, in decode File "C:\Program Files\Python311\Lib\encodings\utf_16.py", line 61, in _buffer_decode codecs.utf_16_ex_decode(input, errors, 0, final) UnicodeDecodeError: 'utf-16-le' codec can't decode bytes in position 4-5: illegal encoding
any ideas?
Many thanks
Did you ever find a solution?
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
Hello! I have tried everything recommended here. but it throws an error:
Traceback (most recent call last):
File "...venv\main.py", line 12, in
data = file1.readlines()
^^^^^^^^^^^^^^^^^
File "", line 322, in decode
File "C:\Program Files\Python311\Lib\encodings\utf_16.py", line 61, in
_buffer_decode
codecs.utf_16_ex_decode(input, errors, 0, final)
UnicodeDecodeError: 'utf-16-le' codec can't decode bytes in position 4-5: illegal encoding
any ideas?
Many thanks