html - 在使用html表单上传的Django中读取Excel文件

标签 html django excel python-3.x web

我正在学习 django 框架并尝试读取 excel 文件

我想使用 html 表单(POST 方法)上传文件并想在 python 中读取以进行计算,所以我应该怎么做真的很困惑? 代码在这里 表单.html

<form method="POST" action="minmax_ans.html">{% csrf_tocken %}
   {{form.as_p}}
   <p><b><h2>Upload File</h2></b><p>
   <input type="file" name="file" value="{{ source.title }}">
   <input type="submit">
</form>

View .py

def upload_file(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            handle_uploaded_file(request.FILES['file'])
            return HttpResponseRedirect('/success/url/')
    else:
        form = UploadFileForm()
    return render(request, 'upload.html', {'form': form})

如果它是一个 excel 文件,我想使用通过 html 表单上传的文件中的数据进行一些计算,并将结果显示在我尚未创建的 result.html 上,因为我无法读取该文件

最佳答案

你可以这样做

import pandas as pd
def handle_uploaded_file(f):
    pd.read_excel(f, index_col=0)

Pandas read_excel 接受 str、ExcelFile、xlrd.Book、路径对象或类文件对象中的参数 io。

关于html - 在使用html表单上传的Django中读取Excel文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43332043/

相关文章:

javascript - 如何在 AngularJS 中保存动态表的所有行?

html - 打印纸张尺寸和内容插图

javascript - 如何使用 html 'a' 链接打开 safari 中的现有选项卡?

php - 在 Laravel 中将 Eloquent 导出到 Excel 时如何包含列标题?

html - Bootstrap 水平导航栏在 Internet Explorer 中崩溃

python - Django Unittest 不加载固定装置

python - Wagtail Recaptcha Localhost - SSL : CERTIFICATE_VERIFY_FAILED

python - 带有扩展的多个 Django 管理参数

excel - 范围仅在从引用的工作表中调用时才有效

excel - 运行时错误 '1004' : Method 'Range' of object'_Global' failed