i was trying to open and read a pdf from php using python. but after executing the php code in my browser, its not redirecting to python script. i want my php code to execute the python script and return the output to php

php code:
<?php 
$command = escapeshellcmd("python filename.py");
$output = shell_exec($command);
echo $output;
?>

python code: this code will read the pdf from the directory and extracts required data from the pdf and gives the corresponding result as an output to the php code which is printed on the browser

import PyPDF2
import re,time
import sys
start_time =time.time()
# open the pdf file
address="pv3.pdf"
object = PyPDF2.PdfFileReader(address)
#print('hello')
# get number of pages
NumPages = object.getNumPages()

l = list()
# extract text and do the search
for i in range(0, NumPages):
    PageObj = object.getPage(i)

    Text = PageObj.extractText()
    l.append(Text)
#print(len(l))
#print(l[0])
d={}
count=0
for i in range(len(l)):
    x = l[i].split('\n')
    for j in range(len(x)):
        if re.search('[0-9]{8}',x[j]) and (j!=len(x)-1 and re.search('-',x[j+1])==None):
            #print(x[j],'in page',i+1,',line no',j+1)
            count +=1
            str = ''
            temp =0
            for m in range(j+1,len(x)):

                if '~na' not in x[m]:
                    str+=x[m]
                else:

                    temp = 1
                    break

            if temp==0:
                #print('hello')
                y=l[i+1].split('\n')
                for n in range(len(y)):

                    if '~' in y[n]:
                        break
                    str+=y[n]
           # if d[x[j]]==None:
            d[x[j]]=str


for x,y in d.items():

    print(x,"::::",y,"...,")
    print()
            #print(l[i])
#print(d)
#print('No of matches are',count)
#print(l[0])
end_time = time.time()

#print('Total time taken :',end_time-start_time)

0 Answers