python extract data from pdf code example
Example 1: extract image from pdf python
import PyPDF2
from PIL import Image
if __name__ == '__main__':
input1 = PyPDF2.PdfFileReader(open("input.pdf", "rb"))
page0 = input1.getPage(0)
xObject = page0['/Resources']['/XObject'].getObject()
for obj in xObject:
if xObject[obj]['/Subtype'] == '/Image':
size = (xObject[obj]['/Width'], xObject[obj]['/Height'])
data = xObject[obj].getData()
if xObject[obj]['/ColorSpace'] == '/DeviceRGB':
mode = "RGB"
else:
mode = "P"
if xObject[obj]['/Filter'] == '/FlateDecode':
img = Image.frombytes(mode, size, data)
img.save(obj[1:] + ".png")
elif xObject[obj]['/Filter'] == '/DCTDecode':
img = open(obj[1:] + ".jpg", "wb")
img.write(data)
img.close()
elif xObject[obj]['/Filter'] == '/JPXDecode':
img = open(obj[1:] + ".jp2", "wb")
img.write(data)
img.close()
Example 2: extract pdf text with python
from tika import parser
raw = parser.from_file('yourfile.pdf')
print(raw['content'])
Example 3: python extract text from pdf
import pdfplumber
with pdfplumber.open(r'example.pdf') as pdf:
first_page = pdf.pages[0]
print(first_page.extract_text())
Example 4: pdf form data extract programmatically
string FirstNameFieldValue = DPL.GetFormFieldValueByTitle("FirstName");