如何在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 torequest.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 withrange(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 totext_list[0]if that's what you intended.
How to Test in Postman:
- Set the request method to
POSTand the URL tohttp://localhost:17002/upload - Go to the Body tab, select form-data
- Add a key named
file(match the name in your code), set the type to File, and select your Excel file - Send the request—you should get back all the text entries from your 'Text' column!
内容的提问来源于stack exchange,提问作者SupLike RealLife
相关产品推荐
相关产品推荐

