Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyPDF2 extract empty Text: Python3

Tags:

python

pypdf

I am using PyPDF2 for extract text from pdf. All examples which I found in the google look like my code:

import PyPDF2

reader = PyPDF2.PdfFileReader("test2.pdf")
page = reader.getPage(0)
text = page.extractText()
print(text.encode("utf-8"))

However, I have empty text in my console:

b''

This code I have tested for different pdf and all pdf's were empty

UPD:

# getDocumentInfo
{'/Producer': 'Skia/PDF m75'}

File pdf

like image 737
nesalexy Avatar asked Feb 04 '23 19:02

nesalexy


1 Answers

It looks like some font/text combos make the text unreadable by PyPDF2, PyPDF3 or PyPDF4.

To extract the text from these PDFs, you can use the dedicated PDF text extraction package pdfminer.six.

from pdfminer import high_level

local_pdf_filename = "/path/to/pdf/you_want_to_extract_text_from.pdf"
pages = [0] # just the first page

extracted_text = high_level.extract_text(local_pdf_filename, "", pages)
print(extracted_text)

It works on all the pdfs that were failing for me and is super quick to implement as a fallback. Full docs for the extract_text function are here.

like image 102
timhj Avatar answered Feb 06 '23 09:02

timhj