from subprocess import run import sys outline = run( ['mutool', 'show', sys.argv[1], 'outline'], capture_output=True ).stdout.decode('utf-8') indent = 0 last_quote_index = 0 for line in outline.splitlines(): quote_index = line.find('"') hash_index = line.find('#') if quote_index > last_quote_index: indent += 1 elif quote_index < last_quote_index: indent -= 1 last_quote_index = quote_index title = line[quote_index+1:line.find('"', quote_index+1)].strip() page = int(line[hash_index+1:line.find(',', hash_index+1)]) print(f'{"#"*indent} {title} ({page})')