0

I'm using labview to create and save data from an experiment. Labview itself creates a text file but saves it automatically with a .xls extension (word 1997-2003--it's an old setup that was never changed because it never broke). Whenever I go to open one of the data files, excel spits out this:

"The file you are trying to open, 'name.ext', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?"

I'm generating a lot of data, so I want to use python to sort it out and do some quick analysis over files in a directory.

The problem is that python doesn't like that it's a text file saved with a .xls extension. It can cycle through the directory just fine to get the file names, but whenver I actually try to open the file or do anything with it, I get the error in the image attached. This happens if I change the extension to .xls, .xlsx, or do nothing with it at all and let it try to open the original filename.

error message

I literally have hundreds of these .xls files. I know I can go through, open each one in excel and save as a real excel file by hand, but that will take hours. Can someone please help me figure a way around this error in python?

Dropbox Data File set

*Update. Matlab, when trying to read one of the files using xlsread, says this:

Error using xlsread (line 251) File C:\Users\zane\Documents\Research Projects\PneuFish Project\Data\Nov 28 2016 ATI Data\ATI_Data_2016Y_11M_28D_16h_36m_01s.xls not in Microsoft Excel Format.

Thank you!

Zane Wolf
  • 1
  • 2

2 Answers2

0

You can use the module xlrd.

import xlrd
import csv

def csv_from_excel():

   wb = xlrd.open_workbook('your_workbook.xls')
   sh = wb.sheet_by_name('Sheet1')
   your_csv_file = open('your_csv_file.csv', 'wb')
   wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)

   for rownum in xrange(sh.nrows):
        wr.writerow(sh.row_values(rownum))

   your_csv_file.close()

Taken from This Post

This will convert from .xls to .csv, which is easily manipulated with Python.

Community
  • 1
  • 1
Jason
  • 2,278
  • 2
  • 17
  • 25
  • Doesn't work. In [2]: import convert `In [3]: convert.csv_from_excel() --------------------------------------------------------------------------- FileNotFoundError ----> 1 convert.csv_from_excel() C:\path\convert.py in csv_from_excel() 17 def csv_from_excel(): 18 ---> 19 wb = xlrd.open_workbook('ATI_Data_2016Y_11M_28D_16h_36m_01s.xls') 20 sh = wb.sheet_by_name('Sheet1') 21 your_csv_file = open('your_csv_file.csv', 'wb') FileNotFoundError: [Errno 2] No such file or directory: 'ATI_Data_2016Y_11M_28D_16h_36m_01s.xls'` – Zane Wolf Dec 02 '16 at 20:43
0

You've said that the file is a text file, so don't tell Python that it's an Excel file. Just use Python's open and read it as text, then do whatever you want with it. open doesn't care what extension a file has.

I'm going to guess that the format is actually tab-delimited. From memory, earlier versions of Excel would read in tab-delimited text files with the .xls extension without complaint, whereas csv files would always bring up the text import wizard, so this was a common dodge if saving data intended for Excel from a program that didn't support writing real Excel files.

If you want the LabVIEW code to write real Excel files in future, the Write to Measurement File express VI has an option to write in xlsx format. I'm not sure which version of LabVIEW first introduced this but it's been there for a few years now.

nekomatic
  • 5,988
  • 1
  • 20
  • 27
  • What exact Python command gives this error and have you verified that the file/directory actually exists, and is writable if you're trying to write to it? – nekomatic Dec 03 '16 at 20:45