Ответ 1
Если вы используете linux или mac, вы можете использовать команду ps2ascii в вашем коде:
import os
input="someFile.pdf"
output="out.txt"
os.system(("ps2ascii %s %s") %( input , output))
Как извлечь текст из файла 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" % ˘ 20˛˛307% 4! ˘ "6 ˛ ˝ ˘ & 4" 9% 6% 4% 4 & 5˘2) ˘˘˛%: 6 (
Если вы используете linux или mac, вы можете использовать команду ps2ascii в вашем коде:
import os
input="someFile.pdf"
output="out.txt"
os.system(("ps2ascii %s %s") %( input , output))