2

What I want to do

-I want to reverse the order of about 10 PDFs.

What I have done

-I have found a very nice way of doing this here. (Great thanks to this post): How do I reverse the order of the pages in a pdf file using pyPdf?
-But this code was written for only one file.
-So I edited the code to something like below.

My code

from PyPDF2 import PdfFileWriter, PdfFileReader
import tkinter as tk
from tkinter import filedialog
import ntpath
import os
import glob


output_pdf = PdfFileWriter()

# grab the location of the file path sent
def path_leaf(path):
    head, tail = ntpath.split(path)
    return head

# graphical file selection
def grab_file_path():
    # use dialog to select file
    file_dialog_window = tk.Tk()
    file_dialog_window.withdraw()  # hides the tk.TK() window
    # use dialog to select file
    grabbed_file_path = filedialog.askopenfilenames()
    return grabbed_file_path


# file to be reversed
filePaths = grab_file_path()

# open file and read
for filePath in filePaths:
    with open(filePath, 'rb') as readfile:
        input_pdf = PdfFileReader(readfile)

        # reverse order one page at time
        for page in reversed(input_pdf.pages):
            output_pdf.addPage(page)

        # graphical way to get where to select file starting at input file location
        dirOfFileToBeSaved = os.path.join(path_leaf(filePath), 'reverse' + os.path.basename(filePath)) 
        # write the file created
        with open(dirOfFileToBeSaved, "wb") as writefile:
            output_pdf.write(writefile)

Problem

-It did reverse the order.
-But it did not only reverse the order but also merged all the files.
-e.g.

A.pdf: page c, b, a    
B.pdf: page f, e, d  
C.pdf: page i, h, g  

result would be like this

reverseA.pdf: page a, b, c  
reverseB.pdf: page a, b, c, d, e, f  
reverseC.pdf: page a, b, c, d, e, f, g, h, i  

My question

-How should I edit this code so that the files will not be merged?
-I am very new to python, sorry.

Community
  • 1
  • 1
XTJP
  • 33
  • 6

2 Answers2

1

You keep adding pages to the same output_pdf, even though the pages are from different PDFs originally. You can solve this by adding

output_pdf = PdfFileWriter()

before starting with a new file. You would get:

...
# open file and read
for filePath in filePaths:
    output_pdf = PdfFileWriter()
    with open(filePath, 'rb') as readfile:
        input_pdf = PdfFileReader(readfile)
...
Daniel
  • 81
  • 3
  • If this solved your problem, please accept the answer. – Daniel Oct 29 '19 at 20:34
  • Hi Daniel, this did not work for me. I got the result pdf files, but all the pages were blank and white. Thank you for your suggestion :) And page numbers were the same as original file. – XTJP Oct 30 '19 at 00:11
  • Are you sure you inserted the line at the correct spot? What do you mean that the page numbers (of the blank pages) were the same as the original file? – Daniel Oct 30 '19 at 06:17
  • Thank you for your response. I meant that the result file contained the same number of pages as the original file (e.g. A.pdf), but it was blank. – XTJP Nov 01 '19 at 06:33
0

This seems an indentation issue. Your save instruction was indented to inner loop and not the outermost for loop. Try following which has the correct indentation.

# file to be reversed
filePaths = grab_file_path()

# open file and read
for filePath in filePaths:
    with open(filePath, 'rb') as readfile:
        input_pdf = PdfFileReader(readfile)

        # reverse order one page at time
        for page in reversed(input_pdf.pages):
            output_pdf.addPage(page)

    # graphical way to get where to select file starting at input file location
    dirOfFileToBeSaved = os.path.join(path_leaf(filePath), 'reverse' + os.path.basename(filePath)) 
    # write the file created
    with open(dirOfFileToBeSaved, "wb") as writefile:
        output_pdf.write(writefile)
S.N
  • 4,910
  • 5
  • 31
  • 51
  • This did not work for me. I got the result pdf files, but all the pages were blank and white. Thank you for your suggestion :) – XTJP Oct 30 '19 at 00:08
  • And page numbers were the same as original file. – XTJP Oct 30 '19 at 00:11
  • @XTJP, This solution address the issue that you mentioned in initial thread. If you have more questions then do mention it on the post so that someone can help. – S.N Oct 30 '19 at 08:10