3

I am using PyPDF2 to extract the data from PDF file and then converting into Text format?

PDF format for the file is like this:

Name : John 
Address: 123street , USA 
Phone No:  123456
Gender: Male 

Name : Jim 
Address:  456street , USA 
Phone No:  456899
Gender: Male 

In Python I am using this code:

import PyPDF2
pdf_file = open('C:\\Users\\Desktop\\Sampletest.pdf', 'rb')
read_pdf = PyPDF2.PdfFileReader(pdf_file)
number_of_pages = read_pdf.getNumPages()
page = read_pdf.getPage(0)
page_content = page.extractText()
page_content

This is the outcome which I get from page_content:

 'Name : John \n \nAddress: 123street , USA \n \nPhone No:  123456\n \nGender: Male \n \n \nName : Jim \n \nAddress:  456street , USA \n \nPhone No:  456899\n \nGender: Male \n \n \n'

How do I format it in a JSON or XML format so that I could use extracted data in SQL server database.

I tried using this approach as well

import json
data = json.dumps(page_content)
formatj = json.loads(data)
print (formatj)

Output:

Name : John 
Address: 123street , USA 
Phone No:  123456
Gender: Male 

Name : Jim 
Address:  456street , USA 
Phone No:  456899
Gender: Male 

This is the same output which I have in my word file, but I don't think that this is in JSON format.

2
  • Where are you running this code? In the REPL? Have you tried print(page_content) instead? Having one variable on a line by itself doesn't produce any output in a script Commented Oct 6, 2018 at 3:57
  • What have you tried so far?
    – Klaus D.
    Commented Oct 6, 2018 at 3:59

1 Answer 1

3

Not so pretty, but this would get the job done, I think. You would get a dictionary which then gets printed by the json parser in a nice, pretty format.

import json    

def get_data(page_content):
    _dict = {}
    page_content_list = page_content.splitlines()
    for line in page_content_list:
        if ':' not in line:
            continue
        key, value = line.split(':')
        _dict[key.strip()] = value.strip()
    return _dict

page_data = get_data(page_content)
json_data = json.dumps(page_data, indent=4)
print(json_data)

or, instead of those last 3 lines, just do this:

print(json.dumps(get_data(page_content), indent=4))
2
  • Thanks @UtahJarHead for the answer but it seems like I can only extract only one of the people information. In my case, I am getting this output. { "Name": "Jim", "Address": "456street , USA", "Phone No": "456899", "Gender": "Male" }
    – Avi
    Commented Oct 6, 2018 at 14:58
  • Right. The function I created, you are going to have to pick out each individual section. For instance, each time you see "Name:", you grab what's been gathered and shove it to the get_data() function, process it, and head over to get the next person's data. Commented Oct 6, 2018 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.