10

I am trying to write a couple of python scripts using pyPDF to split PDF pages into six separate pages, order them correctly (usually printed front and back, so every other page needs to have its subpages ordered differently), and remove resulting blank pages at the end of the output document.

I wrote the following script to cut the PDF pages up and reorder them. Cuts each page into two columns and each column into three pages. I am not very experienced with python, so please excuse anything I'm not doing correctly.

#!/usr/bin/env python
import copy, sys
from pyPdf import PdfFileWriter, PdfFileReader
input = PdfFileReader(sys.stdin)
output = PdfFileWriter()

for i in range(0,input.getNumPages(),2):
    p = input.getPage(i)
    q = copy.copy(p)
    r = copy.copy(p)
    s = copy.copy(p)
    t = copy.copy(p)
    u = copy.copy(p)
    (x, y) = p.mediaBox.lowerLeft
    (w, h) = p.mediaBox.upperRight

    p.mediaBox.lowerLeft = (x, 2 * h / 3)
    p.mediaBox.upperRight = (w / 2, h)

    q.mediaBox.lowerLeft = (w / 2, 2 * h / 3)
    q.mediaBox.upperRight = (w, h)

    r.mediaBox.lowerLeft = (x, h / 3)
    r.mediaBox.upperRight = (w / 2, 2 * h / 3)

    s.mediaBox.lowerLeft = (w / 2, h / 3)
    s.mediaBox.upperRight = (w, 2 * h / 3)

    t.mediaBox.lowerLeft = (x, y)
    t.mediaBox.upperRight = (w / 2, h / 3)

    u.mediaBox.lowerLeft = (w / 2, y)
    u.mediaBox.upperRight = (w, h / 3)

    a = input.getPage(i+1)
    b = copy.copy(a)
    c = copy.copy(a)
    d = copy.copy(a)
    e = copy.copy(a)
    f = copy.copy(a)
    (x, y) = a.mediaBox.lowerLeft
    (w, h) = a.mediaBox.upperRight

    a.mediaBox.lowerLeft = (x, 2 * h / 3)
    a.mediaBox.upperRight = (w / 2, h)

    b.mediaBox.lowerLeft = (w / 2, 2 * h / 3)
    b.mediaBox.upperRight = (w, h)

    c.mediaBox.lowerLeft = (x, h / 3)
    c.mediaBox.upperRight = (w / 2, 2 * h / 3)

    d.mediaBox.lowerLeft = (w / 2, h / 3)
    d.mediaBox.upperRight = (w, 2 * h / 3)

    e.mediaBox.lowerLeft = (x, y)
    e.mediaBox.upperRight = (w / 2, h / 3)

    f.mediaBox.lowerLeft = (w / 2, y)
    f.mediaBox.upperRight = (w, h / 3)

    output.addPage(p)
    output.addPage(b)
    output.addPage(q)
    output.addPage(a)
    output.addPage(r)
    output.addPage(d)
    output.addPage(s)
    output.addPage(c)
    output.addPage(t)
    output.addPage(f)
    output.addPage(u)
    output.addPage(e)

output.write(sys.stdout)

Then I use the following script to remove the blank pages.

#!/usr/bin/env python
import copy, sys
from pyPdf import PdfFileWriter, PdfFileReader
input = PdfFileReader(sys.stdin)
output = PdfFileWriter()

for i in range(0,input.getNumPages()):
    p = input.getPage(i)

    text = p.extractText()

    if (len(text) > 10):
        output.addPage(p)

output.write(sys.stdout)

The problem seems to be that while the pages are visibly cropped down, the text draw commands are still there. None of these pages are scanned, so if they are blank, they are really blank. Does anyone have any thoughts on something I could do differently or possibly an entirely different approach to take to remove the blank pages? I would really appreciate any help.

2
  • 1
    Update: If I open the result of the first script in Acrobat and then use 'Save as...', it rebuilds the file from the ground up. Then when I run the second script it works the way I want. I need a way to script the process that acrobat does in 'Save as...' to be used in a script that doesn't require Acrobat itself.
    – rpeck1682
    Jun 10, 2011 at 19:46
  • This is not really an answer, so I'll post it as a comment. The whole thing needed to be put in an iOS app once it was done. I resorted to just drawing the appropriate areas of the original PDF with a setting to identify the number of blank pages at the end. It isn't ideal, but it works.
    – rpeck1682
    Jun 16, 2011 at 22:47

1 Answer 1

7

PdfFileReader has a method, getPage(self, page number) that returns an object, PageObject, that in turn has a method getContents, which will return None if the page is blank. So, with your pdf object, getNumPages(), iterate with if getPage(i).getContents():, collecting the hits into a list of page numbers to output.

1
  • Thanks! I had apparently been using a copy of pyPdf that was a couple of years old and did not have the getContents() method. I have not tested this solution since I took the route described in a comment on the original post (not a solution, a completely different route). If I ever need to return to this, I will use getContents() as a starting point.
    – rpeck1682
    Jun 20, 2011 at 20:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.