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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did you ever find a solution?