Python 3 pdfrw Library Tutorial to Concatenate Multiple Pages into One Output PDF File Full Project For Beginners



Welcome folks today in this blog post we will be concatenating multiple pages into one pdf file in python using pdfrw library . All the full source code of the application is shown below.





Get Started





In order to get started you need to install the below library using the pip command as shown below



pip install pdfrw



After installing this library you need to make an file and copy paste the following code


See also  Python 3 (Tabula) Example Script to Extract All Tables From PDF Document and Convert to XLSX and CSV Files Full Project For Beginners


#!/usr/bin/env python

usage: my.pdf
Creates with a single output page for every
4 input pages.

import sys
import os

from pdfrw import PdfReader, PdfWriter, PageMerge

def get4(srcpages):
    scale = 0.5
    srcpages = PageMerge() + srcpages
    x_increment, y_increment = (scale * i for i in srcpages.xobj_box[2:])
    for i, page in enumerate(srcpages):
        page.x = x_increment if i & 1 else 0
        page.y = 0 if i & 2 else y_increment
    return srcpages.render()

inpfn, = sys.argv[1:]
outfn = '4up.' + os.path.basename(inpfn)
pages = PdfReader(inpfn).pages
writer = PdfWriter(outfn)
for index in range(0, len(pages), 4):
    writer.addpage(get4(pages[index:index + 4]))



Now to run this python file we need to run the below command



python test.pdf

Leave a Reply