Split each PDF page in two?

后端 未结 10 2104
天命终不由人
天命终不由人 2021-01-30 11:39

I have a large number of PDF files which have two slides to a page (for printing).

The format is A4 pages each with two slides setup like so:

-----------         


        
10条回答
  •  执笔经年
    2021-01-30 11:48

    You can use a Python library called PyPDF. This function will split double pages no matter what the page orientation is:

    import copy
    import math
    import pyPdf
    
    def split_pages(src, dst):
        src_f = file(src, 'r+b')
        dst_f = file(dst, 'w+b')
    
        input = pyPdf.PdfFileReader(src_f)
        output = pyPdf.PdfFileWriter()
    
        for i in range(input.getNumPages()):
            p = input.getPage(i)
            q = copy.copy(p)
            q.mediaBox = copy.copy(p.mediaBox)
    
            x1, x2 = p.mediaBox.lowerLeft
            x3, x4 = p.mediaBox.upperRight
    
            x1, x2 = math.floor(x1), math.floor(x2)
            x3, x4 = math.floor(x3), math.floor(x4)
            x5, x6 = math.floor(x3/2), math.floor(x4/2)
    
            if x3 > x4:
                # horizontal
                p.mediaBox.upperRight = (x5, x4)
                p.mediaBox.lowerLeft = (x1, x2)
    
                q.mediaBox.upperRight = (x3, x4)
                q.mediaBox.lowerLeft = (x5, x2)
            else:
                # vertical
                p.mediaBox.upperRight = (x3, x4)
                p.mediaBox.lowerLeft = (x1, x6)
    
                q.mediaBox.upperRight = (x3, x6)
                q.mediaBox.lowerLeft = (x1, x2)
    
            output.addPage(p)
            output.addPage(q)
    
        output.write(dst_f)
        src_f.close()
        dst_f.close()
    

提交回复
热议问题