简体   繁体   中英

AttributeError: 'bytes' object has no attribute 'getPage'

Ive done some coding. Html and flask. Accepting pdf file from a form and watermark it using flask.

Here is my html coding:

<!DOCTYPE html>
<html>
<body>

<p>Click on the "Choose File" button to upload a file:</p>

<form action="/upload_file" method="POST" enctype = "multipart/form-data">
  <input type="file" name="file">
  <input type="submit">
</form>

</body>
</html>

Here is my flask coding:

@app.route('/upload_file', methods=['POST'])
def upload_file():

pdf_file = request.files['file']
watermark = "watermark.pdf"
merged_file = "merged.pdf"

input_pdf= pdf_file.read()
#input_pdf = PyPDF2.PdfFileReader(open(pdf_file,'rb'))

watermark_file = open(watermark,'rb')
watermark_pdf = PyPDF2.PdfFileReader(watermark_file)

pdf_page = input_pdf.getPage(0)

watermark_page = watermark_pdf.getPage(0)

pdf_page.mergePage(watermark_page)

output = PyPDF2.PdfFileWriter()

output.addPage(pdf_page)

merged_file = open(merged_file,'wb')
output.write(merged_file)

merged_file.close()
watermark_file.close()
input_pdf.close()

return "Success"

Traceback:

Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py", line 2088, in 
__call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py", line 2073, in 
wsgi_app
    response = self.handle_exception(e)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py", line 2070, in 
wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py", line 1515, in 
full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py", line 1513, in 
full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py", line 1499, in 
dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "C:\Users\User\Desktop\abc\test.py", line 57, in upload_file
    pdf_page = input_pdf.getPage(0)
AttributeError: 'bytes' object has no attribute 'getPage'

How to fix the AttributeError: 'bytes' object has no attribute 'getPage' error and also AttributeError: 'bytes' object has no attribute 'mergePage' error?

I need help. thank you.

The relevant code is:

input_pdf= pdf_file.read()
pdf_page = input_pdf.getPage(0)

From a commented-out line ( input_pdf = PyPDF2.PdfFileReader(open(pdf_file,'rb')) ), it looks like you probably want to create a PdfFileReader object based on pdf_file. Since pdf_file is a file, try:

input_pdf = PyPDF2.PdfFileReader(pdf_file) # changed from pdf_file.read()
pdf_page = input_pdf.getPage(0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM