You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Postman中读取上传的Excel文件?Flask接口遇ImmutableMultiDict报错

Fixing the "ImmutableMultiDict object is not callable" Error in Your Flask-Pandas Excel API

Hey there, let's get that error sorted out quickly! The root cause of your TypeError: 'ImmutableMultiDict' object is not callable is super straightforward: request.files is not a function you can call with parentheses—it's an ImmutableMultiDict (a special Flask dictionary) that holds the uploaded files.

Here's the corrected code with explanations of the key fixes:

import pandas as pd
from flask import request, jsonify, Flask

app = Flask(__name__)

@app.route('/upload', methods=['POST'])
def excel():
    # Fix 1: Get the uploaded file from request.files (no parentheses!)
    # Replace 'file' with the exact field name you use in Postman's form-data
    uploaded_file = request.files.get('file')
    
    # Fix 2: Add a check to handle cases where no file was uploaded
    if not uploaded_file:
        return jsonify({"error": "No file uploaded"}), 400
    
    # Fix 3: Pass the file object directly to pd.read_excel
    df = pd.read_excel(uploaded_file)
    
    # Fix 4: Clean up how you extract the 'Text' column (no need for range(len()))
    # If you want all text entries, just convert the series to a list directly
    text_list = df['Text'].tolist()
    
    # Fix 5: Return the full list instead of just the first element (adjust if you only need the first)
    return jsonify({"Data": text_list})

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=False, port=17002)

Key Changes Breakdown:

  • Removed parentheses from request.files(): Changed to request.files.get('file') to properly fetch the uploaded file. Make sure the string 'file' matches the key you use in Postman's form-data tab when uploading your Excel file.
  • Added error handling: Checks if a file was actually uploaded, so you don't get a weird error later if the user forgets to attach a file.
  • Simplified text extraction: Used df['Text'].tolist() instead of looping with range(len())—it's more Pythonic and efficient.
  • Returned the full list: Your original code only returned the first text entry (Text[0]). I changed it to return all entries, but you can switch back to text_list[0] if that's what you intended.

How to Test in Postman:

  1. Set the request method to POST and the URL to http://localhost:17002/upload
  2. Go to the Body tab, select form-data
  3. Add a key named file (match the name in your code), set the type to File, and select your Excel file
  4. Send the request—you should get back all the text entries from your 'Text' column!

内容的提问来源于stack exchange,提问作者SupLike RealLife

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:45:23