OCR with Python: Turn Photos and Scanned Receipts into Text and Excel

Many Hong Kong SMEs, accountants and admin staff deal with a mountain of paper every day: customer receipts, supplier invoices, business cards, contracts and forms. To get that information into a computer, the traditional way is to sit there and type every character into Excel — ten documents is bearable, but a few hundred a month is slow and full of typos.

That job — turning text inside an image into computer text — is called OCR (Optical Character Recognition). With Python and a free OCR engine, a dozen lines of code read every image in a folder automatically and assemble them into a spreadsheet. Even if you have never written code, you can follow along.

What is OCR and why use it?

OCR tells the computer to "look at the image and read the words on it". You photograph a receipt or scan a document, and the program recognises the text and turns it into something you can copy and search. Compared with typing by hand, OCR is many times faster, never gets tired and does not mistype — perfect for large batches of similarly formatted documents.

Step 1: Install the tools

We will use pytesseract (the Python wrapper for the OCR engine), Pillow (to open images) and pandas (to export Excel). Install the Python side first in Command Prompt:

pip install pytesseract pillow pandas openpyxl

You also need the Tesseract engine itself. Download the Windows installer (the UB-Mannheim build), and during setup tick "Chinese (Traditional)" so it can read Chinese. Note the install path — usually C:\Program Files\Tesseract-OCR\.

Step 2: What does the script do?

  • Load the images — open every image in the folder and convert it to greyscale
  • Read the text — hand it to Tesseract to recognise the Chinese and English content
  • Write the output — save the results into an Excel sheet, one row per image

Step 3: Write the script

Create a new file called ocr.py (Notepad is fine), make a receipts folder beside it and drop your images in, then paste the code below:

import pytesseract from PIL import Image import pandas as pd import glob, os # Point to the Tesseract executable (adjust to your install path) pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe" rows = [] for path in glob.glob("receipts/*.jpg"): img = Image.open(path).convert("L") # greyscale boosts accuracy text = pytesseract.image_to_string(img, lang="chi_tra+eng") lines = [l.strip() for l in text.splitlines() if l.strip()] rows.append({"File": os.path.basename(path), "Content": " | ".join(lines)}) df = pd.DataFrame(rows) df.to_excel("ocr_results.xlsx", index=False) print("Done! Recognised", len(rows), "images")

Line by line

  • import pytesseract / from PIL import Image / import pandas as pd / import glob, os — loads five tools: the OCR engine, image reading, table handling, file finding and filename handling.
  • tesseract_cmd = ... — tells the program where Tesseract is installed; skip this line and you will usually get "tesseract is not installed or it's not in your PATH".
  • glob.glob("receipts/*.jpg") — grabs every jpg image in the folder in one go.
  • Image.open(path).convert("L") — opens the image and converts it to greyscale, cutting colour noise for better accuracy.
  • lang="chi_tra+eng" — recognises Traditional Chinese and English together; use "eng" alone for English only.
  • text.splitlines() — splits the result into lines and drops blanks for easier reading.
  • df.to_excel(...) — writes everything to Excel, one row per image.

Step 4: Run it

In Command Prompt, navigate to where you saved ocr.py and run:

python ocr.py

Done! An ocr_results.xlsx file appears in the same folder — open it and you will see the text recognised from each image, with no manual typing.

Five tips to boost accuracy

  • Shoot straight — photograph from directly above and avoid angles and glare; the straighter the text, the more accurate the result.
  • Go black and white with high contrast — scanned documents are usually more accurate than phone photos; if needed, apply binarisation (black-and-white) first.
  • Keep the resolution high — images that are too small cannot be read; aim for at least 1000 pixels on each side.
  • Pick the right language pack — use chi_tra for pure Chinese, and only add +eng for mixed content; adding the wrong one actually lowers accuracy.
  • Treat it as a draft, then review — amounts, stamps and handwriting are not always 100% correct, so important documents still need a human check.

Summary

Python Tutorial: With a dozen lines of code you hand the "type every character off the page" chore to the computer, turning receipts, invoices and business cards into Excel in one go. Remember OCR is not magic — the clearer the image and the more uniform the format, the higher the accuracy; pair it with a human review and you save a huge amount of time while cutting the risk of number-entry mistakes. Next, you can add regular expressions (Regex) to the script to pull out the amount and date fields automatically and produce even tidier reports.