使用 Python 解析 PDF-提取格式化和纯文本

我正在寻找一个 PDF 库,它将允许我提取的文本从 PDF 文档。我已经看过 PyPDF,它可以很好地从 PDF 文档中提取文本。这样做的问题是,如果文档中有表,那么表中的文本将与文档文本的其余部分一起提取。这可能是有问题的,因为它产生的文本部分没有用处,看起来乱七八糟(例如,许多数字混在一起)。

我想从 PDF 文档中提取文本,不包括任何表格和特殊格式。外面有图书馆能做这个吗?

127691 次浏览

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.

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.