Skip to content

Instantly share code, notes, and snippets.

@sueszli
Created August 8, 2023 23:53
Show Gist options
  • Save sueszli/dcdbe482c21cf536befffef96d2a2bfe to your computer and use it in GitHub Desktop.
Save sueszli/dcdbe482c21cf536befffef96d2a2bfe to your computer and use it in GitHub Desktop.
limit pdf files to specific pages
from PyPDF2 import PdfFileReader, PdfFileWriter
import os
inputPath = "./solutions"
outputPath = "./extractedPages"
chosenPages = [1]
if not os.path.exists(outputPath):
os.makedirs(outputPath)
for fileName in os.listdir(inputPath):
pdf = PdfFileReader(inputPath + "/" + fileName)
pdfWriter = PdfFileWriter()
for page_num in chosenPages:
pdfWriter.addPage(pdf.getPage(page_num))
with open(outputPath + "/" + fileName, "wb") as f:
pdfWriter.write(f)
print("Extracted the pages", chosenPages, "from the pdfs")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment