Simon Nasser
Simon Nasser

Reputation: 112

PyPDF2 writer function creates blank page

Trying to write a function to combine pages in a PDF document. Streaming the output creates a blank page for an unknown reason Here is the test case

from PyPDF2 import PdfReader, PdfWriter

dr = r"C:\GC"
ldr = dr + r"\12L.pdf"
writer = PdfWriter()

with open(ldr, "rb") as f:
    reader = PdfReader(f)
    page = reader.pages[0]
    writer.add_page(page)
    f.close()

with open(dr + r"\new.pdf", "wb") as output_stream:
    writer.write(output_stream)
    output_stream.close()

Edit: I made some changes to my PE and got more information.

writer.write(output_stream)

raises the error

ValueError: I/O operation on closed file: C:\GC\12L.pdf

I did some troubleshooting with keeping the reader file open and changing syntax to suggestions and I still raise the error.

Upvotes: 2

Views: 3835

Answers (3)

Abhinav Mathur
Abhinav Mathur

Reputation: 8101

Since you're using a context manager, you don't need to explicitly call .close(). Try this:

from PyPDF2 import PdfReader, PdfWriter

dr = r"C:\GC"
ldr = dr + r"\12L.pdf"
writer = PdfWriter()

with open(ldr, "rb") as f:
  reader = PdfReader(f)
  page = reader.pages[0]
  writer.add_page(page)
  
  with open(dr + r"\new.pdf", "wb") as output_stream:
    writer.write(output_stream)

Upvotes: 1

Simon Nasser
Simon Nasser

Reputation: 112

This is what works. Not using the open() as f syntax wouldn't initalize the fields of the writer object. Closing the file before the output being written would produce an error. I guess the file must be open so the object can point to the instance.

from PyPDF2 import PdfFileReader, PdfFileWriter

dr = r"C:\GC"
ldr = dr + r"\pgSrc\12L.pdf"
rdr = dr + r"\pgSrc\12R.pdf"
r5 = dr + r"\pgSrc\pg5R.pdf"
writer = PdfFileWriter()
with open(ldr, "rb") as f:
  reader = PdfFileReader(f)
  page = reader.getPage(5)
  writer.addPage(page)
  with open(dr + r"\new.pdf", "wb") as outputStream:
    writer.write(outputStream)

Upvotes: 0

Life is complex
Life is complex

Reputation: 15619

The code below opens a PDF file that has 12 pages, gets the first page and writes that page to a new PDF file.

from PyPDF2 import PdfFileWriter, PdfFileReader

input_pdf = open('test.pdf', 'rb')
writer = PdfFileWriter()

reader = PdfFileReader(input_pdf)
in1 = writer.addPage(reader.getPage(0))
input_pdf.close()

output_pdf = open('new_test.pdf', 'wb')
writer.write(output_pdf)
output_pdf.close()
----------------------------------------
My system information
----------------------------------------
Platform:     Apple
OS Version:   macOS Catalina 10.15.7
Python Version: 3.9
PyPDF2 Version: 1.26.0
----------------------------------------

Upvotes: 1

Related Questions