2

I am looking for a free program or script that can take a folder path as input and output the word count of every PDF file located in the folder and its subfolders. It should work on Windows 7 SP1 x64 Ultimate and be free.

4

Install Python2.7 for windows from here - install PyPDF2 and use a script with a function like:

import PyPDF2 as ppdf
from os.path import walk, join, splitext
import sys

def getPDFWordCount(path):
    word_count = 0
    # Load PDF into pyPDF
    pdf = ppdf.PdfFileReader(file(path, "rb"))
    # Iterate pages
    for i in range(0, pdf.getNumPages()):
        # Extract text from page and add to content
        content = pdf.getPage(i).extractText()
        # Collapse whitespace and count
        word_count += len(content.replace(u"\xa0", " ").strip().split())
    return word_count

if __ name__ == "__main__":
    for arg in sys.argv[1:]:
        for (root, dirs, files) in walk(arg):
            for filename in files:
                (name, ext) = splitext(filename)
                if ext.lower() == '.pdf':
                    filepath = join(root, filename)
                    print filepath, getPDFWordCount(filepath)

Take a look at the examples of os.path.walk and splitext to get some ideas how to iterate through directories and how to identify pdf files.

This should work on just about any platform even windows but has not been tested on any platform. If you need one you can put a GUI front end on with wxPython.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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