Error when trying to merge two slide slides

Viewed 23

How do you merge two slide slides saved as a PDF? Specifically, I want to know how to add a second slide to a first slide. I tried copying the code in a Python Udemy course while changing the variables' names, I found one solution online that looked very, very not concise, and I glanced at a stack overflow thread that ended in there maybe not being a solution. I got an error four times when running Udemy code that one of my PDFs was corrupted and never got a slide added to another slide. The expected output is a pdf file with two slides, one from each pdf file. Here's a very rough excerpt of my code (attached). Note, the practice presentations I tried to merge I called ~sept-1.pdf and second-page.pdf + I changed the username in the file path to eliminate my nickname.

Goal: Add the one and only slide from pdf 2 and add it to the end of pdf 1.

Import modules

!pip install PyPDF4 then import PyPDF4

Change directory to desktop

import os
os.getcwd()

os.chdir('/Users/mac_username_name/Desktop')
os.getcwd()

Stuff after chaging the directory to the desktop

Adding to my desired PDF

f = open('sept-1.pdf','rb')
pdf_reader = PyPDF4.PdfFileReader(f)

first_page = pdf_reader.getPage(0)

pdf_writer = PyPDF4.PdfFileWriter()

pdf_writer.addPage(first_page)

pdf_output = open("newer.pdf","wb")

pdf_writer.write(pdf_output)

f.close()
0 Answers
Related