Skip to content

Instantly share code, notes, and snippets.

@cellularmitosis
Last active December 5, 2019 00:23
Show Gist options
  • Save cellularmitosis/1dabd7587662f8aa7652369472595806 to your computer and use it in GitHub Desktop.
Save cellularmitosis/1dabd7587662f8aa7652369472595806 to your computer and use it in GitHub Desktop.
Display a random page from a random PDF

Blog 2019/11/27

<- previous | index | next ->

Display a random page from a random PDF

Here's a Python script which will display a random page from a random PDF (in the current directory).

Assumptions:

  • macOS
  • pdftk
  • pdfinfo (brew install poppler)

UPDATE: added a variant of the script which is a Flask web app.

#!/usr/bin/env python
import os
import random
import subprocess
def display_random_page():
"display a random page from a random book."
book_fname = random_book()
pages = page_count(book_fname)
pagenum = random.randrange(pages) + 1
print("%s, page %s of %s" % (book_fname, pagenum, pages))
page_fname = pluck_page(book_fname, pagenum)
display_page(page_fname)
def random_book():
"return a random book among the books in the current directory."
books = [fname for fname in os.listdir('.') if fname.endswith('.pdf')]
return random.choice(books)
def page_count(fname):
"return the page count of the given book."
cmd = "pdfinfo %s | grep Pages: | awk '{print $2}'" % fname
output = subprocess.check_output(cmd, shell=True)
return int(output)
def pluck_page(book_fname, pagenum):
"extract the given page from the given book."
cmd = "pdftk %s cat %s output /tmp/random-page.pdf" % (book_fname, pagenum)
subprocess.check_call(cmd, shell=True)
return "/tmp/random-page.pdf"
def display_page(fname):
"display the extracted book page."
subprocess.check_call("open %s" % fname, shell=True)
if __name__ == "__main__":
display_random_page()
#!/usr/bin/env python
import os
import random
import subprocess
def random_book(path):
"return a random book among the books in the current directory."
books = [fname for fname in os.listdir(path) if fname.endswith('.pdf')]
return random.choice(books)
def page_count(fpath):
"return the page count of the given book."
cmd = "pdfinfo %s | grep Pages: | awk '{print $2}'" % fpath
output = subprocess.check_output(cmd, shell=True)
return int(output)
def pluck_page(book_fname, pagenum):
"extract the given page from the given book."
cmd = "pdftk %s cat %s output /tmp/random-page.pdf" % (book_fname, pagenum)
subprocess.check_call(cmd, shell=True)
title = "%s, page %s" % (book_fname.split('/')[-1], pagenum)
cmd = "exiftool -Title=\"%s\" /tmp/random-page.pdf" % title
subprocess.check_call(cmd, shell=True)
return "/tmp/random-page.pdf"
import flask
application = flask.Flask(__name__)
@application.route("/")
def GET_root(methods=['GET']):
books_dir = '/path/to/pdfs/'
book_fname = random_book(books_dir)
pages = page_count(books_dir + book_fname)
pagenum = random.randrange(pages) + 1
page_fname = pluck_page(books_dir + book_fname, pagenum)
with open(page_fname) as f:
pdf = f.read()
response = flask.make_response(pdf)
response.headers['Content-Type'] = 'application/pdf'
response.headers['Content-Disposition'] = \
'inline; filename=%s' % book_fname
response.headers["x-filename"] = book_fname
response.headers["Access-Control-Expose-Headers"] = 'x-filename'
return response
if __name__ == "__main__":
application.run('0.0.0.0', debug=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment