toc.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # SPDX-FileCopyrightText: 2024 geisserml <geisserml@gmail.com>
  2. # SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
  3. import pypdfium2.internal as pdfium_i
  4. # TODO? consider dotted access
  5. from pypdfium2._cli._parsers import (
  6. add_input,
  7. add_n_digits,
  8. get_input,
  9. round_list,
  10. )
  11. def attach(parser):
  12. add_input(parser, pages=False)
  13. add_n_digits(parser)
  14. parser.add_argument(
  15. "--max-depth",
  16. type = int,
  17. default = 15,
  18. help = "Maximum recursion depth to consider when parsing the table of contents",
  19. )
  20. def main(args):
  21. pdf = get_input(args)
  22. toc = pdf.get_toc(
  23. max_depth = args.max_depth,
  24. )
  25. for item in toc:
  26. state = "*" if item.n_kids == 0 else "-" if item.is_closed else "+"
  27. target = "?" if item.page_index is None else item.page_index+1
  28. print(
  29. " " * item.level +
  30. "[%s] %s -> %s # %s %s" % (
  31. state, item.title, target,
  32. pdfium_i.ViewmodeToStr.get(item.view_mode),
  33. round_list(item.view_pos, args.n_digits),
  34. )
  35. )