3

我正在使用 PDFMiner 将 PDF 转换为文本。然后我想将它编码为UTF-8因为文本是希伯来语

这是 PDFMiner 的代码:

from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
from cStringIO import StringIO
from pdfminer.pdfparser import PDFParser


def convert_pdf_to_txt(path):
    rsrcmgr = PDFResourceManager()
    retstr = StringIO()
    codec = 'utf-8'
    laparams = LAParams()
    device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)

    fp = file(path, 'rb')

    parser = PDFParser(fp)
    doc = PDFDocument(parser)
    parser.set_document(doc)

    interpreter = PDFPageInterpreter(rsrcmgr, device)
    password = ""
    maxpages = 0
    caching = True
    pagenos=set()

    for page in PDFPage.get_pages(fp, pagenos, maxpages=maxpages,  password=password,caching=caching, check_extractable=True):
    interpreter.process_page(page)

    text = retstr.getvalue()

    fp.close()
    device.close()
    retstr.close()
    return text

现在当我尝试打印它时:

    elif file[-4:] == ".pdf":
    text = convert_pdf_to_txt("C:\Users\Vadim\Desktop\Python\New_cvs\\" + file)
    print text

它让我把文本倒过来,就像“rac”而不是“car”,但在希伯来语中。

我该如何纠正?

我尝试了元组切片,但它会反转邮件文本和电话号码文本,所以它不是一个选项。

''.join(reversed(myString))

也不是一个选择:(

4

0 回答 0