title: How to do OCR for PDF text extraction WHILE maintaining text structure (header/subtitle/body)
body: <p>I have been endlessly searching for a tool that can extract text from a PDF while maintaining structure. That is, given a text like this:</p>
  <hr />
  <h1>Title</h1>
  <p><em><strong>Subtitle1</strong></em></p>
  <p>Body1</p>
  <p><em><strong>Subtitle2</strong></em></p>
  <p>Body2</p>
  <hr />
  <p>OR</p>
  <hr />
  <h1>Title</h1>
  <p><em>Subtitle1</em>. Body1</p>
  <p><em>Subtitle2</em>. Body2</p>
  <hr />
  <p>I want a tool that can output a list of titles, subtitles and bodies. Or, if anybody knows how to do this, that would also be useful :)</p>
  <p>This would be easier if these 3 categories would be in the same format, but sometimes the subtitles can be bold, italic, underlined, or a random combination of the 3. Same for the titles. The problem with simple parsing from HTML/PDF/Docx is that these texts have no standard, and so quite often we can encounter sentences divided in several tags (in the case of HTML) and being a really hard to parse. As you can see, the subtitles are not always above a given paragraph or are sometimes in bullet points. So many possible combinations of formatting...</p>
  <p>So far I have encountered similar inquiries in <a href="https://stackoverflow.com/questions/45685911/python-text-detection-ocr">here</a> using Tesseract and <a href="https://stackoverflow.com/questions/24385714/detect-text-region-in-image-using-opencv">here</a> using OpenCV, yet none of them quite answer my question.</p>
  <p>I know that there are some machine learning tools to extract &quot;Table of Contents&quot; sections from scientific papers, but that also does not cut it.
  Does anyone know of a package/library, or if such thing has been implemented yet? Or does anyone know an approach to solve this problem, preferably in Python?</p>
  <p>Thank you!</p>
  <h2>Edit:</h2>
  <p>The documents I am refering to are 10-Ks from companies, such as this one <a href="https://www.sec.gov/Archives/edgar/data/789019/000119312516662209/d187868d10k.htm#tx187868_10" rel="nofollow noreferrer">https://www.sec.gov/Archives/edgar/data/789019/000119312516662209/d187868d10k.htm#tx187868_10</a>
  And say, I want to extract Item 7 in a programmatic and structured way as I mentioned above. But not all of them are standardized to do HTML parsing. (The PDF document is just this HTML saved as a PDF)</p>