尝试从PDF提取时,“非类型对象不可编辑”

2024-10-04 01:30:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从PDF中提取数据,但我一直收到一个类型错误,因为我的对象不可编辑(在语句for line in text:上),但我不理解为什么“text”没有值,就在上面,我使用text = page.extract.text()创建了text对象,然后我想遍历文本的每一行,以找到与正则表达式匹配的内容

我担心我的语句{}是个问题;也许使用“line”不合适,但我不知道还能做什么

我的代码在下面,谢谢你的关注

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))

Tags: textnameinimportreforpdftype
2条回答

我使用libPyPDF2从PDF中提取文本。在这里,我做了一个简单的源代码。 它将按页面提取内容

import PyPDF2

with open('example.pdf', 'rb') as pdfFileObj:
    pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
    print(pdfReader.numPages)
    for i in range(0, pdfReader.numPages):
        print("Page: ", i)
        pageObj = pdfReader.getPage(i)
        print(pageObj.extractText())

图像结果:

Result

如果您有任何问题,请检查并回复我

尝试将循环直接设置为与页面相等。extract_text()值。像这样:

with pdfplumber.open(file) as pdf:
    for page in pdf.pages:
        for line in page.extract_text():

相关问题 更多 >