4

I am using PyPDF2 to read PDF files in python. While it works well for languages in English and European languages (with alphabets in english), the library fails to read Asian languages like Japanese and Chinese. I tried encode('utf-8'), decode('utf-8') but nothing seems to work. It just prints a blank string on extraction of the text.

I have tried other libraries like textract and PDFMiner but no success yet.

When I copy the text from PDF and paste it on a notebook, the characters turn into some random format text (probably in a different encoding).

def convert_pdf_to_text(filename):
    text = ''
    pdf = PyPDF2.PdfFileReader(open(filename, "rb"))
    if pdf.isEncrypted:
        pdf.decrypt('')
    for page in pdf.pages:
        text = text + page.extractText()
    return text

Can anyone point me in the right direction?

  • The code you show seems ok. Probably, there's either a problem with how the PDF encodes the text, or a bug in the library. There's an [open issue on Github](https://github.com/mstamy2/PyPDF2/issues/252) that might be related to your problem. – lenz Jun 22 '18 at 14:36
  • Can you provide a sample PDF? I can think of some possible causes but it's hard to say without more info. – polm23 Jun 25 '18 at 02:39
  • Here is the link to a PDF in Japanese - https://drive.google.com/open?id=1OUO4XnbTcWgQdWZe59QUqEOnKVNDEJqv – Nikunj Agarwal Jun 26 '18 at 16:01

1 Answers1

6

I too faced similar issue. I could resolve it by using 'tika-python' library.

import tika
tika.initVM()
from tika import parser
parsed = parser.from_file('fileName.pdf')
print(parsed["metadata"])
print(parsed["content"])

You can find more information about the library over here

krishna
  • 448
  • 1
  • 5
  • 13