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

Combine PDF

The document contains a Python function that merges the last page of a source PDF into a target PDF. It uses the PyPDF2 library to read and write PDF files. The function saves the merged result as 'merged_pdf.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)
6 views1 page

Combine PDF

The document contains a Python function that merges the last page of a source PDF into a target PDF. It uses the PyPDF2 library to read and write PDF files. The function saves the merged result as 'merged_pdf.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 add_last_page(source_pdf_path, target_pdf_path):


with open(source_pdf_path, 'rb') as source_file:
source_pdf = PyPDF2.PdfReader(source_file)
last_page = source_pdf.pages[-1]

with open(target_pdf_path, 'rb') as target_file:


target_pdf = PyPDF2.PdfReader(target_file)
output_pdf = PyPDF2.PdfWriter()

# Add all pages of the target PDF except the last one
for page_num in range(len(target_pdf.pages) - 1):
output_pdf.add_page(target_pdf.pages[page_num])

# Add the last page of the source PDF


output_pdf.add_page(last_page)

with open('merged_pdf.pdf', 'wb') as output_file:


output_pdf.write(output_file)

# Example usage
source_pdf_path = 'dualdegree0.pdf'
target_pdf_path = 'dualdegree.pdf'
add_last_page(source_pdf_path, target_pdf_path)

You might also like