如何读取亚洲语言(中文,日文,泰文等)的PDF文件并以python字符串形式存储 [英] How to read PDF files which are in asian languages (Chinese, Japanese, Thai, etc.) and store in a string in python

查看:712
本文介绍了如何读取亚洲语言(中文,日文,泰文等)的PDF文件并以python字符串形式存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PyPDF2读取python中的PDF文件.尽管该库对英语和欧洲语言(英语字母)非常有效,但该图书馆无法读取日文和中文等亚洲语言.我尝试了encode('utf-8')decode('utf-8'),但似乎没有任何效果.它只是在提取文本时打印一个空白字符串.

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.

我尝试了其他库,例如textract和PDFMiner,但没有成功.

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

当我从PDF复制文本并将其粘贴到笔记本上时,字符变成一些随机格式的文本(可能采用不同的编码).

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?

推荐答案

我也遇到过类似的问题.我可以通过使用"tika-python"库来解决它.

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"])

您可以在此处

这篇关于如何读取亚洲语言(中文,日文,泰文等)的PDF文件并以python字符串形式存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆