Split each PDF page in two?

后端 未结 10 2129
天命终不由人
天命终不由人 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:52

    Here is how I did it with pdfrw:

    import sys, os, pdfrw
    writer = pdfrw.PdfWriter()
    for page in pdfrw.PdfReader('input.pdf').pages:
        for y in [0, 0.5]:
            newpage = pdfrw.PageMerge()    
            newpage.add(page, viewrect=(0, y, 1, 0.5))
            writer.addpages([newpage.render()])
    writer.write('output.pdf')
    

    Short and working!

    If you want it rotated (example: input A4 portrait, output 2 A5 portrait and not landscape):

    import sys, os, pdfrw
    writer = pdfrw.PdfWriter()
    for page in pdfrw.PdfReader('input.pdf').pages:
        for y in [0, 0.5]:
            newpage = pdfrw.PageMerge()    
            newpage.add(page, viewrect=(0, y, 1, 0.5))
            p = newpage.render()
            p.Rotate = 270
            writer.addpages([p])
    writer.write('output.pdf')
    

提交回复
热议问题