code: def parse_pages(
      root,
      load_page_nums=None,
      require_image=False,
      select_image="first",
      use_images=None,
  ):
      """
      Parses an XML structure in pdf2xml format to extract the pages with their text boxes.
      <root> is the XML tree root
      <load_page_nums> allows to define a sequence of page numbers that should be loaded (by default, all pages
      will be loaded).
      <select_image> if there's more than one background image in a page, select an image according to one of these
                     criteria:
                     'first': simply use the first image
                     'topleft': use the one with top="0" and left="0"
      <use_images> pass a dict with page number -> image string mapping to set which images to use for which page
      position.
  
      Return an OrderedDict with page number -> page dict mapping.
      """
      use_images = use_images or {}
      pages = OrderedDict()
  
      for p in root.findall("page"):  # parse all pages
          p_num = int(p.attrib["number"])
  
          if load_page_nums is not None and p_num not in load_page_nums:
              continue
  
          # find all images of the page
          p_images = p.findall("image")
          if p_images:
              use_image = use_images.get(p_num, None)
              if use_image:
                  imgfile = use_image
              else:
                  if len(p_images) == 1:
                      imgfile = p_images[0].attrib["src"]
                  else:
                      if select_image == "first":
                          imgfile = p_images[0].attrib["src"]
                      elif select_image == "topleft":
                          for imgtag in p_images:
                              if (
                                  int(imgtag.attrib["top"]) == 0
                                  and int(imgtag.attrib["left"]) == 0
                              ):
                                  imgfile = imgtag.attrib["src"]
                                  break
                          else:
                              raise ValueError(
                                  "multiple images on page %d but none of it is in the top left corner"
                                  % p_num
                              )
                      else:
                          raise ValueError(
                              'invalid value for parameter `select_image`: "%s"'
                              % select_image
                          )
          else:
              if require_image:
                  raise ValueError(
                      "no image given on page %d but require_image was set to True"
                      % p_num
                  )
              else:
                  imgfile = None
  
          # create the page dict structure
          page = {
              "number": p_num,
              "image": imgfile,
              "width": int(float(p.attrib["width"])),
              "height": int(float(p.attrib["height"])),
              "texts": [],
              "xmlnode": p,
          }
  
          # add the text boxes to the page
          for t in p.findall("text"):
              tdict = create_text_dict(t)
              try:
                  rect(tdict["topleft"], tdict["bottomright"])
              except ValueError:
                  # seems like there are rectangles with zero area
                  continue  # -> skip them
  
              # join all text elements to one string
              tdict["value"] = " ".join(t.itertext())
  
              page["texts"].append(tdict)
  
          pages[p_num] = page
  
      return pages
  
imports: import xml.etree.ElementTree
  from collections import OrderedDict
  import json
  import struct
  import imghdr
  import numpy
  from .geom import pt, rect
label_desc: Use image node_id 3 for calling a global function with example usage: parse_pages(root, load_page_nums, require_image, select_image, use_images) and returns: pages