pdfplumber给出了fp.seek(pos)AttributeError:“dict”对象没有属性“seek”

2024-04-23 06:06:30 发布

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

这是我的代码:

def main():    
    import combinedparser as cp
    from tkinter.filedialog import askopenfilenames

    files = askopenfilenames()
    print(files) #this gives the right files as a list of strings composed of path+filename


    def file_discriminator(func):
        def wrapper():
            results = []
            for item in files:
                if item.endswith('.pdf'):
                    print(item + 'is pdf')
                    func = f1(file = item)
                    results.append(item, Specimen_Output)
                else:
                    print(item + 'is text')
                    func = f2(file = item)
                    results.append(item, Specimen_Output)

        return wrapper


    @file_discriminator
    def parse_me(**functions):
        print(results)


    parse_me(f1 = cp.advparser(), f2 = cp.vikparser())

main()

其中combinedparser.py有两个函数:

def advparser(**file):
    import pdfplumber
    with pdfplumber.open(file) as pdf:  # opened fname and assigned it to the variable pdf
        page = pdf.pages[0]  # assigned index 0 of pages to the variable page
        text = page.extract_words()
    #followed by a series of python operations generating a dict named Specimen_Output
def vikparser(**file):
    with open(file, mode = 'r') as filename:
        Specimen_Output = {}
    #followed by a series of python operations generating a dict named Specimen_Output 

我有一个目录,随机散布着pdf和文本文件。我试图使用decorator@file_discriminator来运行函数advparser,它使用pdfplumber和后续处理从目录中的pdf文件中提取可用信息;以及vikparser对文本文件执行常规文本文件处理。每一个都应该生成一个名为example_Output的字典。当advparser是作为advparser(file)运行的一个单独的.py文件时,我得到了正确的结果,导入了askopenfilename而不是其复数形式,并使用advparser(file=askopenfilename())调用;与vikparser类似(vikparser正在查看带有读线的文本文件)。但是,当我试图从主模块执行此操作并使用父函数调用它们时,我无法使其工作。我已经尝试了几乎所有可能的调用位置的排列,以及使用“file”的位置和关键字参数

当我修复因改变环境而产生的任何bug时,这是我遇到的最常见错误:

Traceback (most recent call last):


 File "<input>", line 1, in <module>
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/zachthomasadmin/PycharmProjects/pythonProject1/main.py", line 29, in <module>
    parse_me(f1 = cp.advparser(), f2 = cp.vikparser())
  File "/Users/zachthomasadmin/PycharmProjects/pythonProject1/combinedparser.py", line 12, in advparser
    with pdfplumber.open(file) as pdf:  # opened fname and assigned it to the variable pdf
  File "/Users/zachthomasadmin/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/pdfplumber/pdf.py", line 48, in open
    return cls(path_or_fp, **kwargs)
  File "/Users/zachthomasadmin/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/pdfplumber/pdf.py", line 25, in __init__
    self.doc = PDFDocument(PDFParser(stream), password=password)
  File "/Users/zachthomasadmin/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/pdfminer/pdfparser.py", line 39, in __init__
    PSStackParser.__init__(self, fp)
  File "/Users/zachthomasadmin/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/pdfminer/psparser.py", line 502, in __init__
    PSBaseParser.__init__(self, fp)
  File "/Users/zachthomasadmin/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/pdfminer/psparser.py", line 172, in __init__
    self.seek(0)
  File "/Users/zachthomasadmin/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/pdfminer/psparser.py", line 514, in seek
    PSBaseParser.seek(self, pos)
  File "/Users/zachthomasadmin/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/pdfminer/psparser.py", line 202, in seek
    self.fp.seek(pos)
AttributeError: 'dict' object has no attribute 'seek'

我做错了什么?它说的是什么dict对象,当我尝试从askopenfilename()单独调用每种类型时,为什么pdfplumber没有这个问题?我是一个新手,整天都在为这件事发愁。谢谢


Tags: inpyvenvpdflineitemusersfile
1条回答
网友
1楼 · 发布于 2024-04-23 06:06:30

问题是advparservikparser函数中的file参数实际上是命名参数的字典,因为它是用两个星号定义的。所以当你这样调用这些函数时

func = f1(file = item)

advparservikparser函数中的file参数实际上等于{"file": "some_filename.pdf"}

您需要解压缩您的参数:

def vikparser(**file):
    with open(file["file"], mode='r') as filename:
        pass

或者只在函数定义中使用单个file参数:

def vikparser(file):
    with open(file, mode='r') as filename:
        pass

相关问题 更多 >