{ "cells": [ { "cell_type": "code", "execution_count": 3, "id": "1da63fbc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Defaulting to user installation because normal site-packages is not writeableNote: you may need to restart the kernel to use updated packages.\n", "\n", "Collecting pdf2image\n", " Obtaining dependency information for pdf2image from https://files.pythonhosted.org/packages/62/33/61766ae033518957f877ab246f87ca30a85b778ebaad65b7f74fa7e52988/pdf2image-1.17.0-py3-none-any.whl.metadata\n", " Downloading pdf2image-1.17.0-py3-none-any.whl.metadata (6.2 kB)\n", "Requirement already satisfied: pillow in c:\\programdata\\anaconda3\\lib\\site-packages (from pdf2image) (9.4.0)\n", "Downloading pdf2image-1.17.0-py3-none-any.whl (11 kB)\n", "Installing collected packages: pdf2image\n", "Successfully installed pdf2image-1.17.0\n" ] }, { "ename": "ModuleNotFoundError", "evalue": "No module named 'pytesseract'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[3], line 5\u001b[0m\n\u001b[0;32m 1\u001b[0m get_ipython()\u001b[38;5;241m.\u001b[39mrun_line_magic(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mpip\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124minstall pdf2image\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m 4\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mpdf2image\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m convert_from_path\n\u001b[1;32m----> 5\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mpytesseract\u001b[39;00m\n\u001b[0;32m 7\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mextract_text_from_image_pdf\u001b[39m(pdf_file):\n\u001b[0;32m 8\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m 9\u001b[0m \u001b[38;5;124;03m Extract text from image-based PDF using OCR.\u001b[39;00m\n\u001b[0;32m 10\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n", "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'pytesseract'" ] } ], "source": [ "%pip install pdf2image\n", "\n", "\n", "from pdf2image import convert_from_path\n", "import pytesseract\n", "\n", "def extract_text_from_image_pdf(pdf_file):\n", " \"\"\"\n", " Extract text from image-based PDF using OCR.\n", " \"\"\"\n", " pages = convert_from_path(pdf_file)\n", " text = []\n", " for page in pages:\n", " text.append(pytesseract.image_to_string(page, lang='hin+ben'))\n", " return text\n", "\n", "def read_write_excel(pdf_file1, pdf_file2, output_file):\n", " \"\"\"\n", " Reads two PDF files, extracts their text, and writes their contents line by line\n", " into two separate columns of an Excel file.\n", "\n", " Args:\n", " pdf_file1 (str): Path to the first PDF file.\n", " pdf_file2 (str): Path to the second PDF file.\n", " output_file (str): Path to the output Excel file.\n", " \"\"\"\n", " # Extract text from PDF files\n", " text1 = extract_text_from_pdf(pdf_file1)\n", " text2 = extract_text_from_pdf(pdf_file2)\n", "\n", " # Open the Excel workbook\n", " workbook = openpyxl.Workbook()\n", " worksheet = workbook.active\n", "\n", " # Set column titles (optional)\n", " worksheet.cell(row=1, column=1).value = \"Hindi\"\n", " worksheet.cell(row=1, column=2).value = \"Bengali\"\n", "\n", " # Write data to Excel\n", " row = 2\n", " for line1, line2 in zip(text1, text2):\n", " worksheet.cell(row=row, column=1).value = line1.strip()\n", " worksheet.cell(row=row, column=2).value = line2.strip()\n", " row += 1\n", "\n", " # Save the workbook\n", " workbook.save(output_file)\n", " print(f\"Excel file created: {output_file}\")\n", "\n", "# Example usage\n", "pdf_file1 = \"atithi-hin.pdf\"\n", "pdf_file2 = \"atithi-ben.pdf\"\n", "output_file = \"Atithi-hi-bn.xlsx\"\n", "\n", "read_write_excel(pdf_file1, pdf_file2, output_file)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "d4bb9932", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "de6a63fe", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.11" } }, "nbformat": 4, "nbformat_minor": 5 }