xlrd - issue on opening file
<p>I'm using <strong>xlrd 0.9.4</strong> and I would like verify if the file that I must open is valid.</p>

<p>To do this, I wrote this code <a href="http://stackoverflow.com/questions/28645167/how-to-check-if-valid-excel-file-in-python-xlrd-library">in according with this question</a>:</p>

<pre><code>try:
    book = xlrd.open_workbook(file_path)
    print "Done"
except XLRDError:
    print "Wrong type of file."
</code></pre>

<p>where <strong>file_path</strong> is the path of my file. </p>

<p>This works fine, the problem is the following. First of all I have a valid .xls file, so script prints <em>Done</em>. Now, assume that the valid .xls file is renamed (also extension), for example from test.xls to test.txt.</p>

<p>If I run the script, i have the same result (<em>Done</em>). </p>

<p>Instead, if I use a "real" .txt file (empty or with some text), the script prints <em>Wrong type of file.</em>  </p>

<p>This behavior happens because the "structure" of the file is not changed? Am I doing something wrong? There is another type of Exception that I can add to <strong>except</strong> branch?</p>

<p>Thanks in advance</p>

<p>You can see how to xlrd check the file before reading. In <a href="https://github.com/python-excel/xlrd/blob/master/xlrd/compdoc.py" rel="nofollow">xldr source</a> at lines 18-19 defined a Â«magicÂ» bytes. First bytes of file compared with this byte sequence at line 85. If its not equal exception will be rise. File extention not involved.</p>

<p>Signatures for different file types can be found <a href="http://www.garykessler.net/library/file_sigs.html" rel="nofollow">there</a>.</p>

python excel xlrd