如何在python中检查上传的文件是csv还是xls?

时间:2014-05-07 10:51:53

标签: python excel csv

How to check is upload file is CSV or XLS。 如何在python中检查它。我正在将文件导入openerp中的二进制字段,该字段可以作为二进制对象进行检索。我需要读取文件并将数据导入表中。用户可以上传csv或xls文件。只知道我可以使用csv包或xlrd包。

3 个答案:

答案 0 :(得分:5)

hex signature for an .xls文件如下:

Excel电子表格子标题(MS Office)

09 08 10 00 00 06 05 00 [512 byte offset]

您可以在Wikipedia上了解其他各种签名。

我相信你可以做这样的事情。这是未经测试的,但你可以摆弄它直到它工作。如有任何建议或更改,请留下评论。谢谢!

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.'

更新1

经过测试,我可以验证它是否适用于检测.xls个文件。

更新2

我开发了一个程序来确定文件是xls还是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)

以下是输出:

('0908100000060500', 8)
"spreadsheet.xls" is an xls.
('504b0506', 4)
"spreadsheet.xlsx" is an xlsx.

答案 1 :(得分:2)

只是对PolyWhirl帖子的扩展,其中包含一些我遇到的边缘案例。

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

答案 2 :(得分:1)

您可以尝试,如果不能正常工作,请尝试其他方法。

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."