As we see, the pages of the PDF were converted to images. Then the images were read, and the content was written into a text file.
Advantages of this method include:
Avoiding text-based conversion because of encoding scheme resulting in loss of data.
Even handwritten content in PDF can be recognized due to the usage of OCR.
Recognizing only particular pages of the PDF is also possible.
Getting the text as a variable so that any amount of required pre-processing can be done.
Disadvantages of this method include:
Disk storage is used to store the images in the local system. Although these images are tiny in size.
Using OCR cannot guarantee 100% accuracy. Given a computer typed PDF document results in very high accuracy.
Handwritten PDFs are still recognized, but the accuracy depends on various factors like handwriting, page color, etc.
Part #1 deals with converting the PDF into image files. Each page of the PDF is stored as an image file. The names of the images stored are:
PDF page 1 -> [login to view URL]
PDF page 2 -> [login to view URL]
PDF page 3 -> [login to view URL]
PDF page n -> [login to view URL]
Part #2 deals with recognizing text from the image files and storing it into a text file. Here, we process the images and convert it into text. Once we have the text as a string variable, we can do any processing on the text. For example, in many PDFs, when a line is completed, but a particular word cannot be written entirely in the same line, a hyphen (‘-‘) is added, and the word is continued on the next line. For example –
This is some sample text but this parti-
cular word could not be written in the same line.
Now for such words, a fundamental pre-processing is done to convert the hyphen and the new line into a full word. After all the pre-processing is done, this text is stored in a separate text file.
To get the input PDF files used in the code, click [login to view URL]
Below is the implementation:
# Import libraries
from PIL import Image
from pdf2image import convert_from_path
# Path of the pdf
PDF_file = "[login to view URL]"
Part #1 : Converting PDF to images
# Store all the pages of the PDF in a variable
pages = convert_from_path(PDF_file, 500)
# Counter to store images of each page of PDF to image
image_counter = 1
# Iterate through all the pages stored above
for page in pages:
# Declaring filename for each page of PDF as JPG
# For each page, filename will be:
# PDF page 1 -> [login to view URL]
# PDF page 2 -> [login to view URL]
# PDF page 3 -> [login to view URL]
# PDF page n -> [login to view URL]
filename = "page_"+str(image_counter)+".jpg"
# Save the image of the page in system
[login to view URL](filename, 'JPEG')
# Increment the counter to update filename
image_counter = image_counter + 1
Part #2 - Recognizing text from the images using OCR
# Variable to get count of total number of pages
filelimit = image_counter-1
# Creating a text file to write the output
outfile = "[login to view URL]"
# Open the file in append mode so that
# All contents of all images are added to the same file
f = open(outfile, "a")
# Iterate from 1 to total number of pages
for i in range(1, filelimit + 1):
# Set filename to recognize text from
# Again, these files will be:
# [login to view URL]
# [login to view URL]
# [login to view URL]
filename = "page_"+str(i)+".jpg"
# Recognize the text as string in image using pytesserct
text = str(((pytesseract.image_to_string([login to view URL](filename)))))
# The recognized text is stored in variable text
# Any string processing may be applied on text
# Here, basic formatting has been done:
# In many PDFs, at line ending, if a word can't
# be written fully, a 'hyphen' is added.
# The rest of the word is written in the next line
# Eg: This is a sample text this word here GeeksF-
# orGeeks is half on .
33 freelancere byder i gennemsnit ₹19330 timen for dette job
PDF--WORD EXPERT -------I AM AVAILABLE RIGHT NOW-----100% ACCURACY I can do this work checked your whole description and attachments . Please knock me then I can do this Thanks
Good Day! I am Ilxam and I have read your requirements and already ready to start working. Just contact me and I will finish your project in a short time for cheap price
I have scanned many books and converted them to pdfs and separated them in chapters. If you want the perfect completion of the project. Contact me.
sir i am professional writer and also i have completed many types of projects like these on time and on affordable price and also my experience is good in this field
Hi sir, I'm prabath I live in Sri lanka I can do this job. I need your trust. I am very excited to do this project. I will do my best. Thank you!
hello, my name is Awais khan I am a dedicated and hard working person who believes in honesty and good working relation. though i am new it this sector of job but i have certain qualities which makes me good at this.
As this pandemic brings us to home so I have learnt to manage all things online which also include PDF and word file conversions so I feel am good at this . I will do the quality job and will complete the task on time.
Hi, I have read your requirements and I am very interested in this project. I have required skills and experience to do this for you at reasonable price. Please feel free to contact me. Regards, Sanket Dahane