I would like to get some feedback on the below python code.

It works correctly when run, but I am concerned about efficiency as it's not the fastest to run and also about whether I should be using a with open statement.

Any feedback would be greatly appreciated as this is the first time I've written something like this.

import string  # To clean up strings extracted from images.
import cv2  # To read  JPG images for tesseract.
import pytesseract  # OCR to extract text from image read by OpenCV cv2.
import glob  # To iterate over all JPG files in a directory.
from datetime import datetime  # Handles dates, to parse dates from string and to return todays date.
import pandas  # To create dataframe from lists and then a csv from the dataframe.
from PIL import Image  # To convert image to PDF.
import os  # For renaming files.

# path to tesseract, this is necessary to avoid complicated installation.
pytesseract.pytesseract.tesseract_cmd = r"C:\Users\Anthony.Fox\AppData\Local\Programs\Tesseract-OCR\tesseract.exe"

# This lists are appended at each stage of file processing and then become the columns in the exported csv
csv_account_number = []
csv_doc_date = []
csv_file_type = []
csv_file_name = []
# Empty string variables to be amended at each stage of file processing before being appended to above lists.
file_type = ""
dates = ""
account_number = ""

pdf_counter = 1
# path the folder containing scanned images, this is also the output folder for pdfs for bot and csv for reporting
MYPATH = r"path_to_directory_containing_jpgs"

for file in glob.glob(MYPATH + "/*.jpg"):
    text = f"{pytesseract.image_to_string(cv2.imread(file))}"  # Extract text from image
    text1 = text.splitlines()  # Split text into lines
    image = Image.open(file).convert('RGB').save(f"{MYPATH}\\{pdf_counter}.pdf")  # Save a pdf version of the image
    if "Bill number" in text:
        file_type = "Bill"
    elif "Reminder" in text:
        file_type = "R2"
    elif "Your home will soon be powered by green energy" in text:
        file_type = "W4"
    elif "Your new home is powered by green energy" in text:
        file_type = "W5"
        file_type = "Letter"
    for line in text1:
        line_search = line.lower()
        if "account number" in line_search:
            # remove spaces and punctuation from line leaving account number
            account_number = line_search[16:].translate(str.maketrans('', '', string.punctuation))
            # print(account_number)
            # Break as the account numbers are at the top of the document we don't want to continue once the
            # account number has been found.
    for line in text1:
        if "Date" in line:
            # Remove spaces, words, and punctuation from text line, leaving just the date, which is then parsed.
            dates = str(datetime.strptime(line.split("Date")[-1].split(' ', 1)[-1].
                                          translate(str.maketrans('', '', string.punctuation)), '%d %B %Y'))[:-9]
            # print(dates)
            # Break as the date is at the top of the document we don't want to continue once the
            # date has been found.
    filename = f"{dates}_{file_type}_file_{pdf_counter}"
    os.rename(f"{MYPATH}\\{pdf_counter}.pdf", f"{MYPATH}\\{filename}.pdf")
    pdf_counter += 1

# Create a dataframe from the lists amended at each stage of file processing.
df = pandas.DataFrame(data={'Account Number': csv_account_number, 'Document Date': csv_doc_date,
                            'File Type': csv_file_type, 'File Name': csv_file_name})
# Create a csv in MYPATH from above data frame named with todays date.
df.to_csv(f"{MYPATH}\\File_Data_exported_{datetime.today().strftime('%d-%m-%Y')}.csv", sep=',', index=False)

# Prints to verify that each stage is working correctly.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.