XTJP
XTJP

Reputation: 33

How do I reverse multiple the order of multiple PDF files with pyPDF2?

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.

Upvotes: 2

Views: 419

Answers (2)

Daniel
Daniel

Reputation: 81

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)
...

Upvotes: 1

S.N
S.N

Reputation: 5140

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)

Upvotes: 0

Related Questions