如何实现从HTML读取数据与Excel文件并运行Python代码?
解决方案:基于Flask实现Excel文件处理与前端交互
后端Flask代码修复与完善
你的原代码存在几个关键问题:未导入pandas、未调用核心计算函数f、未定义result变量,且缺少基础的错误处理。以下是修复后的完整代码:
from flask import Flask, render_template, request, flash import pandas as pd app = Flask(__name__) app.secret_key = 'your-secret-key-here' # 用于展示提示信息 @app.route('/') def index(): return render_template('index.html') @app.route('/calculate', methods=['POST']) def calculate(): # 检查文件是否上传 if 'file-input-1' not in request.files or 'file-input-2' not in request.files: flash('请上传两个Excel文件') return render_template('index.html') file1 = request.files['file-input-1'] file2 = request.files['file-input-2'] if file1.filename == '' or file2.filename == '': flash('请选择有效的Excel文件') return render_template('index.html') try: # 读取Excel文件 df = pd.read_excel(file1, index_col=0) df1 = pd.read_excel(file2, index_col=0) # 获取前端输入值 your_choice = request.form.get('pipeline-input', '') your_choice_year = request.form.get('data-input', '') # 验证输入有效性 if not your_choice or not your_choice_year.isdigit(): flash('请填写有效的选项和年份') return render_template('index.html') your_choice_year = int(your_choice_year) name_list = df.index.tolist() # 核心计算函数(替换为你的实际业务逻辑) def f(your_choice, df, df1, name_list, your_choice_year): # 示例逻辑,可根据需求修改 result = f"选择流程:{your_choice},年份:{your_choice_year}\n第一个文件数据行数:{len(df)}\n第二个文件数据行数:{len(df1)}" return result # 调用计算函数获取结果 result = f(your_choice, df, df1, name_list, your_choice_year) return render_template('index.html', result=result) except Exception as e: flash(f'处理出错:{str(e)}') return render_template('index.html') if __name__ == '__main__': app.run(debug=True)
前端HTML模板(templates/index.html)
前端需确保表单配置正确的enctype(支持文件上传),控件ID与后端对应,同时添加结果展示和提示区域:
<!DOCTYPE html> <html> <head> <title>Excel数据处理工具</title> <style> .container { max-width: 800px; margin: 20px auto; padding: 0 20px; } .form-group { margin-bottom: 15px; } label { display: block; margin-bottom: 5px; } input, button { padding: 8px; font-size: 14px; } .result { margin-top: 20px; padding: 15px; background-color: #f0f8ff; border-radius: 4px; white-space: pre-wrap; } .flash { padding: 10px; margin-bottom: 15px; border-radius: 4px; color: #721c24; background-color: #f8d7da; } </style> </head> <body> <div class="container"> <h1>Excel数据处理</h1> <!-- 提示信息展示 --> {% with messages = get_flashed_messages() %} {% if messages %} {% for message in messages %} <div class="flash">{{ message }}</div> {% endfor %} {% endif %} {% endwith %} <!-- 表单区域 --> <form method="POST" action="/calculate" enctype="multipart/form-data"> <div class="form-group"> <label for="pipeline-input">选择流程:</label> <input type="text" id="pipeline-input" name="pipeline-input" required> </div> <div class="form-group"> <label for="data-input">选择年份:</label> <input type="number" id="data-input" name="data-input" required> </div> <div class="form-group"> <label for="file-input-1">上传第一个Excel文件:</label> <input type="file" id="file-input-1" name="file-input-1" accept=".xlsx,.xls" required> </div> <div class="form-group"> <label for="file-input-2">上传第二个Excel文件:</label> <input type="file" id="file-input-2" name="file-input-2" accept=".xlsx,.xls" required> </div> <button type="submit" id="runCodeButton">运行代码</button> </form> <!-- 结果展示区域 --> {% if result %} <div class="result"> <h3>计算结果:</h3> <p>{{ result }}</p> </div> {% endif %} </div> </body> </html>
关键注意事项
- 表单enctype:必须设置
enctype="multipart/form-data",否则无法正确接收上传的文件。 - 依赖安装:执行
pip install flask pandas openpyxl安装所需依赖(openpyxl用于读取.xlsx格式文件)。 - 逻辑替换:将示例中的
f函数逻辑替换为你实际的业务计算代码即可。
内容的提问来源于stack exchange,提问作者Анастасия Шелякина
相关产品推荐
相关产品推荐

