Advertisement
Guest User

mkevac

a guest
Mar 26th, 2009
1,361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import pyPdf
  4. import re
  5. import isbn
  6. import sys
  7.  
  8. def get_pdf_content(path, pages_count=10):
  9.     content = ""
  10.  
  11.     pdf = pyPdf.PdfFileReader(file(path, "rb"))
  12.  
  13.     pages_total = pdf.getNumPages()
  14.  
  15.     if pages_total < pages_count:
  16.         pages = pages_total
  17.     else:
  18.         pages = pages_count
  19.  
  20.     for i in xrange(0, pages):
  21.         content += pdf.getPage(i).extractText() + "\n"
  22.  
  23.     content = " ".join(content.replace(u"\xa0", " ").strip().split())
  24.     return content.encode("ascii", "ignore")
  25.  
  26. content = get_pdf_content(sys.argv[1])
  27.  
  28. isbn_13 = re.findall("ISBN.{0,15}([0-9][-]?[0-9][-]?[0-9][-]?[0-9][-]?[0-9][-]?[0-9][-]?[0-9][-]?[0-9][-]?[0-9][-]?[0-9][-]?[0-9][-]?[0-9][-]?[0-9])", content)
  29. isbn_10 = re.findall("ISBN.{0,15}([0-9][-]?[0-9][-]?[0-9][-]?[0-9][-]?[0-9][-]?[0-9][-]?[0-9][-]?[0-9][-]?[0-9][-]?[0-9])", content)
  30.  
  31. isbn_13 = list(set(isbn_13))
  32. isbn_10 = list(set(isbn_10))
  33.  
  34. isbn_13_valid = [i for i in isbn_13 if isbn.isValid(i)]
  35. isbn_10_valid = [i for i in isbn_10 if isbn.isValid(i)]
  36.  
  37. print "ISBN-13:", isbn_13_valid
  38. print "ISBN-10:", isbn_10_valid
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement