Given a digitally created PDF file, I would like to extract the text with the coordinates. A bounding box would be awesome, but an anchor + font / font size would work as well.
I've created an example PDF document so that it's easy to try things out / share the result.
What I've tried
pdftotext
pdftotext PDF-export-example.pdf -layout
gives this output. It already contains the text, but the coordinates are not in there.
PyPDF2
PyPDF2 is even worse - also neither coordinates, nor font size and in this case not even ASCII art clues how the layout was:
from PyPDF2 import PdfFileReader
def text_extractor(path):
with open(path, "rb") as f:
pdf = PdfFileReader(f)
page = pdf.getPage(0)
text = page.extractText()
print(text)
if __name__ == "__main__":
path = "PDF-export-example.pdf"
text_extractor(path)
pdfminer.six
Another method to extract text, but without coordinates / font size. Thank you to Duck puncher for this one:
from io import StringIO
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfinterp import PDFPageInterpreter, PDFResourceManager
from pdfminer.pdfpage import PDFPage
def convert_pdf_to_txt(path):
rsrcmgr = PDFResourceManager()
retstr = StringIO()
codec = "utf-8"
laparams = LAParams()
device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
fp = open(path, "rb")
interpreter = PDFPageInterpreter(rsrcmgr, device)
password = ""
maxpages = 0
caching = True
pagenos = set()
for page in PDFPage.get_pages(
fp,
pagenos,
maxpages=maxpages,
password=password,
caching=caching,
check_extractable=True,
):
interpreter.process_page(page)
text = retstr.getvalue()
fp.close()
device.close()
retstr.close()
return text
if __name__ == "__main__":
print(convert_pdf_to_txt("PDF-export-example.pdf"))
This one goes a bit more in the right direction as it can give the font name and size. But the coordinates are still missing (and the output is a bit verbose as it is character-by-character):
from pdfminer.high_level import extract_pages
from pdfminer.layout import LTTextContainer, LTChar
for page_layout in extract_pages("PDF-export-example.pdf"):
for element in page_layout:
if isinstance(element, LTTextContainer):
for text_line in element:
for character in text_line:
if isinstance(character, LTChar):
print(character)
print(character.fontname)
print(character.size)
tabula-py
Here I don't get anything at all:
from tabula import read_pdf
df = read_pdf("PDF-export-example.pdf")
print(df)