-1

I am making a pdf splitter and at first seemed to work fine. But when i tryed to use multiple page regions , i keep getting this error--> ValueError: seek of closed file. If i omit pdf_file.close() the error will stop but all the pdf created will have no pages. My code is here:

from PyPDF2 import PdfFileReader , PdfFileWriter

counter = 1
pdf_file = open(fileName2,'rb')
pdf_reader = PdfFileReader(pdf_file)
pdf_writer = PdfFileWriter()
output_file2 , _ = QtWidgets.QFileDialog.getSaveFileName(self, "Save file", fileName2_c2+"_splited", "Folder will be created")
os.makedirs(r'{}'.format(output_file2+"\\{}_splited".format(fileName2_c2)))

for z in list_pdf_split:
    try:
        pdf_file = open(fileName2,'rb')
    except:
        print("error")
    print(z)
    c_z = z.split("-")

    for i in range(int(c_z[0]),int(c_z[1])+1):
        print(i)


        pdf_writer.addPage(pdf_reader.getPage(i-1))


    output_file = open(output_file2+"\\{}_splited".format(fileName2_c2)+"{}".format(counter)+".pdf",'wb')


    pdf_reader = PdfFileReader(pdf_file)
    pdf_writer = PdfFileWriter()
    pdf_writer.write(output_file)
    output_file.close()
    counter +=1
    pdf_file.close() 

2 Answers2

1

Your logic doesn't make much sense, in multiple places.


First, the problem you're asking about. Look at what you do with pdf_file and pdf_reader:

  1. Open a file as pdf_file.
  2. Create a PdfFileReader attached to pdf_file as pdf_reader.
  3. Reopen the same file as pdf_file. This releases the old file, causing it to become garbage, so it will soon (usually immediately) get closed.
  4. Repeatedly call getPage(:-1) on pdf_reader, which is probably attached to a closed file the first time, and definitely every time after that.
  5. Create a new PdfFileReader with the file we opened in step 3, as pdf_reader.
  6. Close the pdf_file you just opened, so pdf_reader is definitely now referencing a closed file.
  7. Repeat steps 2-6.

You either need to do step 4 before step 3, or after step 5, or you need to have two different pdf_file variables so you can open the new one while still using the old one. I'm not sure which of the three you want, but as-is, you're reading from a closed file.

But I think it would be simpler to reorganize things to eliminate step 1—instead of trying to opening stuff before the loop and then reopen stuff at the end of each loop, you just open stuff at the start of the loop, right where you need it.


Second, your writer is just as confused. Look at what you do with output_file and pdf_writer:

  1. Create PdfFileWriter as pdf_writer.
  2. Repeatedly add pages to it.
  3. Open an output file as output_file.
  4. Create a new PdfFileWriter as pdf_writer, discarding everything you wrote to the old one.
  5. Write out the now-empty pdf_writer to output_file.
  6. Repeat steps 2-5.

Again, you need to do step 5 somewhere else, probably before step 4. But, again, it's probably a lot simpler to reorganize things to eliminate step 1.

abarnert
  • 354,177
  • 51
  • 601
  • 671
0

Sorry i suppose i was too fast answering this question. I moved pdf.writer and pdf.reader to the begining of for loops as it seems to block code (stream for writing pdf).