PDF Parsing Using Python - extracting formatted and plain texts

126,707

Solution 1

You can also take a look at PDFMiner (or for older versions of Python see PDFMiner and PDFMiner).

A particular feature of interest in PDFMiner is that you can control how it regroups text parts when extracting them. You do this by specifying the space between lines, words, characters, etc. So, maybe by tweaking this you can achieve what you want (that depends of the variability of your documents). PDFMiner can also give you the location of the text in the page, it can extract data by Object ID and other stuff. So dig in PDFMiner and be creative!

But your problem is really not an easy one to solve because, in a PDF, the text is not continuous, but made from a lot of small groups of characters positioned absolutely in the page. The focus of PDF is to keep the layout intact. It's not content oriented but presentation oriented.

Solution 2

That's a difficult problem to solve since visually similar PDFs may have a wildly differing structure depending on how they were produced. In the worst case the library would need to basically act like an OCR. On the other hand, the PDF may contain sufficient structure and metadata for easy removal of tables and figures, which the library can be tailored to take advantage of.

I'm pretty sure there are no open source tools which solve your problem for a wide variety of PDFs, but I remember having heard of commercial software claiming to do exactly what you ask for. I'm sure you'll run into them while googling.

Share:
126,707

Related videos on Youtube

Mike Cialowicz
Author by

Mike Cialowicz

Twitter Flickr Website

Updated on July 05, 2022

Comments

  • Mike Cialowicz
    Mike Cialowicz almost 2 years

    I'm looking for a PDF library which will allow me to extract the text from a PDF document. I've looked at PyPDF, and this can extract the text from a PDF document very nicely. The problem with this is that if there are tables in the document, the text in the tables is extracted in-line with the rest of the document text. This can be problematic because it produces sections of text that aren't useful and look garbled (for instance, lots of numbers mashed together).

    I'd like to extract the text from a PDF document, excluding any tables and special formatting. Is there a library out there that does this?

  • Mike Cialowicz
    Mike Cialowicz over 14 years
    PDFMiner looks interesting. I be able to use the XML output from it, and then parse that to ignore what I don't want. This still requires substantial post-processing, but for now it's probably the best solution. Thank you.
  • Sahil Mittal
    Sahil Mittal almost 10 years
    @Etienne, can this be used if the PDF has other language characters too?
  • Etienne
    Etienne almost 10 years
    It should work with other language characters. Docs mention: CJK languages and vertical writing scripts support. Best way to be sure, test it!
  • Ivan
    Ivan almost 8 years
    if you are looking for better performance and Py3 check this zderadicka.eu/parsing-pdf-for-fun-and-profit-indeed-in-pytho‌​n libpoppler with python binding is 10x faster then PDFminer
  • hamish
    hamish almost 7 years
    The pdfminer.six Python 3 port of PDFMiner is working well for me
  • fantabolous
    fantabolous over 4 years
    Starting from version 20191010, PDFMiner supports Python 3 only
  • Semnodime
    Semnodime over 3 years
    Warning: As of 2020, PDFMiner is not actively maintained. The code still works, but this project is largely dormant. For the active project, check out its fork pdfminer.six.
  • Etienne
    Etienne over 3 years
    @Semnodime Thanks, I updated the answer with the community maintained version.