Skip to content

Instantly share code, notes, and snippets.

@amitness
Created March 13, 2019 11:57
Show Gist options
  • Save amitness/32ee028889cde8fba2e34615b2f9eee2 to your computer and use it in GitHub Desktop.
Save amitness/32ee028889cde8fba2e34615b2f9eee2 to your computer and use it in GitHub Desktop.
Read the TOC of a PDF using Python
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument
import sys
# python tldr.py filename.pdf
fp = open(sys.argv[1], 'rb')
parser = PDFParser(fp)
doc = PDFDocument(parser)
outlines = doc.get_outlines()
for (level, title, dest, a, se) in outlines:
print ' ' * (level - 1) + title
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment