FastAPI上传文件并将其传递到python-docx的Document中

问题描述 投票:0回答:1

我想使用FastAPI的方法(

https://fastapi.tiangolo.com/tutorial/request-forms-and-files/
)上传 .docx文档,然后将该文件(使用字节)传递到
python-docx 
的文档。我尝试使用问题中的@Mazze解决方案(尝试使用FastAPI和python-docx库读取docx文件:AttributeError:'bytes'对象没有属性'seek'错误)但我得到了

TypeError: stat: path should be string, bytes, os.PathLike or integer, not BytesIO

Python:3.11.5 快速API:0.103.2 Python-docx:0.8.11

from docx import Document
from io import BytesIO

from fastapi import UploadFile
from fastapi.responses import JSONResponse

app = FastAPI()


@app.post("/upload", responses={HTTPStatus.BAD_REQUEST: {"model": Error}})
async def upload_document(file: UploadFile) -> dict[str, Any]:
    file_name: str = file.filename
    doc = Document(BytesIO(await file.read()))
    return JSONResponse(
        status_code=HTTPStatus.CREATED,
        content={"file_name": file.filename},
    )

我也尝试过:

doc = Document(BytesIO(await file.read()).read())
# AttributeError: 'bytes' object has no attribute 'seek'

doc = Document(base64.decodebytes(BytesIO(await file.read())))
TypeError: expected bytes-like object, not BytesIO
python fastapi python-docx python-3.11
1个回答
0
投票

docx.Document()
的签名是
(str | IO[bytes] | None) -> docx.document.Document

请注意,

bytes
不是选项之一。然而
io.BytesIO
确实满足
IO[bytes]
类型约束,所以我认为可以安全地说你想要的是:

bytes = ...  # -- however you translate the FastAPI payload into bytes --
document = Document(io.BytesIO(bytes))

您可能需要在

file.seek(0)
之前执行
file.read()
步骤,因为如果刚刚完成写入,它将指向文件的末尾而不是开头。

© www.soinside.com 2019 - 2024. All rights reserved.