我使用PyPDF2从PDF文件中提取数据,然后转换为文本格式?
Name : John
Address: 123street , USA
Phone No: 123456
Gender: Male
Name : Jim
Address: 456street , USA
Phone No: 456899
Gender: Male
在Python中,我使用以下代码:
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
这是我从页面内容中得到的结果:
'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'
如何将其格式化为JSON或XML格式,以便在SQL server数据库中使用提取的数据。
import json
data = json.dumps(page_content)
formatj = json.loads(data)
print (formatj)
输出:
姓名:约翰
地址:美国123街
电话:123456
姓名:吉姆
地址:美国456街
性别:男
这与我在word文件中的输出相同,但我认为这不是JSON格式。