Created
August 8, 2023 23:53
-
-
Save sueszli/dcdbe482c21cf536befffef96d2a2bfe to your computer and use it in GitHub Desktop.
limit pdf files to specific pages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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