How to Merge two pages from a pdf file as one page

Question:

I have a pdf in which there are total 6 pages of images.I want to merge page 1 and 2 as a single pdf and so on for 3 to 6 pages.

I splitted all 6 pages of pdf as individual pdf.

import os
from PyPDF2 import PdfFileReader, PdfFileWriter

def pdf_splitter(path):
fname = os.path.splitext(os.path.basename(path))[0]

pdf = PdfFileReader(path)
for page in range(pdf.getNumPages()):
    pdf_writer = PdfFileWriter()
    pdf_writer.addPage(pdf.getPage(page))

    output_filename = '{}_page_{}.pdf'.format(
        fname, page+1)

    with open(output_filename, 'wb') as out:
        pdf_writer.write(out)

    print('Created: {}'.format(output_filename))

if name == ‘main‘:
path = ‘D:TasksSamplesfw9.pdf’
pdf_splitter(path)

I want to know how to merge page 1 and 2 of fw9 as single pdf file which contains only 1 page which have half page as page 1 of fw9 pdf file and another half as page 2 of fw9 pdf.I have to do this for all 6 pages as 1-2 as 1 pdf with 1 page ,3-4 page as another pdf which has only 1 page with both on the same page and so on.Kindly help if any one have any idea on how to do so.

Asked By: Ani

||

Answers:

The library pyPDF2 has also a PdfFileMerger object, that should do exactly what you want.

As from the example here you can just create a PdfFileMerger, read two pages and put them into one single file.

I changed your script slightly to create also files with pages 0-1, 2-3, 4-5 ecc.. (of course page 0 is the first page but python numbering starts from 0)

import os
from PyPDF2 import PdfFileReader, PdfFileWriter, PdfFileMerger

def pdf_splitter(path): 

    fname = os.path.splitext(os.path.basename(path))[0]

    pdf = PdfFileReader(path)
    input_paths = []
    for page in range(pdf.getNumPages()):
        pdf_writer = PdfFileWriter()
        pdf_writer.addPage(pdf.getPage(page))
        output_filename = '{}_page_{}.pdf'.format(fname, page+1)
        input_paths.append(output_filename)
        with open(output_filename, 'wb') as out:
            pdf_writer.write(out)

        print('Created: {}'.format(output_filename))

        # every 2 pages! 
        # Change the two if you need every other number of pages!
        if page % 2 == 1:
            pdf_merger = PdfFileMerger() #create pdfilemerger
            for path in input_paths: 
                pdf_merger.append(path) #read the single pages

            # we call it pages_N-1_N, so first would be pages_0_1!
            output_path = '{}_pages_{}_{}.pdf'.format(fname, page-1, page)
            with open(output_path, 'wb') as fileobj:
                pdf_merger.write(fileobj) # write the two pages pdf!

            input_paths = []

if __name__ == '__main__': 

    path = 'D:TasksSamplesfw9.pdf' 
    pdf_splitter(path)

Is this what you wanted?

This will first create single pdf for each page and then combine them 2 to 2. Creating the single pdf could also be skipped, but I was not sure whether you want it or not.

Answered By: freerafiki
from PyPDF2 import PdfFileReader, PdfFileWriter
from PyPDF2 import PageObject

#Open the files that have to be merged
pdf1File = open('document.pdf', 'rb')

#Read the files that you have opened
pdf1Reader = PdfFileReader(pdf1File)

#Make a list of all pages
pages = []
for pageNum in range(pdf1Reader.numPages):
    pageObj = pdf1Reader.getPage(pageNum)
    pages.append(pageObj)

#Calculate width and height for final output page
width = pages[0].mediaBox.getWidth() * 6
height = pages[0].mediaBox.getHeight() + 100

#Create blank page to merge all pages in one page
merged_page = PageObject.createBlankPage(None, width, height)

#Loop through all pages and merge / add them to blank page
x = 0
for page in pages:
    merged_page.mergeScaledTranslatedPage(page, 1, x, 10)
    x = float(x) + float(page.mediaBox.getWidth())

#Create final file with one page
writer = PdfFileWriter()
writer.addPage(merged_page)

with open('out.pdf', 'wb') as f:
    writer.write(f)

I wanted to merge 6 files / page so I have used 6 as a multiplier for page width.

Answered By: nitin4july

this is the answer of how to merge two pages to one page in vertical way

from PyPDF2 import PdfFileReader, PdfFileWriter
from PyPDF2 import PageObject

#Open the files that have to be merged
pdf1File = open('1.pdf', 'rb')

#Read the files that you have opened
pdf1Reader = PdfFileReader(pdf1File)

#Make a list of all pages
pages = []
for pageNum in range(pdf1Reader.numPages):
    pageObj = pdf1Reader.getPage(pageNum)
    pages.append(pageObj)

#Calculate width and height for final output page
width = pages[1].mediaBox.getWidth() * 2
height = pages[1].mediaBox.getHeight() 

#Create blank page to merge all pages in one page
merged_page = PageObject.createBlankPage(None, width, height)
writer = PdfFileWriter()
#Loop through all pages and merge / add them to blank page
y =0
merged_page = PageObject.createBlankPage(None, width, height)
for page in range(len(pages)):
    y+=1
    if y%2!=0:
        merged_page.mergePage(pages[page])
        x=float(pages[page+1].mediaBox.getWidth())
        merged_page.mergeScaledTranslatedPage(pages[page+1], 1,x, 0)
    if y%2==0:
        writer.addPage(merged_page)
        merged_page = PageObject.createBlankPage(None, width, height)
        y=0
    
    

#Create final file with one page



with open('out.pdf', 'wb') as f:
    writer.write(f)

Answered By: al3rab sword

@al3rab sword, when I try your code, I get the output but I am expecting odd numbered pages(1,3,5..) on left side and even numbered pages(2,4,6…) on right side.
Currently this is not happening.

import PyPDF2

#Open the files that have to be merged
pdf1File = open('random.pdf', 'rb')

#Read the files that you have opened
pdf1Reader = PyPDF2.PdfReader(pdf1File)

#Make a list of all pages
pag = []
for pageNum in range(len(pdf1Reader.pages)):
    pageObj = pdf1Reader.pages[pageNum]
    pag.append(pageObj)

#Calculate width and height for final output page
#box = pdf1Reader.pages[0].mediabox
w = pag[1].mediabox.width * 2
h = pag[1].mediabox.height 
print(w)
print(h)
#print(len(pag))
#print(pag[1])

#Create blank page to merge all pages in one page
merged_page = PyPDF2.PageObject.create_blank_page(None, w, h)
writer = PyPDF2.PdfWriter()
#Loop through all pages and merge / add them to blank page
y =0
merged_page = PyPDF2.PageObject.create_blank_page(None, w, h)
print(len(pag))
for page in range(len(pag)):
    y+=1 
    print(y)
    if y%2!=0:
        print(page)
        merged_page.merge_page(pag[page])
        print(page)
        x=float(pag[page+1].mediabox.width)
        op = PyPDF2.Transformation().scale(1.0).translate(x, 0.0)
        merged_page.add_transformation(op)
        merged_page.merge_page(pag[page+1])


    if y%2==0:
        print('Hello')
        writer.add_page(merged_page)
        merged_page = PyPDF2.PageObject.create_blank_page(None, w, h)
        y=0
    
#Create final file with one page
with open('out.pdf', 'wb') as f:
    writer.write(f)

Answered By: Amey Shah
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.