我试图从PDF中提取数据,但我一直收到一个类型错误,因为我的对象不可编辑(在语句for line in text:
上),但我不理解为什么“text”没有值,就在上面,我使用text = page.extract.text()
创建了text对象,然后我想遍历文本的每一行,以找到与正则表达式匹配的内容
我担心我的语句{
我的代码在下面,谢谢你的关注
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))
我使用lib
PyPDF2
从PDF中提取文本。在这里,我做了一个简单的源代码。 它将按页面提取内容图像结果:
如果您有任何问题,请检查并回复我
尝试将循环直接设置为与页面相等。extract_text()值。像这样:
相关问题 更多 >
编程相关推荐