I have over 50,000 PDF documents that need to be allocated to customer accounts. However each PDF contains pages I do not want the customer to see (internal company stuff) and so I would like to be able to delete specific pages based on their content ("for office use only" appearing on the page) and save the PDF as a new file.

I am comfortable with the scripting side, I just need to know what software is capable of such a task.


There are a number of libraries for manipulating PDF files, including pyPdf and is derivative PyPDF2. You can process a file page by page, extract the text and copy the pages whose text is suitable. Untested code:

#!/usr/bin/env python
import re, sys
from pyPdf import PdfFileWriter, PdfFileReader
input = PdfFileReader(sys.stdin)
output = PdfFileWriter()
for p in [input.getPage(i) for i in range(0,input.getNumPages())]:
    text = p.extractText()
    if not re.search(r'for\s+office\s+use\s+only', text, re.I):

Beware that the text may not appear in the document exactly the way you want it. In a PDF document, lines, words or even characters can appear out of order: rather than a stream of text, the document may be constructed with pieces appearing at certain coordinates. Even if the text is in order, which is usually the case, text that spans multiple pages will have a footer and a header in the middle. Text that spans multiple lines may have extra spaces around the line breaks. Even text on the same lines may have multiple spaces between words, due to text justification (that's why I used \s+ rather than a plain space in the regular expression above). Tools that extract text from PDF try to reconstruct the page as text but don't always do a perfect job. If pyPdf doesn't work well with your documents, you can try other libraries for text extraction; see this and this thread for some examples.

One option would be to look at command-line PDF editors like PDFEdit, Pdftk, or PDFSaM. pdftotext may be useful as a simple way to identify the page numbers to remove.

  • Why would you convert to postscript? What's the point? Extracting text from PS isn't easier than extracting from PDF, quite the contrary. – Gilles Oct 26 '16 at 0:04

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.