46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
|
|
#!/usr/bin/env python
|
||
|
|
# -*- encoding:utf-8 -*-
|
||
|
|
|
||
|
|
import asyncio
|
||
|
|
from datetime import datetime
|
||
|
|
from http.client import HTTPException
|
||
|
|
|
||
|
|
from openpyxl.reader.excel import load_workbook
|
||
|
|
from starlette.responses import JSONResponse
|
||
|
|
from fastapi import FastAPI
|
||
|
|
import config
|
||
|
|
|
||
|
|
app = FastAPI()
|
||
|
|
|
||
|
|
def json_serializable(obj):
|
||
|
|
if isinstance(obj, datetime):
|
||
|
|
return obj.isoformat() # 转换为 ISO 格式的字符串
|
||
|
|
raise TypeError(f"Type {type(obj)} not serializable")
|
||
|
|
|
||
|
|
@app.get("/read-excel")
|
||
|
|
async def read_excel_rows():
|
||
|
|
try:
|
||
|
|
rows="1,2,3,4,5"
|
||
|
|
# 加载Excel工作簿
|
||
|
|
file_path =config.osp.join(config.BASE_DIR, 'output/','4834ed97e0ba477b9d239560e4b12be6.xlsx')
|
||
|
|
workbook = load_workbook(filename=file_path)
|
||
|
|
sheet = workbook.active # 或者使用workbook.get_sheet_by_name('Sheet1')
|
||
|
|
|
||
|
|
# 获取要读取的行号列表
|
||
|
|
# row_numbers = [int(r) for r in rows.split(',') if r.isdigit()]
|
||
|
|
|
||
|
|
# 读取指定行的数据
|
||
|
|
data = []
|
||
|
|
|
||
|
|
for row in sheet:
|
||
|
|
row_data = [json_serializable(cell.value) if isinstance(cell.value, datetime) else cell.value for cell in row]
|
||
|
|
data.append(row_data)
|
||
|
|
return JSONResponse(content={"data": data})
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
raise HTTPException(status_code=500, detail=str(e))
|
||
|
|
|
||
|
|
if __name__ == '__main__':
|
||
|
|
|
||
|
|
import uvicorn
|
||
|
|
uvicorn.run(app, host='0.0.0.0', port=9001)
|