Forum Discussion
How do I extract just certain pages from a PDF?
This is a great script-based method to easily how to extract pdf pages without purchasing paid PDF editing software. It essentially lets you customize page selection logic to split PDF chapters locally via Python code, allowing you to set exact target page indexes and automate batch extraction to generate new segmented PDF documents.
Usage Guide: Install Python runtime on your computer and run pip install PyPDF2 in Command Prompt to install the library, create a new .py script file and paste the full processing code below, replace "input.pdf" with your source file path and adjust the page index list according to your required pages, then execute the script to export the extracted PDF file.
from PyPDF2 import PdfReader, PdfWriter
reader = PdfReader("input.pdf")
writer = PdfWriter()
# Extract pages 5, 12-15, 20
pages = [4, 11, 12, 13, 14, 19] # 0-indexed
for page_num in pages:
writer.add_page(reader.pages[page_num])
with open("extracted.pdf", "wb") as f:
writer.write(f)This method runs fully offline and swiftly helps you reliably how to extract pdf pages, effectively finishing customized single or batch PDF chapter splitting tasks without uploading files to cloud servers.
It is particularly useful for advanced users with basic coding skills, recurring mass PDF sorting work, or integrating automatic PDF page extraction functions into local automation scripts.