Skip to main content

Command Palette

Search for a command to run...

Fastapi receive a xlsx file and read it

Published
1 min read

How to accept a xlsx file and read it using Fastapi, the following is the implementation code.

@application.post("/uploadFile/")
async def create_upload_file(file: UploadFile):
    if file.filename.endswith('.xlsx'):
        # Read it, 'f' type is bytes
        f = await file.read()
        xlsx = io.BytesIO(f)
        wb = openpyxl.load_workbook(xlsx)
        ws = wb['Sheet1']

        for cells in ws.iter_rows():
            print([cell.value for cell in cells])
        return True

    else:
        raise HTTPException(status_code=status.HTTP_405_METHOD_NOT_ALLOWED)

More from this blog

Python实现长度16位十进制整数的客制化雪花算法(Snowflake)生成器

前言 Javascript 整数表达最大为 2^53-1 位,大约是十进制16位,超过就会导致精度丢失,一般在浏览器中会直接报错。 场景 我们知道,雪花算法(SnowFlake)生成的雪花ID是 2^64 位,如果直接使用雪花算法(SnowFlake)生成器,前端是没办法直接拿到int类型的雪花ID,需要后端先将其转换为string类型才行。 使用雪花算法(SnowFlake)的很重要一个原因就是,int类型对数据库索引是很友好的,为了前后端数据交互方便,在数据库中直接储存string类型的雪花...

Dec 28, 20222 min read478
A

Asiones Jia's blog

5 posts