0% found this document useful (0 votes)
13 views1 page

add_page

The document contains a Python function that duplicates a specified page in a PDF file. It utilizes the PyPDF2 library to read an input PDF, copy its pages, and write the modified content to a new output PDF file. An example usage is provided, where the second page of 'dualdegree.pdf' is duplicated in 'DualDegree.pdf'.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views1 page

add_page

The document contains a Python function that duplicates a specified page in a PDF file. It utilizes the PyPDF2 library to read an input PDF, copy its pages, and write the modified content to a new output PDF file. An example usage is provided, where the second page of 'dualdegree.pdf' is duplicated in 'DualDegree.pdf'.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import PyPDF2

def duplicate_page(input_path, output_path, page_number):


# Open the PDF file
with open(input_path, 'rb') as input_file:
# Create a PDF reader object
pdf_reader = PyPDF2.PdfReader(input_file)

# Create a PDF writer object


pdf_writer = PyPDF2.PdfWriter()

# Iterate through each page in the input PDF


for page_index in range(len(pdf_reader.pages)):
# Add each page to the writer object
page = pdf_reader.pages[page_index]
pdf_writer.add_page(page)

# Duplicate the specified page


if page_index + 1 == page_number:
pdf_writer.add_page(page)

# Write the modified PDF to the output file


with open(output_path, 'wb') as output_file:
pdf_writer.write(output_file)

# Example usage
input_path = 'dualdegree.pdf'
output_path = 'DualDegree.pdf'
page_number = 2 # Duplicate the second page
duplicate_page(input_path, output_path, page_number)

You might also like