代码之家  ›  专栏  ›  技术社区  ›  Pedro Queirós

通过django读取xlsx上传

  •  0
  • Pedro Queirós  · 技术社区  · 6 年前

    我正在上传文件(xlsx)通过我的网站,我认为这些文件是正确的上传。

    def upload_file(request):
        print('here',request,request.method,request.FILES)
        if request.method == 'POST':
            file_json= convert_xlsx_to_json(request.FILES['file_query'])
            return render(request, 'drax_internal_dbs/drax_success_reactions.html', {'xlsx_file':file_json})
        return render(request, 'drax_internal_dbs/no_results.html')
    

    这是我的文件:

    <WSGIRequest: POST '/drax_output.html'> POST <MultiValueDict: {'file_query': [<InMemoryUploadedFile: calcitriol_2.xlsx (application/vnd.openxmlformats-officedocument.spreadsheetml.sheet)>]}>
    

    当我试图阅读这个文件时,我遇到了很多问题。我正在使用.read()获取数据,并希望将其转换为数据帧。问题出现在这里,我不知道如何转换数据?我搜索了一整天,还是找不到解决办法。 到目前为止,我一直在尝试:

    • 将文件转换为stringio并使用openpyxl.load_工作簿
    • 将文件转换为bytesio并使用openpyxl.load\u工作簿
    • 使用pandas.read_csv(file_contents=file.read())

    可能还有其他我现在记不起来的方式。 我不想保存这个文件,因为我只想用它在html中生成一个表。

    谢谢!

    1 回复  |  直到 6 年前
        1
  •  0
  •   Pedro Queirós    6 年前

    我找不到xlsx的答案。我转而使用csv文件作为输入文件,它几乎立即生效。