{"id":3736,"date":"2025-04-19T07:13:49","date_gmt":"2025-04-19T07:13:49","guid":{"rendered":"https:\/\/alsaeeddev.com\/?p=3736"},"modified":"2025-04-19T07:21:16","modified_gmt":"2025-04-19T07:21:16","slug":"extract-the-text-from-image-using-python","status":"publish","type":"post","link":"https:\/\/alsaeeddev.com\/shop\/extract-the-text-from-image-using-python\/","title":{"rendered":"Extract The Text From Image Using Python"},"content":{"rendered":"<p>Discover how to easily Extract the text From image using python to pdf and word file. This guide shows you how to build a Python Tkinter application that extracts text from images and saves it into Word or PDF files effortlessly.<\/p>\n<h3>Introduction<\/h3>\n<p>Manually extracting text from images can be a tedious task. Thankfully, Python offers powerful libraries like <strong>pytesseract<\/strong> and <strong>Tkinter<\/strong> to automate this process. In this tutorial, you\u2019ll learn how to Extract the text From an image to pdf and word file by creating an easy-to-use desktop application. This project is perfect for beginners exploring GUI development and OCR (Optical Character Recognition).<\/p>\n<p>&nbsp;<\/p>\n<h3>Technologies Used<\/h3>\n<ul>\n<li><strong>Python<\/strong> &#8211; Programming language<\/li>\n<li><strong>Tkinter<\/strong> &#8211; GUI toolkit for Python<\/li>\n<li><strong>pytesseract<\/strong> &#8211; Optical Character Recognition (OCR) tool<\/li>\n<li><strong>Pillow<\/strong> &#8211; Image processing library<\/li>\n<li><strong>fpdf<\/strong> &#8211; PDF creation library<\/li>\n<li><strong>python-docx<\/strong> &#8211; Word document generation library<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h3>Installation Guide for Technologies Used<\/h3>\n<p>Before building the project to <strong>Extract the Text from an Image Using Python<\/strong>, make sure to install the following required libraries and tools.<\/p>\n<h5>1. Install Python<\/h5>\n<p>Make sure you have Python installed. You can download it from the official site: <a href=\"https:\/\/www.python.org\/downloads\/\" target=\"_blank\" rel=\"nofollow noopener\">Download Python<\/a>.<\/p>\n<h5>2. Install Required Python Libraries<\/h5>\n<pre><code># Install tkinter (already included with Python on most systems)\r\n# For Linux, you can install manually:\r\nsudo apt-get install python3-tk\r\n\r\n# Install pytesseract\r\npip install pytesseract\r\n\r\n# Install Pillow for image processing\r\npip install Pillow\r\n\r\n# Install fpdf to create PDF files\r\npip install fpdf\r\n\r\n# Install python-docx to create Word documents\r\npip install python-docx\r\n<\/code><\/pre>\n<h5>3. Install Tesseract-OCR Engine<\/h5>\n<p>Since <strong>pytesseract<\/strong> is only a wrapper, you also need to install the <strong>Tesseract-OCR engine<\/strong>:<\/p>\n<ul>\n<li><strong>Windows:<\/strong> Download from <a href=\"https:\/\/github.com\/tesseract-ocr\/tesseract\" target=\"_blank\" rel=\"nofollow noopener\">Tesseract GitHub<\/a> and install.<\/li>\n<li><strong>Linux (Ubuntu\/Debian):<\/strong> <code>sudo apt install tesseract-ocr<\/code><\/li>\n<li><strong>macOS (using Homebrew):<\/strong> <code>brew install tesseract<\/code><\/li>\n<\/ul>\n<h5>4. Set Tesseract Path in Python Code<\/h5>\n<pre><code>pytesseract.pytesseract.tesseract_cmd = r\"C:\\Program Files\\Tesseract-OCR\\tesseract.exe\"\r\n<\/code><\/pre>\n<p>Adjust the path if you installed Tesseract in a different location.<\/p>\n<h5>5. (Optional) Create a requirements.txt<\/h5>\n<p>To install all packages at once, you can create a <code>requirements.txt<\/code> file with the following content:<\/p>\n<pre><code>pytesseract\r\nPillow\r\nfpdf\r\npython-docx\r\n<\/code><\/pre>\n<p>Then run:<\/p>\n<pre><code>pip install -r requirements.txt\r\n<\/code><\/pre>\n<p>Now your Python environment is fully ready to build the project and extract text from images easily!<\/p>\n<h3>Project Overview: Extract the text From an image to pdf and word file<\/h3>\n<p>This simple Python application allows users to:<\/p>\n<ol>\n<li>Select an image (formats: PNG, JPG, BMP, TIFF)<\/li>\n<li>Extract text using OCR<\/li>\n<li>View extracted text in a scrollable text box<\/li>\n<li>Save extracted text as a Word (.docx) file<\/li>\n<li>Save extracted text as a PDF (.pdf) file<\/li>\n<\/ol>\n<h4>How It Works<\/h4>\n<h5>1. Selecting an Image<\/h5>\n<p>Click the &#8220;Select Image&#8221; button to open a file dialog where you can choose an image file. The application supports PNG, JPG, BMP, and TIFF formats.<\/p>\n<h5>2. Extracting Text<\/h5>\n<p>After selecting an image, click &#8220;Extract Text&#8221;. The app uses Tesseract OCR to scan the image and retrieve all readable text, which is displayed inside a scrollable text widget.<\/p>\n<h5>3. Saving as Word or PDF<\/h5>\n<p>Once the text is extracted, you can choose to save it as a Word document or a PDF file. The application ensures Unicode font support for multi-language text.<\/p>\n<h4>Key Code Snippets<\/h4>\n<hr \/>\n<h5>Selecting an Image File<\/h5>\n<pre><code>filepath = filedialog.askopenfilename(\r\n    filetypes=[(\"Image Files\", \"*.png *.jpg *.jpeg *.bmp *.tiff\")]\r\n)<\/code><\/pre>\n<h5>Extracting Text Using pytesseract<\/h5>\n<pre><code>img = Image.open(image_path)\r\ntext = pytesseract.image_to_string(img)<\/code><\/pre>\n<h5>Saving Text as a Word Document<\/h5>\n<pre><code>doc = Document()\r\ndoc.add_paragraph(text)\r\ndoc.save(filepath)<\/code><\/pre>\n<h5>Saving Text as a PDF Document<\/h5>\n<pre><code>pdf = FPDF()\r\npdf.add_page()\r\npdf.add_font('DejaVu', '', font_path, uni=True)\r\npdf.set_font('DejaVu', '', 12)\r\nfor line in text.split('\\n'):\r\n    pdf.multi_cell(0, 10, line)\r\npdf.output(filepath)<\/code><\/pre>\n<h5>Benefits of Automating Text Extraction<\/h5>\n<ul>\n<li>Saves time compared to manual typing<\/li>\n<li>Improves data entry accuracy<\/li>\n<li>Streamlines document management<\/li>\n<li>Boosts productivity for businesses and individuals<\/li>\n<\/ul>\n<hr \/>\n<h3>Complete Code extract-text.py<\/h3>\n<pre><code>import tkinter as tk\r\nfrom tkinter import filedialog, messagebox, ttk\r\nfrom tkinter.scrolledtext import ScrolledText\r\nfrom PIL import Image\r\nimport pytesseract\r\nfrom docx import Document\r\nfrom fpdf import FPDF\r\nimport os\r\nimport threading\r\n\r\n# Set path to tesseract.exe\r\npytesseract.pytesseract.tesseract_cmd = r\"C:\\Program Files\\Tesseract-OCR\\tesseract.exe\"  # Adjust if needed\r\n\r\ndef select_image():\r\n    filepath = filedialog.askopenfilename(\r\n        title=\"Select an image file\",\r\n        filetypes=[(\"Image Files\", \"*.png *.jpg *.jpeg *.bmp *.tiff\")]\r\n    )\r\n    if filepath:\r\n        entry_file.delete(0, tk.END)\r\n        entry_file.insert(0, filepath)\r\n\r\ndef extract_text_threaded():\r\n    threading.Thread(target=extract_text).start()\r\n\r\ndef extract_text():\r\n    image_path = entry_file.get()\r\n    if not os.path.exists(image_path):\r\n        messagebox.showerror(\"Error\", \"Image file does not exist!\")\r\n        return\r\n\r\n    try:\r\n        progress_bar.place(relx=0.5, rely=0.5, anchor=\"center\")  # Centered inside text box\r\n        progress_bar.start()\r\n\r\n        img = Image.open(image_path)\r\n        text = pytesseract.image_to_string(img)\r\n\r\n        if not text.strip():\r\n            messagebox.showinfo(\"No Text\", \"No text detected in the image.\")\r\n            return\r\n\r\n        text_box.config(state=tk.NORMAL)\r\n        text_box.delete(1.0, tk.END)\r\n        text_box.insert(tk.END, text)\r\n        text_box.config(state=tk.NORMAL)\r\n\r\n    except Exception as e:\r\n        messagebox.showerror(\"Error\", str(e))\r\n    finally:\r\n        progress_bar.stop()\r\n        progress_bar.place_forget()  # Hide progress bar\r\n\r\ndef save_as_word():\r\n    text = text_box.get(1.0, tk.END).strip()\r\n    if not text:\r\n        messagebox.showwarning(\"Warning\", \"No text to save!\")\r\n        return\r\n\r\n    filepath = filedialog.asksaveasfilename(\r\n        defaultextension=\".docx\",\r\n        filetypes=[(\"Word Document\", \"*.docx\")]\r\n    )\r\n    if filepath:\r\n        try:\r\n            doc = Document()\r\n            doc.add_paragraph(text)\r\n            doc.save(filepath)\r\n            messagebox.showinfo(\"Saved\", f\"Text saved as Word file:\\n{filepath}\")\r\n        except Exception as e:\r\n            messagebox.showerror(\"Error\", str(e))\r\n\r\ndef save_as_pdf():\r\n    text = text_box.get(1.0, tk.END).strip()\r\n    if not text:\r\n        messagebox.showwarning(\"Warning\", \"No text to save!\")\r\n        return\r\n\r\n    filepath = filedialog.asksaveasfilename(\r\n        defaultextension=\".pdf\",\r\n        filetypes=[(\"PDF file\", \"*.pdf\")]\r\n    )\r\n    if filepath:\r\n        try:\r\n            pdf = FPDF()\r\n            pdf.add_page()\r\n            \r\n            # Add a Unicode font (DejaVuSans.ttf must be in the same folder)\r\n            font_path = os.path.join(os.path.dirname(__file__), r\"D:\\Python\\extract-text\\DejavuSans.ttf\")\r\n            pdf.add_font('DejaVu', '', font_path, uni=True)\r\n            pdf.set_font('DejaVu', '', 12)\r\n            \r\n            for line in text.split('\\n'):\r\n                pdf.multi_cell(0, 10, line)\r\n\r\n            pdf.output(filepath)\r\n            messagebox.showinfo(\"Saved\", f\"Text saved as PDF file:\\n{filepath}\")\r\n        except Exception as e:\r\n            messagebox.showerror(\"Error\", str(e))\r\n\r\n\r\n# Helper function to get correct path for icon\r\ndef resource_path(relative_path):\r\n    try:\r\n        # PyInstaller creates a temp folder and stores path in _MEIPASS\r\n        base_path = sys._MEIPASS\r\n    except Exception:\r\n        base_path = os.path.abspath(\".\")\r\n    return os.path.join(base_path, relative_path)\r\n\r\n# --- GUI ---\r\nroot = tk.Tk()\r\nroot.title(\"Image to Text Extractor\")\r\nroot.minsize(400, 600)  # Minimum window size\r\n\r\nframe = tk.Frame(root, padx=10, pady=10)\r\nframe.pack(fill=tk.BOTH, expand=True)\r\n\r\n# Widgets\r\nbtn_select = tk.Button(frame, text=\"Select Image\", command=select_image)\r\nentry_file = tk.Entry(frame, width=50)\r\nbtn_extract = tk.Button(frame, text=\"Extract Text\", command=extract_text_threaded)\r\n\r\ntext_frame = tk.Frame(frame)\r\ntext_box = ScrolledText(text_frame, width=80, height=20, wrap=tk.WORD)\r\n\r\nprogress_bar = ttk.Progressbar(text_frame, orient=tk.HORIZONTAL, mode='indeterminate', length=200)\r\nprogress_bar.place_forget()\r\n\r\nbtn_save_word = tk.Button(frame, text=\"Save as Word\", command=save_as_word)\r\nbtn_save_pdf = tk.Button(frame, text=\"Save as PDF\", command=save_as_pdf)\r\n\r\n# Layout function (dynamic)\r\ndef layout_widgets(event=None):\r\n    width = root.winfo_width()\r\n\r\n    # Clear old layout\r\n    for widget in frame.winfo_children():\r\n        widget.grid_forget()\r\n        widget.pack_forget()\r\n\r\n    if width &lt; 500:  # Mobile Layout\r\n        btn_select.pack(pady=5, fill=tk.X)\r\n        entry_file.pack(pady=5, fill=tk.X)\r\n        btn_extract.pack(pady=5, fill=tk.X)\r\n\r\n        text_frame.pack(pady=10, fill=tk.BOTH, expand=True)\r\n        text_box.pack(fill=tk.BOTH, expand=True)\r\n\r\n        btn_save_word.pack(pady=5, fill=tk.X)\r\n        btn_save_pdf.pack(pady=5, fill=tk.X)\r\n\r\n    else:  # Desktop Layout\r\n        btn_select.grid(row=0, column=0, padx=5, pady=5)\r\n        entry_file.grid(row=0, column=1, padx=5, pady=5, sticky=\"ew\")\r\n        btn_extract.grid(row=0, column=2, padx=5, pady=5)\r\n\r\n        frame.grid_columnconfigure(1, weight=1)\r\n\r\n        text_frame.grid(row=1, column=0, columnspan=3, pady=10, sticky=\"nsew\")\r\n        text_box.pack(fill=tk.BOTH, expand=True)\r\n\r\n        btn_save_word.grid(row=2, column=0, pady=5)\r\n        btn_save_pdf.grid(row=2, column=2, pady=5)\r\n\r\n    # Make sure frame expands correctly\r\n    frame.pack(fill=tk.BOTH, expand=True)\r\n\r\n# Bind resize event\r\nroot.bind(\"\", layout_widgets)\r\n\r\n# Initialize layout\r\nlayout_widgets()\r\n\r\nroot.mainloop()\r\n<\/code><\/pre>\n<hr \/>\n<p><iframe loading=\"lazy\" title=\"Extract Text from Image Using Python | Build a GUI App with Tkinter and Pytesseract #shorts #python\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/igKNAtmiF3Y?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<hr \/>\n<p><a href=\"https:\/\/github.com\/alsaeeddev\/ExtractImageText\/archive\/refs\/heads\/main.zip\">Download Source Code<\/a><\/p>\n<h5>Conclusion<\/h5>\n<p>By completing this project, you now know how to Extract the text From an image to pdf and word file using a Python Tkinter GUI application. This hands-on project introduces you to OCR technology and file conversion, which are valuable skills for automating tasks and improving workflow efficiency. You can extend this app by adding features like multi-image processing or cloud uploads to make it even more powerful!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Discover how to easily Extract the text From image using python to pdf and word file. This guide shows you how to build a Python Tkinter application that extracts text from images and saves it into Word or PDF files effortlessly. Introduction Manually extracting text from images can be a tedious task. Thankfully, Python offers [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3739,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[94,164],"tags":[90,74,92,85,91,88,81,87,84,80,89,76,82,93,75,83,86,78,79,77],"class_list":["post-3736","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-source-codes","tag-beginner-python-project","tag-extract-text-from-image","tag-extract-text-from-images-python","tag-fpdf-python-library","tag-gui-python-project","tag-image-processing-with-python","tag-image-to-text-python","tag-ocr-with-python","tag-pillow-python-library","tag-pytesseract-python","tag-python-automation-project","tag-python-ocr","tag-python-pdf-generation","tag-python-programming","tag-python-text-extraction","tag-python-word-document-creation","tag-python-docx-tutorial","tag-save-text-as-pdf","tag-save-text-as-word-file","tag-tkinter-gui-app"],"_links":{"self":[{"href":"https:\/\/alsaeeddev.com\/shop\/wp-json\/wp\/v2\/posts\/3736","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alsaeeddev.com\/shop\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alsaeeddev.com\/shop\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alsaeeddev.com\/shop\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alsaeeddev.com\/shop\/wp-json\/wp\/v2\/comments?post=3736"}],"version-history":[{"count":17,"href":"https:\/\/alsaeeddev.com\/shop\/wp-json\/wp\/v2\/posts\/3736\/revisions"}],"predecessor-version":[{"id":4036,"href":"https:\/\/alsaeeddev.com\/shop\/wp-json\/wp\/v2\/posts\/3736\/revisions\/4036"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/alsaeeddev.com\/shop\/wp-json\/wp\/v2\/media\/3739"}],"wp:attachment":[{"href":"https:\/\/alsaeeddev.com\/shop\/wp-json\/wp\/v2\/media?parent=3736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alsaeeddev.com\/shop\/wp-json\/wp\/v2\/categories?post=3736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alsaeeddev.com\/shop\/wp-json\/wp\/v2\/tags?post=3736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}