如何修复“文件不是zip文件”

时间:2019-10-16 13:36:55

标签: django excel openpyxl

我正在尝试上传一些表示有关产品信息的数据。 我的excel文件的第一行代表产品的ID。 列单元格中的数据(从第二行开始)代表序列号。

我无法成功上传我的.xls文件,并返回错误文件不是zip文件。

我的观点

def excel_view(request):
    if "GET" == request.method:
        return render(request, 'excel/excel_form.html', {})
    else:
        excel_file = request.FILES["excel_file"]

        # you may put validations here to check extension or file size

        wb = openpyxl.load_workbook(excel_file)
        sheet_obj = wb.active 

        # getting a particular sheet by name out of many sheets
        worksheet = wb["Sheet1"]
        # print(worksheet)

        excel_data = list()
        # iterating over the rows and
        # getting value from each cell in row

        #flag=0
        header_list=[] #list to store drug index
        input_dict={} #dict to store serial numbers per drug
        i = 0 #index of columns
        j = 0 #index of rows
        for row in worksheet.iter_rows():
             ....

我的模板

       <title>
            Excel file upload and processing
        </title>
    <body style="margin-top: 30px;margin-left: 30px;">

      <h1>Excel file upload and processing</h1>
        <form action="{% url 'excel_functionality' %}" method="post" enctype="multipart/form-data">
            {% csrf_token %}
            <input type="file"
                   title="Upload excel file"
                   name="excel_file"
                   style="border: 1px solid black; padding: 5px;"
                   required="required">
            <p>
            <input type="submit"
                   value="Upload"
                   style="border: 1px solid green; padding:5px; border-radius: 2px; cursor: pointer;">
        </form>

我的回溯

Environment:


Request Method: POST
Request URL: http://example/excel/

Django Version: 1.11.16
Python Version: 2.7.12
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'bootstrap3',
 'bootstrap_themes',
 'intranet',
 'crispy_forms',
 'fm',
 'dal',
 'dal_select2',
 'django_crontab',
 'django_tables2',
 'django_filters']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/var/www/vhosts/intranet.health-nutrition.gr/health_nutrition/intranet/views.py" in excel_view
  3355.         wb = openpyxl.load_workbook(excel_file)

File "/usr/local/lib/python2.7/dist-packages/openpyxl/reader/excel.py" in load_workbook
  174.     archive = _validate_archive(filename)

File "/usr/local/lib/python2.7/dist-packages/openpyxl/reader/excel.py" in _validate_archive
  124.         archive = ZipFile(f, 'r', ZIP_DEFLATED)

File "/usr/lib/python2.7/zipfile.py" in __init__
  770.                 self._RealGetContents()

File "/usr/lib/python2.7/zipfile.py" in _RealGetContents
  811.             raise BadZipfile, "File is not a zip file"

Exception Type: BadZipfile at /excel/
Exception Value: File is not a zip file

为什么会这样,有什么主意吗?

0 个答案:

没有答案