Как извлечь текст из PDF-файла в Python?
Как извлечь текст из PDF-файла в Python?
я попробовал следующий:
import sys
import pyPdf
def convertPdf2String(path):
content = ""
pdf = pyPdf.PdfFileReader(file(path, "rb"))
for i in range(0, pdf.getNumPages()):
content += pdf.getPage(i).extractText() + " n"
content = " ".join(content.replace(u"xa0", u" ").strip().split())
return content
f = open('a.txt','w+')
f.write(convertPdf2String(sys.argv[1]).encode("ascii","xmlcharrefreplace"))
f.close()
но результат следующий, а не читаемый текст:
728; ˇˆ ˇˇ!""ˇˆˆˆˆ ˆˆˆ ˆ"ˆ"ˆˆˆ#ˆˆ$%&ˆ ˆ'%ˆˇ ˆˆ'ˆ ˇˆ#$%&('%$&))$$+%#,-.+&&()))+,, -./012)()*+,-3ˆ/0245)6#57+82,55)6#57+,+2,+ /!#!!&1"%203ˆ07%4!"6 ˆ ˆ ˆ & /&4" 9% % 6ˇ%4%4&52)%:6(
1 ответов
Если вы используете linux или mac, вы можете использовать ps2ascii в коде:
import os
input="someFile.pdf"
output="out.txt"
os.system(("ps2ascii %s %s") %( input , output))