Skip to content

Instantly share code, notes, and snippets.

@chatchavan
Created November 14, 2015 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chatchavan/69f14bb5739862ad6f46 to your computer and use it in GitHub Desktop.
Save chatchavan/69f14bb5739862ad6f46 to your computer and use it in GitHub Desktop.
Extract the first page of PDF files in the current directory and combine them into a single PDF.
# requires: pip install pyPDF2
from PyPDF2 import PdfFileReader, PdfFileWriter
import os
# prepare output writer
pdfWriter = PdfFileWriter()
# loop reading the first page of each files
for aFileName in [fname for fname in os.listdir(".") if ".pdf" in fname]:
pdfReader = PdfFileReader(open(aFileName, "rb"))
firstPage = pdfReader.getPage(0)
pdfWriter.addPage(firstPage)
# save target PDF file
fOut = open("FirstPages.pdf", "wb")
pdfWriter.write(fOut)
fOut.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment