How to read PDF files which are in asian languages (Chinese, Japanese, Thai, etc.) and store in a string in python

巧了我就是萌 提交于 2019-12-06 06:35:47

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/50985619/how-to-read-pdf-files-which-are-in-asian-languages-chinese-japanese-thai-etc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!