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 app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#!/usr/bin/env python ''' usage: 4up.py my.pdf Creates 4up.my.pdf 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.scale(scale) 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])) writer.write() |
Now to run this python
file we need to run the below command
python app.py test.pdf