0

Hi I'm trying the following for api input

def upload_excel_parser(file: UploadFile = File(...)):
 s_filename = file.filename
 unique_id = str(uuid4())
 project_id = s_filename + unique_id    
 df = pd.read_excel(file)   
 return "success"

also tried

df = pd.read_excel(file.file)

getting error ValueError: Excel file format cannot be determined, you must specify an engine manually.

Is there some error in reading the file?

devb
  • 317
  • 3
  • 12
  • Shouldn't you `read()` the `file` first, to get the content of the file? – DSteman Mar 01 '23 at 05:47
  • 1
    Does this answer your question? [How to upload a file in FastAPI and convert it into a Pandas Dataframe?](https://stackoverflow.com/questions/71477787/how-to-upload-a-file-in-fastapi-and-convert-it-into-a-pandas-dataframe) – DSteman Mar 01 '23 at 05:48

1 Answers1

0

pandas cannot directly read from a UploadFile - you need to do the following.

async def upload_excel_parser(file: UploadFile = File(...)):
    content = await file.read()
    df = pd.read_excel(BytesIO(content))
    return "success"
EyuelDK
  • 3,029
  • 2
  • 19
  • 27