0

I have a pdf that looks like this and i'd like to crop all the text out, almost right down the middle of the page. I found this script that does something simmilar:

def splitHorizontal():
from pyPdf import PdfFileWriter, PdfFileReader
input1 = PdfFileReader(file("in.pdf", "rb"))
output = PdfFileWriter()

numPages = input1.getNumPages()
print "document has %s pages." % numPages

for i in range(numPages):
    page = input1.getPage(i)
    print page.mediaBox.getUpperRight_x(), page.mediaBox.getUpperRight_y()
    page.trimBox.lowerLeft = (25, 25)
    page.trimBox.upperRight = (225, 225)
    page.cropBox.lowerLeft = (50, 50)
    page.cropBox.upperRight = (200, 200)
    output.addPage(page)

outputStream = file("out.pdf", "wb")
output.write(outputStream)
outputStream.close()

However these crop dimensions are tuned to that specific example. Can anyone show me how to find the correct crop dimensions.

Jeff
  • 21
  • 1
  • 3

2 Answers2

2

I originally got the script from here --> Cropping pages of a .pdf file.

I read more into what the author had said, finally realizing that he had said:

The resulting document has a trim box that is 200x200 points and starts at 25,25 points inside the media box. The crop box is 25 points inside the trim box.

meaning

page.cropBox.upperRight = (200, 200)

must control the ultimate margins, i therefore adjusted the statement to

page.cropBox.upperLeft = (290, 792)

To mirror the cropping onto the other side and make sure the cropping holds the full vertical value

Community
  • 1
  • 1
Jeff
  • 21
  • 1
  • 3
0

Chops each page in half, e.g. if a source were created in booklet form, and then re-combines it for further processing eg. text extraction

Importing required libraries

from PyPDF2 import PdfFileWriter,PdfFileReader,PdfFileMerger

Splitting Left Part

with open("docu.pdf", "rb") as in_f:
    input1 = PdfFileReader(in_f)
    output = PdfFileWriter()

    numPages = input1.getNumPages()

    for i in range(numPages):
        page = input1.getPage(i)
        page.cropBox.lowerLeft = (60, 50)
        page.cropBox.upperRight = (305, 700)
        output.addPage(page)

    with open("left.pdf", "wb") as out_f:
        output.write(out_f)

Splitting right part :

with open("docu.pdf", "rb") as in_f:
    input1 = PdfFileReader(in_f)
    output = PdfFileWriter()

    numPages = input1.getNumPages()

    for i in range(numPages):
        page = input1.getPage(i)
        page.cropBox.lowerLeft = (300, 50)
        page.cropBox.upperRight = (540, 700)
        output.addPage(page)

    with open("right.pdf", "wb") as out_f:
        output.write(out_f)

Combining left with right (two columns to two pages)

input1 = PdfFileReader(open("left.pdf","rb"))
input2 = PdfFileReader(open("right.pdf","rb"))
output = PdfFileWriter()
numPages = input1.getNumPages()

for i in range(numPages):
    l = input1.getPage(i)
    output.addPage(l)
    r = input2.getPage(i)
    output.addPage(r)

with open("out.pdf", "wb") as out_f:
    output.write(out_f)