本帖最后由 GL_n 于 2018-12-16 02:24 编辑
Sometimes we need to split or crop double-arranged PDF’s pages along the spine direction (i.e., the middle line) into two new pages, for example, two A4 book pages scanned into a A3 paper, or two A5 book pages arranged in a A4 paper, and so on. Well, there exist some cutting tools to fill the need of cutting double-up pages. However, this cutting/cropping procedure is not automatic, and the repeated cropping-operation is somewhat tedious.
How to get the repeated cropping-manipulations done automatically? Writing a python program is most likely a good choice for such assignment. The following coding is one of such right examples for working well towards cropping double-arranged PDF’s pages, no matter what the page format is.
- #coding=utf-8
- from PyPDF2 import PdfFileWriter, PdfFileReader
- from copy import copy
- from os import listdir
- import math
- def op(pdfInputFileName):
-
- pdfFileObj = open(pdfInputFileName, 'rb')
- pdfReader = PdfFileReader(pdfFileObj)
- pdfWriter = PdfFileWriter()
- for page in [pdfReader.getPage(i) for i in range(pdfReader.getNumPages())]:
- p = page
- q = copy(p)
- q.mediaBox = copy(p.mediaBox)
- x_1, x_2 = p.mediaBox.lowerLeft
- x_3, x_4 = p.mediaBox.upperRight
- x_1, x_2 = math.floor(x_1), math.floor(x_2)
- x_3, x_4 = math.floor(x_3), math.floor(x_4)
- x_5, x_6 = math.floor(x_3/2), math.floor(x_4/2)
- if x_3 < x_4: # If your scanned page is normally presented in Adobe Acrobat this "if" statement can be deleted.
- p = p.rotateClockwise(90)
- q = q.rotateClockwise(90)
-
- if x_3 > x_4: # For editable page
- # vertical cropping along Y-axis(x_5 direction, i.e., cutting X-axis)
- p.mediaBox.lowerLeft = (x_1, x_2) # Left part of two-page-rectangle
- p.mediaBox.upperRight = (x_5* 105/100, x_4)
- q.mediaBox.lowerLeft = (x_5* 95/100, x_2)
- q.mediaBox.upperRight = (x_3, x_4) # Right part of two-page-rectangle
-
- else: # For image page
- # vertical cropping along X-axis(x_6 direction, i.e., cutting Y-axis)
-
- p.mediaBox.lowerLeft = (x_1, x_2)
- p.mediaBox.upperRight = (x_3, x_6* 105/100) # Left part of two-page-rectangle
- q.mediaBox.lowerLeft = (x_1, x_6* 95/100)
- q.mediaBox.upperRight = (x_3, x_4) # Right part of two-page-rectangle
- pdfWriter.addPage(p)
- pdfWriter.addPage(q)
- pdfOutputFileName = pdfInputFileName[:-4]+'-cut_myself_revised.pdf'
- pdfOutputFile = open(pdfOutputFileName, 'wb')
- pdfWriter.write(pdfOutputFile)
- pdfFileObj.close()
- pdfOutputFile.close()
-
- # Accomplish the whole execution of a series of PDF-cropping (both editable pages and image pages) automatically in current directory.
- for pdfInputFileName in listdir('.'):
- if pdfInputFileName[-4:]=='.pdf' or pdfInputFileName[-4:]=='.PDF':
- op(pdfInputFileName)
复制代码
|