首页 > 解决方案 > 在我的程序中从 xlrd 加载 open_workbook 时遇到问题

问题描述

我真的不知道为什么我在使用 xlrd 的 open_workbook 时总是出错......我的想法是我想创建一个网页来上传一个 excel 文件并通过我的代码打开该文件来处理数据。有没有办法解决这个问题?

    def upload(request):
         if "GET" == request.method:
                messages.warning(request, 'no file uploaded')
                return render(request, 'uploadpage/upload.html', {})
            else:
                excel_file = request.FILES["excel_file"]

                wb = xlrd.open_workbook(excel_file)

                # iterating over the rows and
                # getting value from each cell in row


                sheet = wb.sheet_by_name("Summary")

                #looping through each rows in a column to find the seller's info 
                # and storying the values in an array
                seller_info = []
                count = 1
                cfirst, clast = 1,2
                while (count < 6):
                    for colx in range(cfirst, clast):
                        count = count+1
                        seller_info.append(sheet.col_values(colx, start_rowx=count-1, end_rowx=count))

 return render(request, 'uploadpage/upload.html', {"excel_data": seller_info})

这是前端代码

  <div id='upload-container' >

                <span>
                    <h2>Upload Here</h2>
                </span>


                    <div id='input'>
                        <form method="post" enctype="multipart/form-data">
                            {% csrf_token %}
                        <input type="file" name="excel_file">
                        <div id='btn'>
                            <button type="submit">Upload File</button> 
                        </div>
                        </form>
                    </div>

这是我收到的错误消息

TypeError at / expected str, bytes or os.PathLike object, not InMemoryUploadedFile 请求方法:POST 请求 URL:http: //127.0.0.1 :8000/ Django 版本:2.2.5 异常类型:TypeError 异常值:
预期 str,字节或os.PathLike 对象,而不是 InMemoryUploadedFile

标签: pythonpython-3.xxlrd

解决方案


推荐阅读