content: #!/usr/bin/env python3
  
  import os
  import re
  import unittest
  from collections import deque
  
  from pdfminer.pdftypes import resolve1
  
  import pdfplumber
  from pdfplumber.structure import PDFStructTree
  
  HERE = os.path.abspath(os.path.dirname(__file__))
  TREE = [
      {
          "type": "Document",
          "children": [
              {
                  "type": "P",
                  "attributes": {
                      "O": "Layout",
                      "Placement": "Block",
                      "SpaceBefore": 0.24,
                      "TextAlign": "Center",
                  },
                  "mcids": [0],
              },
              {
                  "type": "H1",
                  "attributes": {
                      "O": "Layout",
                      "Placement": "Block",
                      "SpaceBefore": 0.36,
                  },
                  "mcids": [1],
              },
              {
                  "type": "P",
                  "attributes": {
                      "O": "Layout",
                      "Placement": "Block",
                      "SpaceBefore": 0.12,
                  },
                  "mcids": [2],
              },
              {
                  "type": "P",
                  "attributes": {
                      "O": "Layout",
                      "Placement": "Block",
                      "SpaceBefore": 0.181,
                  },
                  "mcids": [3, 4, 5, 6, 7],
              },
              {
                  "type": "H2",
                  "attributes": {
                      "O": "Layout",
                      "Placement": "Block",
                      "SpaceBefore": 0.381,
                  },
                  "mcids": [8],
              },
              {
                  "type": "P",
                  "attributes": {
                      "O": "Layout",
                      "Placement": "Block",
                      "SpaceBefore": 0.12,
                  },
                  "mcids": [9],
              },
              {
                  "type": "L",
                  "children": [
                      {
                          "type": "LI",
                          "children": [
                              {
                                  "type": "LBody",
                                  "children": [
                                      {
                                          "type": "P",
                                          "attributes": {
                                              "O": "Layout",
                                              "Placement": "Block",
                                              "SpaceBefore": 0.181,
                                              "StartIndent": 0.36,
                                          },
                                          "mcids": [10, 11],
                                      }
                                  ],
                              }
                          ],
                      },
                      {
                          "type": "LI",
                          "children": [
                              {
                                  "type": "LBody",
                                  "children": [
                                      {
                                          "type": "P",
                                          "attributes": {
                                              "O": "Layout",
                                              "Placement": "Block",
                                              "SpaceBefore": 0.181,
                                              "StartIndent": 0.36,
                                          },
                                          "mcids": [12, 13],
                                      },
                                      {
                                          "type": "L",
                                          "children": [
                                              {
                                                  "type": "LI",
                                                  "children": [
                                                      {
                                                          "type": "LBody",
                                                          "children": [
                                                              {
                                                                  "type": "P",
                                                                  "attributes": {
                                                                      "O": "Layout",
                                                                      "Placement": "Block",  # noqa: E501
                                                                      "SpaceBefore": 0.181,  # noqa: E501
                                                                      "StartIndent": 0.72,  # noqa: E501
                                                                  },
                                                                  "mcids": [14, 15],
                                                              }
                                                          ],
                                                      }
                                                  ],
                                              }
                                          ],
                                      },
                                  ],
                              }
                          ],
                      },
                      {
                          "type": "LI",
                          "children": [
                              {
                                  "type": "LBody",
                                  "children": [
                                      {
                                          "type": "P",
                                          "attributes": {
                                              "O": "Layout",
                                              "Placement": "Block",
                                              "SpaceBefore": 0.181,
                                              "StartIndent": 0.36,
                                          },
                                          "mcids": [16, 17, 18, 19, 20, 21, 22, 23],
                                      }
                                  ],
                              }
                          ],
                      },
                  ],
              },
              {
                  "type": "H3",
                  "attributes": {
                      "O": "Layout",
                      "Placement": "Block",
                      "SpaceBefore": 0.321,
                  },
                  "mcids": [24],
              },
              {
                  "type": "Table",
                  "attributes": {
                      "O": "Layout",
                      "Placement": "Block",
                      "SpaceBefore": 0.12,
                      "SpaceAfter": 0.015,
                      "Width": 9.972,
                      "Height": 1.047,
                      "BBox": [56.7, 249.75, 555.3, 302.1],
                  },
                  "children": [
                      {
                          "type": "TR",
                          "attributes": {"O": "Layout", "Placement": "Block"},
                          "children": [
                              {
                                  "type": "TH",
                                  "attributes": {
                                      "O": "Layout",
                                      "Placement": "Inline",
                                      "Width": 4.985,
                                      "Height": 0.291,
                                  },
                                  "children": [
                                      {
                                          "type": "P",
                                          "attributes": {
                                              "O": "Layout",
                                              "Placement": "Block",
                                          },
                                          "mcids": [25],
                                      }
                                  ],
                              },
                              {
                                  "type": "TH",
                                  "attributes": {
                                      "O": "Layout",
                                      "Placement": "Inline",
                                      "Width": 4.987,
                                      "Height": 0.291,
                                  },
                                  "children": [
                                      {
                                          "type": "P",
                                          "attributes": {
                                              "O": "Layout",
                                              "Placement": "Block",
                                          },
                                          "mcids": [26],
                                      }
                                  ],
                              },
                          ],
                      },
                      {
                          "type": "TR",
                          "attributes": {"O": "Layout", "Placement": "Block"},
                          "children": [
                              {
                                  "type": "TD",
                                  "attributes": {
                                      "O": "Layout",
                                      "Placement": "Inline",
                                      "Width": 4.985,
                                      "Height": 0.291,
                                  },
                                  "children": [
                                      {
                                          "type": "P",
                                          "attributes": {
                                              "O": "Layout",
                                              "Placement": "Block",
                                          },
                                          "mcids": [27],
                                      }
                                  ],
                              },
                              {
                                  "type": "TD",
                                  "attributes": {
                                      "O": "Layout",
                                      "Placement": "Inline",
                                      "Width": 4.987,
                                      "Height": 0.291,
                                  },
                                  "children": [
                                      {
                                          "type": "P",
                                          "attributes": {
                                              "O": "Layout",
                                              "Placement": "Block",
                                          },
                                          "mcids": [28],
                                      }
                                  ],
                              },
                          ],
                      },
                      {
                          "type": "TR",
                          "attributes": {"O": "Layout", "Placement": "Block"},
                          "children": [
                              {
                                  "type": "TD",
                                  "attributes": {
                                      "O": "Layout",
                                      "Placement": "Inline",
                                      "Width": 4.985,
                                      "Height": 0.33,
                                  },
                                  "children": [
                                      {
                                          "type": "P",
                                          "attributes": {
                                              "O": "Layout",
                                              "Placement": "Block",
                                          },
                                          "mcids": [29],
                                      }
                                  ],
                              },
                              {
                                  "type": "TD",
                                  "attributes": {
                                      "O": "Layout",
                                      "Placement": "Inline",
                                      "Width": 4.987,
                                      "Height": 0.33,
                                  },
                                  "children": [
                                      {
                                          "type": "P",
                                          "attributes": {
                                              "O": "Layout",
                                              "Placement": "Block",
                                          },
                                          "mcids": [30],
                                      }
                                  ],
                              },
                          ],
                      },
                  ],
              },
          ],
      }
  ]
  
  
  class Test(unittest.TestCase):
      """Test a PDF specifically created to show structure."""
  
      @classmethod
      def setup_class(self):
          path = os.path.join(HERE, "pdfs/pdf_structure.pdf")
          self.pdf = pdfplumber.open(path)
  
      @classmethod
      def teardown_class(self):
          self.pdf.close()
  
      def test_structure_tree(self):
          assert self.pdf.pages[0].structure_tree == TREE
          # Add page numbers
          d = deque(TREE)
          while d:
              el = d.popleft()
              el["page_number"] = 1
              if "children" in el:
                  d.extend(el["children"])
          assert self.pdf.structure_tree == TREE
  
  
  PVSTRUCT = [
      {
          "type": "Sect",
          "children": [
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [0]},
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [1]},
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [2]},
              {"type": "P", "lang": "FR-FR", "page_number": 1, "mcids": [3]},
              {"type": "P", "lang": "FR-FR", "page_number": 1, "mcids": [4]},
              {"type": "P", "lang": "FR-FR", "page_number": 1, "mcids": [5]},
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [6]},
              {"type": "P", "lang": "FR-FR", "page_number": 1, "mcids": [7]},
              {
                  "type": "P",
                  "lang": "FR-FR",
                  "page_number": 1,
                  "mcids": [8],
                  "children": [
                      {"type": "Span", "lang": "FR-CA", "page_number": 1, "mcids": [9]}
                  ],
              },
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [11]},
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [12]},
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [13]},
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [14]},
              {"type": "P", "lang": "FR-FR", "page_number": 1, "mcids": [15]},
              {"type": "P", "lang": "FR-FR", "page_number": 1, "mcids": [16]},
              {
                  "type": "L",
                  "children": [
                      {
                          "type": "LI",
                          "children": [
                              {
                                  "type": "LBody",
                                  "lang": "FR-CA",
                                  "page_number": 1,
                                  "mcids": [19],
                              }
                          ],
                      }
                  ],
              },
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [22]},
              {"type": "P", "lang": "FR-FR", "page_number": 1, "mcids": [23]},
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [24]},
              {
                  "type": "L",
                  "children": [
                      {
                          "type": "LI",
                          "children": [
                              {
                                  "type": "LBody",
                                  "lang": "FR-CA",
                                  "page_number": 1,
                                  "mcids": [27],
                              }
                          ],
                      }
                  ],
              },
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [30]},
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [31]},
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [32]},
              {
                  "type": "L",
                  "children": [
                      {
                          "type": "LI",
                          "children": [
                              {
                                  "type": "LBody",
                                  "lang": "FR-CA",
                                  "page_number": 1,
                                  "mcids": [35],
                              }
                          ],
                      }
                  ],
              },
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [38]},
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [39]},
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [40]},
              {
                  "type": "L",
                  "children": [
                      {
                          "type": "LI",
                          "children": [
                              {
                                  "type": "LBody",
                                  "lang": "FR-CA",
                                  "page_number": 1,
                                  "mcids": [43, 45],
                                  "children": [
                                      {
                                          "type": "Span",
                                          "lang": "FR-FR",
                                          "page_number": 1,
                                          "mcids": [44],
                                      }
                                  ],
                              }
                          ],
                      }
                  ],
              },
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [48]},
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [49]},
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [50]},
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [51]},
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [52]},
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [53]},
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [54]},
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [55]},
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [56]},
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [57]},
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [58]},
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [59]},
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [60]},
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [61]},
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [62]},
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [63]},
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [64]},
              {"type": "P", "lang": "FR-CA", "page_number": 1, "mcids": [65]},
              {"type": "P", "lang": "FR-CA", "page_number": 2, "mcids": [0]},
              {"type": "P", "lang": "FR-CA", "page_number": 2, "mcids": [1]},
              {"type": "P", "lang": "FR-CA", "page_number": 2, "mcids": [2]},
              {"type": "P", "lang": "FR-CA", "page_number": 2, "mcids": [3]},
              {"type": "P", "lang": "FR-CA", "page_number": 2, "mcids": [4]},
              {"type": "P", "lang": "FR-CA", "page_number": 2, "mcids": [5]},
              {"type": "P", "lang": "FR-CA", "page_number": 2, "mcids": [6]},
              {
                  "type": "L",
                  "children": [
                      {
                          "type": "LI",
                          "children": [
                              {
                                  "type": "LBody",
                                  "lang": "FR-CA",
                                  "page_number": 2,
                                  "mcids": [9, 11],
                                  "children": [
                                      {
                                          "type": "Span",
                                          "lang": "FR-FR",
                                          "page_number": 2,
                                          "mcids": [10],
                                      }
                                  ],
                              }
                          ],
                      }
                  ],
              },
              {"type": "P", "lang": "FR-CA", "page_number": 2, "mcids": [14]},
              {"type": "P", "lang": "FR-CA", "page_number": 2, "mcids": [15]},
              {"type": "P", "lang": "FR-CA", "page_number": 2, "mcids": [16]},
              {"type": "P", "lang": "FR-FR", "page_number": 2, "mcids": [17]},
              {"type": "P", "lang": "FR-FR", "page_number": 2, "mcids": [18]},
              {"type": "P", "lang": "FR-FR", "page_number": 2, "mcids": [19]},
          ],
      }
  ]
  
  
  PVSTRUCT1 = [
      {
          "type": "Sect",
          "children": [
              {"lang": "FR-CA", "type": "P", "mcids": [0]},
              {"lang": "FR-CA", "type": "P", "mcids": [1]},
              {"lang": "FR-CA", "type": "P", "mcids": [2]},
              {"lang": "FR-CA", "type": "P", "mcids": [3]},
              {"lang": "FR-CA", "type": "P", "mcids": [4]},
              {"lang": "FR-CA", "type": "P", "mcids": [5]},
              {"lang": "FR-CA", "type": "P", "mcids": [6]},
              {
                  "type": "L",
                  "children": [
                      {
                          "type": "LI",
                          "children": [
                              {
                                  "lang": "FR-CA",
                                  "type": "LBody",
                                  "mcids": [9, 11],
                                  "children": [
                                      {"lang": "FR-FR", "type": "Span", "mcids": [10]}
                                  ],
                              }
                          ],
                      }
                  ],
              },
              {"lang": "FR-CA", "type": "P", "mcids": [14]},
              {"lang": "FR-CA", "type": "P", "mcids": [15]},
              {"lang": "FR-CA", "type": "P", "mcids": [16]},
              {"lang": "FR-FR", "type": "P", "mcids": [17]},
              {"lang": "FR-FR", "type": "P", "mcids": [18]},
              {"lang": "FR-FR", "type": "P", "mcids": [19]},
          ],
      }
  ]
  
  PVSTRUCT2 = [
      {
          "type": "Sect",
          "children": [
              {"type": "P", "lang": "FR-CA", "page_number": 2, "mcids": [0]},
              {"type": "P", "lang": "FR-CA", "page_number": 2, "mcids": [1]},
              {"type": "P", "lang": "FR-CA", "page_number": 2, "mcids": [2]},
              {"type": "P", "lang": "FR-CA", "page_number": 2, "mcids": [3]},
              {"type": "P", "lang": "FR-CA", "page_number": 2, "mcids": [4]},
              {"type": "P", "lang": "FR-CA", "page_number": 2, "mcids": [5]},
              {"type": "P", "lang": "FR-CA", "page_number": 2, "mcids": [6]},
              {
                  "type": "L",
                  "children": [
                      {
                          "type": "LI",
                          "children": [
                              {
                                  "type": "LBody",
                                  "lang": "FR-CA",
                                  "page_number": 2,
                                  "mcids": [9, 11],
                                  "children": [
                                      {
                                          "type": "Span",
                                          "lang": "FR-FR",
                                          "page_number": 2,
                                          "mcids": [10],
                                      }
                                  ],
                              }
                          ],
                      }
                  ],
              },
              {"type": "P", "lang": "FR-CA", "page_number": 2, "mcids": [14]},
              {"type": "P", "lang": "FR-CA", "page_number": 2, "mcids": [15]},
              {"type": "P", "lang": "FR-CA", "page_number": 2, "mcids": [16]},
              {"type": "P", "lang": "FR-FR", "page_number": 2, "mcids": [17]},
              {"type": "P", "lang": "FR-FR", "page_number": 2, "mcids": [18]},
              {"type": "P", "lang": "FR-FR", "page_number": 2, "mcids": [19]},
          ],
      }
  ]
  
  
  IMAGESTRUCT = [
      {
          "type": "Document",
          "children": [
              {"type": "P", "mcids": [0]},
              {"type": "P", "mcids": [1]},
              {
                  "type": "Figure",
                  "alt_text": "pdfplumber on github\n\n"
                  "a screen capture of the github page for pdfplumber",
                  "mcids": [2],
              },
          ],
      }
  ]
  
  
  WORD365 = [
      {
          "type": "Document",
          "children": [
              {
                  "type": "H1",
                  "children": [
                      {"type": "Span", "mcids": [0]},
                      {"type": "Span", "actual_text": " ", "mcids": [1]},
                  ],
              },
              {"type": "P", "mcids": [2]},
              {
                  "type": "L",
                  "attributes": {"O": "List", "ListNumbering": "Disc"},
                  "children": [
                      {"type": "LI", "children": [{"type": "LBody", "mcids": [3]}]},
                      {"type": "LI", "children": [{"type": "LBody", "mcids": [4]}]},
                      {"type": "LI", "children": [{"type": "LBody", "mcids": [5]}]},
                  ],
              },
              {"type": "P", "mcids": [6]},
              {
                  "type": "L",
                  "attributes": {"O": "List", "ListNumbering": "Decimal"},
                  "children": [
                      {"type": "LI", "children": [{"type": "LBody", "mcids": [7]}]},
                      {"type": "LI", "children": [{"type": "LBody", "mcids": [8]}]},
                  ],
              },
              {
                  "type": "Table",
                  "children": [
                      {
                          "type": "THead",
                          "children": [
                              {
                                  "type": "TR",
                                  "children": [
                                      {
                                          "type": "TH",
                                          "children": [{"type": "P", "mcids": [9, 10]}],
                                      },
                                      {
                                          "type": "TH",
                                          "children": [{"type": "P", "mcids": [11, 12]}],
                                      },
                                      {
                                          "type": "TH",
                                          "children": [{"type": "P", "mcids": [13, 14]}],
                                      },
                                  ],
                              }
                          ],
                      },
                      {
                          "type": "TBody",
                          "children": [
                              {
                                  "type": "TR",
                                  "children": [
                                      {
                                          "type": "TD",
                                          "children": [{"type": "P", "mcids": [15, 16]}],
                                      },
                                      {
                                          "type": "TD",
                                          "children": [{"type": "P", "mcids": [17, 18]}],
                                      },
                                      {
                                          "type": "TD",
                                          "children": [{"type": "P", "mcids": [19, 20]}],
                                      },
                                  ],
                              },
                              {
                                  "type": "TR",
                                  "children": [
                                      {
                                          "type": "TD",
                                          "children": [{"type": "P", "mcids": [21, 22]}],
                                      },
                                      {
                                          "type": "TD",
                                          "children": [{"type": "P", "mcids": [23, 24]}],
                                      },
                                      {
                                          "type": "TD",
                                          "children": [{"type": "P", "mcids": [25, 26]}],
                                      },
                                  ],
                              },
                          ],
                      },
                  ],
              },
              {"type": "P", "mcids": [27]},
          ],
      }
  ]
  
  
  SCOTUS = [
      {
          "type": "Div",
          "children": [
              {
                  "type": "P",
                  "page_number": 1,
                  "attributes": {
                      "LineHeight": 25.75,
                      "TextIndent": 21.625,
                      "O": "Layout",
                  },
                  "mcids": [1],
              },
              {
                  "type": "P",
                  "page_number": 1,
                  "attributes": {
                      "LineHeight": 25.75,
                      "StartIndent": 86.375,
                      "O": "Layout",
                  },
                  "mcids": [2],
              },
              {
                  "type": "P",
                  "page_number": 1,
                  "attributes": {
                      "LineHeight": 25.75,
                      "TextIndent": 50.375,
                      "O": "Layout",
                  },
                  "mcids": [3, 4],
              },
              {
                  "type": "P",
                  "page_number": 1,
                  # This is important, it has attributes and a class
                  "attributes": {
                      "LineHeight": 25.75,
                      "StartIndent": 165.625,
                      "EndIndent": 57.625,
                      "SpaceAfter": 24.5,
                      "O": "Layout",
                  },
                  "mcids": [5],
              },
              {
                  "type": "P",
                  "page_number": 1,
                  "attributes": {
                      "LineHeight": 25.75,
                      "TextIndent": 100.75,
                      "O": "Layout",
                  },
                  "mcids": [6],
              },
              {
                  "type": "P",
                  "page_number": 1,
                  # This is important, it has attributes and a class
                  "attributes": {
                      "LineHeight": 25.75,
                      "TextIndent": 21.625,
                      "EndIndent": 50.375,
                      "O": "Layout",
                      "TextAlign": "None",
                      "SpaceAfter": 179.125,
                  },
                  "mcids": [7],
              },
              {
                  "type": "P",
                  "page_number": 1,
                  # This is important, it has two attribute classes
                  "attributes": {"O": "Layout", "TextAlign": "Center", "SpaceAfter": 8.5},
                  "mcids": [8],
              },
              {
                  "type": "P",
                  "page_number": 1,
                  "attributes": {"O": "Layout", "TextAlign": "Center"},
                  "mcids": [9],
              },
          ],
      }
  ]
  
  
  HELLO = [
      {
          "type": "Section",
          "page_number": 1,
          "children": [
              {
                  "type": "P",
                  "page_number": 1,
                  "attributes": {"O": "Foo", "A1": 1},
                  "mcids": [1],
              },
              {
                  "type": "P",
                  "page_number": 2,
                  "attributes": {"O": "Foo", "A1": 2, "A2": 2},
                  "mcids": [1],
              },
          ],
      },
      {
          "type": "P",
          "revision": 1,
  