首页 > 解决方案 > 如何在 Python3 脚本中使用 PyPDF2 从 PDF 中读取阿拉伯文本

问题描述

我正在尝试编写一个从 PDF 中提取阿拉伯文本的脚本。

我尝试了不同的编码,例如 utf-8、utf-16 和 cp1256,但到目前为止没有运气。

我的代码是:

import PyPDF2, codecs

pdf_file = open('okt2020.pdf', 'rb')
reader = PyPDF2.PdfFileReader(pdf_file)
print(reader.numPages)
page = reader.getPage(0)

text = page.extractText()
pdf_file.close()

with open('win1256.txt','w',encoding='cp1256') as e:
    e.write(text)

with open('unicode.txt','w',encoding='utf-8-sig') as f:
    f.write(text)

以上两个输出文件都有奇怪的字符,例如:

!"#$%&#'(
 )*+,-+./
001%"#234
5467879!#':;;9<79
=8>>?9
!#'73>9<79
!%6(7879
@AB/ACD*E
FGFGHIJ*E
K+HL*E
H,M*E
N+HO*E
NHOD*E
HJP
Q!"#$%&'(&%)'&*!('*+!,'(&!,'-)!*./01234

有人可以指出我在这里做错了什么吗?

提前致谢!

标签: python-3.x

解决方案


推荐阅读