赞
踩
from flask import Flask, jsonify import requests import pandas as pd import os import tempfile app = Flask(__name__) @app.route('/read_excel', methods=['GET']) def read_excel(): # Excel 文件的 URL excel_url = 'https://xxx.xxx.xlsx' # 发送请求并下载文件 response = requests.get(excel_url) # 确保请求成功 if response.status_code != 200: return jsonify({'error': 'Failed to retrieve Excel file'}), 400 # 创建一个临时文件来保存 Excel 文件 with tempfile.NamedTemporaryFile(delete=False) as tmp: tmp.write(response.content) tmp_file_path = tmp.name try: # 读取 Excel 文件 df = pd.read_excel(tmp_file_path) # 这里可以处理数据,比如转换成 JSON 格式 data = df.to_json(orient='records') # 返回数据 return jsonify(data) except Exception as e: # 如果发生错误,返回错误信息 return jsonify({'error': str(e)}), 500 finally: # 删除临时文件 os.remove(tmp_file_path) if __name__ == '__main__': app.run(debug=True)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。