Search code examples
pythonrefactoring

split method in sub methods with python


I have a little app that text reads from images in pdf.

And then filtering from the extracted text some subtext. like this:

apples_royal_gala = 'Appels Royal Gala 13kg 60/65 Generica PL Klasse I'
ananas_crownless = 'Ananas Crownless 14kg 10 Sweet CR Klasse I'
peen_waspeen = 'Peen Waspeen 14x1lkg 200-400 Generica BE Klasse I'

But I try now to refactor this long method in some sub methods.

So I try it like this:

import io
from tracemalloc import start
from PIL import Image
import pytesseract
from wand.image import Image as wi
import re

apples_royal_gala = 'Appels Royal Gala 13kg 60/65 Generica PL Klasse I'
ananas_crownless = 'Ananas Crownless 14kg 10 Sweet CR Klasse I'
peen_waspeen = 'Peen Waspeen 14x1lkg 200-400 Generica BE Klasse I'


def make_pattern(substr):
    return r"(?<=" + substr + r").*?(?P<number>[0-9,.]*)\n"


def reading_text_from_pdf():
    pdfFile = wi(filename="C:\\Users\\engel\\Documents\\python\\docs\\fixedPDF.pdf", resolution=300)
    text_factuur_verdi = []

    image = pdfFile.convert('jpeg')
    imageBlobs = []
    for img in image.sequence:
        imgPage = wi(image=img)
    imageBlobs.append(imgPage.make_blob('jpeg'))

    for imgBlob in imageBlobs:
        image = Image.open(io.BytesIO(imgBlob))
        text = pytesseract.image_to_string(image, lang='eng')
        text_factuur_verdi.append(text)
        extracting_text_from_pdf()


def extracting_text_from_pdf(text):
    substring_apples = re.findall(make_pattern(apples_royal_gala), text)
    substring_ananas = re.findall(make_pattern(ananas_crownless), text)
    substring_peen_waspeen = re.findall(make_pattern(peen_waspeen), text)

    print(substring_ananas[0]) if len(substring_ananas) > 0 else 'null'
    print(substring_apples[0]) if len(substring_apples) > 0 else 'null'
    print(substring_peen_waspeen[0]) if len(substring_peen_waspeen) > 0 else 'null'

oke,

I try it like this:


apples_royal_gala = 'Appels Royal Gala 13kg 60/65 Generica PL Klasse I'
ananas_crownless = 'Ananas Crownless 14kg 10 Sweet CR Klasse I'
peen_waspeen = 'Peen Waspeen 14x1lkg 200-400 Generica BE Klasse I'


def make_pattern(substr):
    return r"(?<=" + substr + r").*?(?P<number>[0-9,.]*)\n"
def reading_text_from_pdf():

    pdfFile = wi(
        filename="C:\\Users\\engel\\Documents\\python\\docs\\fixedPDF.pdf", resolution=300)
    text_factuur_verdi = []
    

    image = pdfFile.convert('jpeg')
    imageBlobs = []
    for img in image.sequence:
        imgPage = wi(image=img)
    imageBlobs.append(imgPage.make_blob('jpeg'))

    for imgBlob in imageBlobs:
        image = Image.open(io.BytesIO(imgBlob))
        text = pytesseract.image_to_string(image, lang='eng')
        text_factuur_verdi.append(text)
        extracting_sub_text_from_main_text(text)
      
        
def extracting_sub_text_from_main_text(text):    
    substring_apples = re.findall(make_pattern(apples_royal_gala), text)
    substring_ananas = re.findall(make_pattern(ananas_crownless), text)
    substring_peen_waspeen = re.findall(make_pattern(peen_waspeen), text)
    print(substring_ananas[0]) if len(substring_ananas) > 0 else 'null'
    print(substring_apples[0]) if len(substring_apples) > 0 else 'null'
    print(substring_peen_waspeen[0]) if len(substring_peen_waspeen) > 0 else 'null'
    
print(reading_text_from_pdf())

But ofcourse the method

extracting_sub_text_from_main_text

is none.

But how to pas the text tot the method?

Thank you

I had it like this:

import io
from tracemalloc import start
from PIL import Image
import pytesseract
from wand.image import Image as wi
import re


def make_pattern(substr):
    return r"(?<=" + substr + r").*?(?P<number>[0-9,.]*)\n"


pdfFile = wi(
    filename="C:\\Users\\engel\\Documents\\python\\docs\\fixedPDF.pdf", resolution=300)

text_factuur_verdi = []
apples_royal_gala = 'Appels Royal Gala 13kg 60/65 Generica PL Klasse I'
ananas_crownless = 'Ananas Crownless 14kg 10 Sweet CR Klasse I'
peen_waspeen = 'Peen Waspeen 14x1lkg 200-400 Generica BE Klasse I'


image = pdfFile.convert('jpeg')
imageBlobs = []

for img in image.sequence:
    imgPage = wi(image=img)
    imageBlobs.append(imgPage.make_blob('jpeg'))

    for imgBlob in imageBlobs:
        image = Image.open(io.BytesIO(imgBlob))
        text = pytesseract.image_to_string(image, lang='eng')
        text_factuur_verdi.append(text)
        substring_apples = re.findall(make_pattern(apples_royal_gala), text)
        substring_ananas = re.findall(make_pattern(ananas_crownless), text)
        substring_peen_waspeen = re.findall(make_pattern(peen_waspeen), text)

    print(substring_ananas[0]) if len(substring_ananas) > 0 else 'null'
    print(substring_apples[0]) if len(substring_apples) > 0 else 'null'
    print(substring_peen_waspeen[0]) if len(
        substring_peen_waspeen) > 0 else 'null'
    

And this works. But of course this has to be splitted in several methods.


Solution

  • I solved like this:

    
    pdfFile = wi(filename="C:\\Users\\engel\\Documents\\python\\docs\\fixedPDF.pdf", resolution=300)
    text_factuur_verdi = []
    apples_royal_gala = 'Appels Royal Gala 13kg 60/65 Generica PL Klasse I'
    ananas_crownless = 'Ananas Crownless 14kg 10 Sweet CR Klasse I'
    peen_waspeen = 'Peen Waspeen 14x1lkg 200-400 Generica BE Klasse I'
    
    def make_pattern(substr):
        return r"(?<=" + substr + r").*?(?P<number>[0-9,.]*)\n"
    
    
    def get_text_from_image(): 
        
        image = pdfFile.convert('jpeg')
       
        imageBlobs = []
        
        for img in image.sequence:
            imgPage = wi(image=img)
            imageBlobs.append(imgPage.make_blob('jpeg'))
    
        for imgBlob in imageBlobs:        
            image = Image.open(io.BytesIO(imgBlob))
            text = pytesseract.image_to_string(image, lang='eng')
            text_factuur_verdi.append(text)       
    
        return text_factuur_verdi
            
    def filterAnanas():
        get_text_from_image()        
        return re.findall(make_pattern(ananas_crownless), text_factuur_verdi[0])
    
    
    if ananas_crownless: print(filterAnanas())