I am trying to extract data from a PDF, but I keep getting a type error because my object is not iterable (on the statement for line in text:
but I don't understand why 'text' has no value, just above that I create the text object using text = page.extract.text()
and then I want to iterate through each line of the text to find matches to my regexes.
I'm afraid that my statement for line in text:
is the problem; perhaps using 'line' isn't appropriate, but I don't know what else to do.
My code is below, thanks for looking!
import requests
import pdfplumber
import pandas as pd
import re
from collections import namedtuple
Line = namedtuple('Line', 'gbloc_name contact_type email')
gbloc_re = re.compile(r'^(?:a.s[A-Z]{5}:s[A-Z]{4})')
line_re = re.compile(r'^[^@s]+@[^@s].[^@s]+$')
file = 'sampleReport.pdf'
lines=[]
with pdfplumber.open(file) as pdf:
pages = pdf.pages
for page in pdf.pages:
text = page.extract_text()
for line in text:
gbloc = gbloc_re.search(line)
if gbloc:
gbloc_name = gbloc
elif line.startswith('Outbound'):
contact_type = 'Outbound'
elif line.startswith('Tracing'):
contact_type = 'Tracing'
elif line.startswith('Customer'):
contact_type = 'Customer Service'
elif line.startswith('QA'):
contact_type = 'Quality Assurance'
elif line.startswith('NTS'):
contact_type = 'NTS'
elif line.startswith('Inbound'):
contact_type = 'Inbound'
elif line_re.search(line):
items = line.split()
lines.append(Line(gbloc_name, contact_type, *items))
question from:
https://stackoverflow.com/questions/65649324/nonetype-object-is-not-itreable-when-trying-to-extract-from-pdf 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…