Python 是當今世界最受歡迎的編程語言之一。我們可以用它來分析數據,但數據並不總是以所需的格式提供。在這種情況下,我們可以將文件的格式從 pdf、jpg 轉換為文本(。txt)格式,以便以更好的方式分析數據。有許多庫可用於執行此類任務。
我們可以使用 Python 的 PyPDF2 模塊來執行轉換。pdf 文件轉換為文本格式。使用該模塊時,我們面臨的主要缺點是編碼方案。PDF 文檔文件可以包含多種編碼,如 Unicode、ASCII、UTF-8 等。因此,將 PDF 文件轉換為文本可能會因為編碼模式而導致數據丟失。
在本教程中,我們將學習如何讀取 PDF 文件的內容並將其存儲在文本文件中。txt)格式,採用“OCR”方法。
首先,我們必須將 PDF 文檔文件的頁面轉換成圖像,然後,我們將使用 OCR 從圖像中讀取內容並將其存儲在文本中()。txt)格式文件。
所需模塊:
在本教程中,我們必須使用給定的命令安裝以下模塊:
- 電池:
!pip3 install PIL
- 中小企業:-
!pip3 install pytesseract
- pdf2image: –
!pip3 install pdf2image
- 宇宙魔方-ocr: –
!pip3 install tesseract-ocr
(為此用戶應該擁有微軟 Visual C++ 14.0,使用“Visual Studio 構建工具”獲取:https://visualstudio.microsoft.com/downloads/)
第一部分:
第一部分將處理轉換我的 PDF 頁面為圖像文件。PDF 文件的每一頁都將存儲為圖像文件,圖像的名稱將存儲為:
PDF page no. 1: page_no_1.jpg
PDF page no. 2: page_no_2.jpg
PDF page no. 3: page_no_3.jpg
PDF page no. 4: page_no_4.jpg
.
.
PDF page no. n: page_no_n.jpg
第二部分:
第二部分將處理從圖像文件中識別文本,將其分類到文本文件中。txt”格式。在這裡,我們將處理圖像文件,將其轉換為文本內容。一旦我們將文本作為字符串變量,我們就可以開始處理文本了。txt)文件。比如,在很多 PFD 文件中,我們可以看到,當一行完整,但最後一個字不能完全寫在同一行,那麼,在最後加一個連字符,這個字就延續到下一行。例如:
This is an example to show the above explanation of the wo-
rd which cannot be written entirely in the same line and is conti-
nued in the next line.
對於這樣的單詞,我們將做一個基本的預處理,將連字符和下面的行轉換成一個完整的單詞。當我們完成預處理後,這些文本將被分類到一個單獨的文本文件中。
代碼:
from PIL import Image as img
import pytesseract as PT
import sys
from pdf2image import convert_from_path as CFP
import os
# Importing the pdf file
PDF_file_1 = "exp.pdf"
pages_1 = CFP(PDF_file1, 9)
# Now, we will create a counter for storing images of each page of PDF to image
image_counter1 = 1
# Iterating through all the pages of the pdf file stored above
for page in pages_1:
# We will Declare the filename for each page of PDF file as JPG file
# For each page, the filename will be:
# PDF page no. 1: Page_no_1.jpg
# PDF page no.2: Page_no_2.jpg
# PDF page no. 3: Page_no_3.jpg
# PDF page no. 4: Page_no_4.jpg
# .... and so on..
# PDF page n: page_n.jpg
filename1 = "Page_no_" + str(image_counter) + " .jpg"
# Now, we will save the image of the page in system
page.save(filename1, 'JPEG')
# Then, we will increase the counter for updating filenames
image_counter1 = image_counter1 + 1
'''
Part #2 - Recognize the text content from the image files by using OCR
'''
# Variable for getting the count of the total number of pages
filelimit1 = image_counter1 - 1
# then, we will create a text file for writing the output
out_file1 = "output_text.txt"
# Now, we will open the output file in append mode so that all contents of the # images will be added in the same output file.
f_1 = open(out_file1, "a")
# Iterating from 1 to total number of pages
for K in range(1, filelimit1 + 1):
# Now, we will set filename for recognizing text from images
# Again, these files will be:
# Page_no_1.jpg
# Page_no_2.jpg
# Page_no_3.jpg
# ....
# page_no_n.jpg
filename1 = "Page_no_" + str(K) + " .jpg"
# Here, we will write a code for recognizing the text as a string variable in an image file by using the pytesserct module
text1 = str(((PT.image_to_string (Image.open (filename1)))))
# : The recognized text will be stored in variable text
# : Any string variable processing may be applied to text content
# : Here, basic formatting will be done:-
text1 = text1.replace('-\n', '')
# At last, we will write the processed text into the file.
f_1.write(text1)
# Closing the file after writing all the text content.
f_1.close()
輸出:
輸入 PDF 文件:
輸出文本文件:
我們可以看到,PDF 文件的頁面是如何轉換成圖像的。然後這些圖像被讀取,它們的內容被寫入文本文件。
使用 OCR 方法的優點:
- 用戶可以避免基於文本的轉換,這種轉換會因編碼方案而導致數據丟失。
- OCR 模塊還可以識別 PDF 文件中的手寫內容。
- 用戶還可以通過使用 OCR 模塊來修改僅識別 PDF 的特定頁面。
- 預處理可以大量完成,因為它以可變的形式獲得文本。
使用 OCR 方法的缺點:
- 磁盤存儲器用於在本地系統中存儲圖像文件。但是,這些文件佔用的空間很少。
- OCR 的使用不能保證 100%的準確性。而在計算機上創建的 PDF 文件文檔具有很高的準確性。
- OCR 模塊可以識別手寫內容,但準確性取決於許多因素,如頁面的顏色、筆跡的清晰度等。
結論
在本教程中,我們討論了如何使用 Python 中的 OCR 來讀取 PDF 文件的內容。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/239071.html