How to check is upload file is CSV or XLS . How to check it in python. I'm importing a file to a binary field in openerp which can be retrived as a binary object. I need to read the file and import the data to a table. User can upload csv or xls file. By knowing only I can use the csv package or xlrd package.
Asked
Active
Viewed 1.1k times
4
-
Wouldn't an XLS/XLSX be encoded in bytes? The CSV is just a standard text file. – Mr. Polywhirl May 07 '14 at 10:52
-
1In general you only can guess. The only clean solution is to get the "Content-Type" of the HTTP request. Binary data can be anything if you don't specify what it is. That is the reason for the Content-Type field. – PeterMmm May 07 '14 at 11:21
-
See my solution below. I have tested it for validity. – Mr. Polywhirl May 07 '14 at 13:11
3 Answers
9
The hex signature for an .xls
file is the following:
Excel spreadsheet subheader (MS Office)
09 08 10 00 00 06 05 00 [512 byte offset]
You can read about the other various signatures on Wikipedia.
I believe that you can do something like this. This is untested, but you can fiddle around with it until it works. Please leave comments for any suggestions or changes. Thanks!
xls_sig = b'\x09\x08\x10\x00\x00\x06\x05\x00'
offset = 512
size = 8
with open('spreadsheet.xls', 'rb') as f:
f.seek(offset) # Seek to the offset.
bytes = f.read(size) # Capture the specified number of bytes.
if bytes == xls_sig:
print 'Uploaded file is an xls.'
else:
print 'File is not an xls.'
Update 1
Tested this and I can verify that it works for detecting .xls
files.
Update 2
I developed a program to determine if the file is an xls or xlsx:
import codecs
xlsx_sig = b'\x50\x4B\x05\06'
xls_sig = b'\x09\x08\x10\x00\x00\x06\x05\x00'
filenames = [
('spreadsheet.xls', 0, 512, 8),
('spreadsheet.xlsx', 2, -22, 4)]
for filename, whence, offset, size in filenames:
with open(filename, 'rb') as f:
f.seek(offset, whence) # Seek to the offset.
bytes = f.read(size) # Capture the specified number of bytes.
print codecs.getencoder('hex')(bytes)
if bytes == xls_sig:
msg = '"{}" is an xls.'
elif bytes == xlsx_sig:
msg = '"{}" is an xlsx.'
else:
msg = '"{}" is not an Excel document.'
print msg.format(filename)
Here is the output:
('0908100000060500', 8)
"spreadsheet.xls" is an xls.
('504b0506', 4)
"spreadsheet.xlsx" is an xlsx.

Mr. Polywhirl
- 42,981
- 12
- 84
- 132
-
This method validates the file but when I try to read the xlsx file using XLRD I get an unsupported or corrupt file error. – aliasav Apr 10 '15 at 15:05
-
1Just as an edge case. If you have a .xls file saved by LibreOffice Calc, the offset is 1536. – Bossman Sep 04 '20 at 14:40
-
Just a note: xlsx_sig = b'\x50\x4B\x05\06' is a bit misleading; should read open_xml_sig as files with this signature can also be .docx or .pptx files. – thomiel Jan 19 '22 at 16:16
5
Just an extension to PolyWhirl's post with a few edge cases that i came across.
def isExcelDoc(file):
excelSigs = [
('xlsx', b'\x50\x4B\x05\x06', 2, -22, 4),
('xls', b'\x09\x08\x10\x00\x00\x06\x05\x00', 0, 512, 8), #Saved from Excel
('xls', b'\x09\x08\x10\x00\x00\x06\x05\x00', 0, 1536, 8), #Saved from LibreOffice Calc
('xls', b'\x09\x08\x10\x00\x00\x06\x05\x00', 0, 2048, 8) #Saved from Excel then saved from Calc
]
for sigType, sig, whence, offset, size in excelSigs:
with open(file, 'rb') as f:
f.seek(offset, whence)
bytes = f.read(size)
if bytes == sig:
return True
return False

Bossman
- 1,416
- 1
- 11
- 17
4
You can try and if not working, try another method.
import xlrd
import csv
try:
# reading the file by xlrd
...
print "Thanks for your Excel file"
except: # if you find specific Exception types, use them here
try:
# reading as CSV file
...
print "thanks for your CSV file"
except: # if you find specific Exception types, use them here
print "sorry, now way, give me some usable file."

Jan Vlcinsky
- 42,725
- 12
- 101
- 98
-
I would consider this to be the most Pythonic answer. See https://wiki.c2.com/?LookBeforeYouLeap – John Anderson Dec 22 '20 at 19:58