pythonpdffastapibytesio

Why I keep getting the `TypeError: expected str, bytes or os.PathLike object, not BufferedWriter` error?


There is something very basic that I'm not able to do. In this code :

@app.get('/resume')
async def resume():
    path = os.path.abspath(os.getcwd())
    with open(f'{path}/cv.pdf', 'rb') as f:
        data_binary = f.read()
        file_data = base64.b64encode(data_binary)

    with open(f'{path}/sample.pdf','wb') as pdf:
        pdf.write(file_data)
        parsed_data = ResumeParser(pdf).get_extracted_data()
    pdf.close()
    return await process_pdf(parsed_data)

I'm getting the error TypeError: expected str, bytes or os.PathLike object, not BufferedWriter over and over again for the line ResumeParser(pdf).get_extracted_data() when :

parsed_data = ResumeParser(f'{path}/cv.pdf').get_extracted_data()

is working perfectly.

Do you know by any chance what I'm missing here please ?

TYVM.

For the context :

  1. Obviously, I tried to convert the base64 file to bytes with BytesIO
  2. ResumeParser is coming from this package pyresparser
  3. I don't know if this is related but I never see this sample.pdf file created in my file system.
  4. I'm trying to convert a pdf file to base64 in order to make it work also with a frontend app.

Solution

  • ResumeParser appears to want the path to the file to parse. You are supplying the contents of the file in the pdf variable. you don't need the with clause there. also your with clause would automatically close the pdf file when the with is exited. You do not need the explicit close() call. I have updated that section and included it here.

     with open(f'{path}/sample.pdf','wb') as pdf:
            pdf.write(file_data)
    
     parsed_data = ResumeParser(f'{path}/sample.pdf').get_extracted_data()
    

    The above code writes and closes the file and then passes the name to resume parser. I think that is what you are trying to do.