Python ocr pdf extraction with multiple languages
<p>Hello i have a pdf file with 2 languages (English, Greek) and i want to extract it via python ocr. Until now i have this code but it works only for one language only (Greek)</p>

<p>How to run ocr extract in pdf file which has 2 languages? </p>

<pre><code>#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfpage import PDFPage
from pdfminer.converter import XMLConverter, HTMLConverter, TextConverter
from pdfminer.layout import LAParams
from cStringIO import StringIO
from wand.image import Image
from PIL import Image as PI
import pyocr
import pyocr.builders
import io

def pdfparser(data):
 tool = pyocr.get_available_tools()[0]
 for i in enumerate(tool.get_available_languages()):
    print(i)
 lang = tool.get_available_languages()[2]
 req_image = []
 final_text = []
 words = []
 image_pdf = Image(filename=data, resolution=600)
 image_jpeg = image_pdf.convert('jpeg')
 for img in image_jpeg.sequence:
    img_page = Image(image=img)
    req_image.append(img_page.make_blob('jpeg'))
 for img in req_image: 
    txt = tool.image_to_string(
        PI.open(io.BytesIO(img)),
        lang=lang,
        builder=pyocr.builders.TextBuilder()
    )
    final_text.append(txt)
    #words.extend(u'{}'.format(txt.split()))
#print(final_text)
#print(words)

for x in final_text:
    '''
    for i in x:
        print(i.replace('|(',u'Î').replace('|',u'Î'))
    '''

    try:
        word = x.encode('utf8')
        print(word)

    except UnicodeEncodeError , e:
        print(e)
        continue

if __name__ == '__main__':
 pdfparser(sys.argv[1]) 
</code></pre>

<p>I'm venturing an answer here. Try <code>lang = 'eng+ell'</code>. Make sure you have both <code>eng.traineddata</code> and <code>ell.traineddata</code> files are in your <code>tessdata</code> folder.</p>

<p><a href="https://github.com/tesseract-ocr/tessdata" rel="nofollow">https://github.com/tesseract-ocr/tessdata</a></p>

python extract ocr