In this tutorial, I will be showing you how to extract specific pages (or split specific pages) from a PDF file and save those pages as a separate PDF using Python.
Before we dive into tutorial, you will need to install PyPDF2 library (pip install PyPDF2).
Buy Me a Coffee? Your support is much appreciated!
PayPal Me: https://www.paypal.me/jiejenn/5
Venmo: @Jie-Jenn
Source Code:
from PyPDF2 import PdfFileReader, PdfFileWriter
pdf_file_path = 'Unknown.pdf'
file_base_name = pdf_file_path.replace('.pdf', '')
pdf = PdfFileReader(pdf_file_path)
pages = [0, 2, 4] # page 1, 3, 5
pdfWriter = PdfFileWriter()
for page_num in pages:
pdfWriter.addPage(pdf.getPage(page_num))
with open('{0}_subset.pdf'.format(file_base_name), 'wb') as f:
pdfWriter.write(f)
f.close()
Hello, I watched your video on youtube. Your videos are amazing. I was curious, is there a way to get this script to to use file names in a column from a .csv file as variable inputs ?
My preferred method is use either CSV module or Pandas library to read the data from a CSV file and store the dataset into an array, them I can iterate each row of values accordingly.
Hello can you help me to add paragraph in particular page word using python