首页 > 解决方案 > 通过 Django 读取 xlsx 上传

问题描述

我正在通过我的网站上传文件(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() 获取数据并希望将其转换为数据帧。问题出现在这里,我不知道如何转换数据?我一直在谷歌搜索,但仍然找不到解决方案。这是我到目前为止所尝试的:

可能还有其他我现在不记得的方式。我不想保存文件,因为我只会用它来生成 HTML 表格。

谢谢!

标签: pythondjango

解决方案


我没有找到 xlsx 的答案。相反,我转而使用 csv 文件作为输入文件,它几乎可以立即工作。


推荐阅读