Here's a copy-and-paste-ready example that lists the top-left corners of every block of text in a PDF, and which I think should work for any PDF that doesn't include "Form XObjects" that have text in them:
from pdfminer.layout import LAParams, LTTextBox
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfinterp import PDFResourceManager
from pdfminer.pdfinterp import PDFPageInterpreter
from pdfminer.converter import PDFPageAggregator
fp = open('yourpdf.pdf', 'rb')
rsrcmgr = PDFResourceManager()
laparams = LAParams()
device = PDFPageAggregator(rsrcmgr, laparams=laparams)
interpreter = PDFPageInterpreter(rsrcmgr, device)
pages = PDFPage.get_pages(fp)
for page in pages:
print('Processing next page...')
interpreter.process_page(page)
layout = device.get_result()
for lobj in layout:
if isinstance(lobj, LTTextBox):
x, y, text = lobj.bbox[0], lobj.bbox[3], lobj.get_text()
print('At %r is text: %s' % ((x, y), text))
The code above is based upon the Performing Layout Analysis example in the PDFMiner docs, plus the examples by pnj (https://mcmap.net/q/322126/-how-to-extract-text-and-text-coordinates-from-a-pdf-file) and Matt Swain (https://mcmap.net/q/324060/-how-does-one-obtain-the-location-of-text-in-a-pdf-with-pdfminer-duplicate). There are a couple of changes I've made from these previous examples:
- I use
PDFPage.get_pages()
, which is a shorthand for creating a document, checking it is_extractable
, and passing it to PDFPage.create_pages()
- I don't bother handling
LTFigure
s, since PDFMiner is currently incapable of cleanly handling text inside them anyway.
LAParams
lets you set some parameters that control how individual characters in the PDF get magically grouped into lines and textboxes by PDFMiner. If you're surprised that such grouping is a thing that needs to happen at all, it's justified in the pdf2txt docs:
In an actual PDF file, text portions might be split into several chunks in the middle of its running, depending on the authoring software. Therefore, text extraction needs to splice text chunks.
LAParams
's parameters are, like most of PDFMiner, undocumented, but you can see them in the source code or by calling help(LAParams)
at your Python shell. The meaning of some of the parameters is given at https://pdfminer-docs.readthedocs.io/pdfminer_index.html#pdf2txt-py since they can also be passed as arguments to pdf2text
at the command line.
The layout
object above is an LTPage
, which is an iterable of "layout objects". Each of these layout objects can be one of the following types...
LTTextBox
LTFigure
LTImage
LTLine
LTRect
... or their subclasses. (In particular, your textboxes will probably all be LTTextBoxHorizontal
s.)
More detail of the structure of an LTPage
is shown by this image from the docs:
Each of the types above has a .bbox
property that holds a (x0, y0, x1, y1) tuple containing the coordinates of the left, bottom, right, and top of the object respectively. The y-coordinates are given as the distance from the bottom of the page. If it's more convenient for you to work with the y-axis going from top to bottom instead, you can subtract them from the height of the page's .mediabox
:
x0, y0_orig, x1, y1_orig = some_lobj.bbox
y0 = page.mediabox[3] - y1_orig
y1 = page.mediabox[3] - y0_orig
In addition to a bbox
, LTTextBox
es also have a .get_text()
method, shown above, that returns their text content as a string. Note that each LTTextBox
is a collection of LTChar
s (characters explicitly drawn by the PDF, with a bbox
) and LTAnno
s (extra spaces that PDFMiner adds to the string representation of the text box's content based upon the characters being drawn a long way apart; these have no bbox
).
The code example at the beginning of this answer combined these two properties to show the coordinates of each block of text.
Finally, it's worth noting that, unlike the other Stack Overflow answers cited above, I don't bother recursing into LTFigure
s. Although LTFigure
s can contain text, PDFMiner doesn't seem capable of grouping that text into LTTextBox
es (you can try yourself on the example PDF from https://mcmap.net/q/324061/-is-there-a-text-string-variable-type-in-adobe-pdf-specification) and instead produces an LTFigure
that directly contains LTChar
objects. You could, in principle, figure out how to piece these together into a string, but PDFMiner (as of version 20181108) can't do it for you.
Hopefully, though, the PDFs you need to parse don't use Form XObjects with text in them, and so this caveat won't apply to you.