Ani
Ani

Reputation: 157

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

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:\Tasks\Samples\fw9.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.

Upvotes: 5

Views: 11194

Answers (3)

al3rab sword
al3rab sword

Reputation: 21

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)

Upvotes: 2

nitin4july
nitin4july

Reputation: 11

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.

Upvotes: 1

freerafiki
freerafiki

Reputation: 561

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:\Tasks\Samples\fw9.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.

Upvotes: 2

Related Questions