PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 29, 2022

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

 July 29, 2022     image, merge, pdf, python, split     No comments   

Issue

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.


Solution

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.



Answered By - freerafiki
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing