在 WINDOWS 上使用 XLRD 0.9.2 版和 python 2.7...
我创建一个临时文件,然后使用 XLRD 读取文件
data = self.excel_file
path = default_storage.save('temp/temp.xls', ContentFile(data.read()))
tmp_file = os.path.join(settings.MEDIA_ROOT, path)
workbook = xlrd.open_workbook(tmp_file)
一旦我尝试打开 XLS 文件,它就会返回错误
xlrd.biffh.XLRDError: Unsupported format found '\xd0\xcf\x11\xe0\xa1\xb1\x00\x00'
所以我猜该文件没有正确保存在上面或其他东西上。不知道该怎么做,因为这在使用 django admin 在文件字段中上传文件时有效。
在 python 中将这样的文件保存到 django 模型会产生上述问题:
from django.core.files import File
p = Foo()
p.excel_file.save(file_name, File(data))
p.save()