toc.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # SPDX-FileCopyrightText: 2026 geisserml <geisserml@gmail.com>
  2. # SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
  3. import pypdfium2.internal as pdfium_i
  4. from pypdfium2._cli._parsers import (
  5. add_input,
  6. add_n_digits,
  7. get_input,
  8. round_list,
  9. )
  10. def attach(parser):
  11. add_input(parser, pages=False)
  12. add_n_digits(parser)
  13. parser.add_argument(
  14. "--max-depth",
  15. type = int,
  16. default = 15,
  17. help = "Maximum recursion depth to consider when parsing the table of contents",
  18. )
  19. def main(args):
  20. pdf = get_input(args)
  21. toc = pdf.get_toc(max_depth=args.max_depth)
  22. for bm in toc:
  23. count, dest = bm.get_count(), bm.get_dest()
  24. out = " " * bm.level
  25. out += "[%s] %s -> " % (
  26. f"{count:+}" if count != 0 else "*",
  27. bm.get_title(),
  28. )
  29. # distinguish between "dest == None" and "dest with unknown mode" while keeping the output machine readable
  30. if dest:
  31. index, (view_mode, view_pos) = dest.get_index(), dest.get_view()
  32. out += "%s # %s %s" % (
  33. index+1 if index != None else "?",
  34. pdfium_i.ViewmodeToStr.get(view_mode),
  35. round_list(view_pos, args.n_digits),
  36. )
  37. else:
  38. out += "_"
  39. print(out)