Open In App

Check if a string exists in a PDF file in Python

In this article, we’ll learn how to use Python to determine whether a string is present in a PDF file. In Python, strings are essential for Projects, applications software, etc. Most of the time, we have to determine whether a string is present in a PDF file or not. Here, we’ll discuss how to check f a string exists in a PDF file in Python.

Here, we’ll talk about two approaches to fixing this issue. In, the First method we can determine whether a string exists or not directly from the PDF. Whereas, In the second method we will determine whether a string is present in a PDF file line by line.



Let’s say the following text is present in the PDF file:

Code.pdf

Check the string from the PDF document

We can directly check from the PDF if a string exists or not. We must first open the file and save its contents to the variable “f.” After that, read the file and put the data in the variable “a.” If the string is found, it will then print the outcome. Finally, the file will be closed. 






St = 'suraj'
f = open("file1.pdf", "r")
a = f.read()
  
if St in a:
    print('String \'', St, '\' Is Found In The PDF File')
else:
    print('String \'', St, '\' Not Found')
  
f.close()

Output: 

String ' geeksforgeeks ' Is Found In The PDF File 

Check the string from the PDF with its line number 

We can check line by line if a string exists in a PDF file or not. We first open a file and store the contents in the “f” variable. Set the line variable and counter both to zero. A for loop is then assigned to check it line by line. If the string is found, it will then print the outcome. Finally, the file will be closed.




St = 'suraj'
f = open("file1.pdf", "r")
  
c = 0
line = 0
  
for a in f:
    line = line + 1
  
    if St in a:
        c = 1
        break
  
if c == 0:
    print('String \'', St, '\' Not Found')
else:
    print('String \'', St, '\' Is Found In Line', line)
  
f.close()

Output:

String ' geeksforgeeks ' Is Found In Line 1 

Article Tags :