XLSXWRITER in Python received Error Excel found unreadable content in

Hi,
I am using xlsxwriter to convert csv to Excel files. For some reason, the line that convert to float and format to 6 decimal places caused the error when opening the xlsx file. If I would to comment out the line or just write it out to excel as string, the error goes away. Any help is appreciated.

Here’s the snippet of the code. The code splits up one csv into multiple excel due to excel limitation of number of rows.

for row in reader:
for col in range(len(row)):
if header[col] == ‘In-Service Date’:
worksheet.write(row_count, col, row[col], datefmt)
elif header[col] == ‘Length’ or header[col] == ‘Length Installed’:
try:
worksheet.write(row_count, col, float(row[col]), numfmt1)
except ValueError, e:
worksheet.write(row_count, col, row[col])
#print “error”, e, row_count
# For some reason the elif below corrupted the excel file. Possibly the defect in the xlsxwriter.
#elif header[col] in [‘First Point Latitude’, ‘First Point Longitude’,‘Last Point Latitude’, ‘Last Point Longitude’]:
# worksheet.write(row_count, col, float(row[col]), numfmt3)
elif header[col] == ‘ERP ID’:
worksheet.write(row_count, col, row[col].lstrip(“0”))
else:
worksheet.write(row_count, col, row[col])
row_count += 1

Well, this type of error commonly appears when the Excel file got corrupted or corruption of the one or more object or even the virus or malware infection

So in this case to get rid of the unreadable content follow the given steps:

  • First Try to open your ‘.xls or .xlsx’ file by making it ‘read-only’
  • Now click on the ‘Office button’ and then select ‘save’ for a new document or ‘save as’ for the previously saved document.
  • Next, you have to click on the ‘Tools’ and then select ‘General Option’.
  • Last but not the least, click on the ‘read-only’ check-box to make document read-only.
  • Now you have to open a new and blank ‘.xls/.xlsx’ file and then copy the whole data from the corrupted Excel file to this new one. Now simply save this file and try to open it again