Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

merge pdf with python at same page

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)
Comment

PREVIOUS NEXT
Code Example
Python :: mypy run on single file 
Python :: python __div__ 
Python :: __sub__ 
Python :: __ge__ 
Python :: find max in for scartch python 
Python :: NumPy resize Example out of bound values [appending zeros] 
Python :: hide ticks without hiding grid 
Python :: funcs_and_args for loop python 
Python :: pandas use 3 columns for 2d distribution 
Python :: NumPy right_shift Code When inputs and bit shift are an arrays 
Python :: django view - mixins and GenericAPIView (list or create - GET, POST) 
Python :: pandas aggregate rename column 
Python :: downsample audio 
Python :: penggunaan clear di python 
Python :: Demonstration of Python range() 
Python :: python dependency injection 
Python :: How to convert an XML file to nice pandas dataframe 
Python :: list average python recursion 
Python :: how to aggregate and add new column 
Python :: Dynamic INSERT to SQLite 
Python :: Trying to set up flask with nginx and gunicorn 
Python :: variable bound to set python 
Python :: Invenco Order Dict 
Python :: ring Do Again Loop 
Python :: hacer un programa en python ingresar números enteros obtenga el segundo valor máximo 
Python :: get length of list python 
Python :: py3 identify file extension 
Python :: create a separate dataframe with the columns 
Python :: biodiversity 
Python :: selenium options to remember user 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =