I am trying to get print one page of a PDF to a new PDF document. I am using the following code:
from PyPDF2 import PdfFileReader, PdfFileWriter
file_path = "/file_path/.pdf"
input_pdf = PdfFileReader(file_path)
output_file = PdfFileWriter()
cover_page = input_pdf.getPage(0)
output_file.addPage(cover_page)
with open("portion.pdf", "wb") as output_file:
output_file.write(output_file)
When I run this code I get the following error:
Traceback (most recent call last):
File ".../Extract a portion of PDF.py", line 18, in <module>
output_file.write(output_file)
TypeError: a bytes-like object is required, not '_io.BufferedWriter'
I have specified that the output needs to write binary, so why is it saying that I must use byte-like objects?
Cheers,