python - "File format is not supported"同时多线程处理 pdf 文件列表

标签 python python-3.x python-multithreading python-camelot

我正在自学 Python 线程的基础知识,但遇到了困难。 我希望脚本将函数应用于 pdf 列表。该函数只是简单地计算每个 pdf 文件中表格的数量,然后返回每个文件有多少表格的组合列表。

现在,我收到一条错误消息,指出我的“文件格式不受支持”。据我所知,列表中的每个路径都是以 .pdf 结尾的完整路径。我不知道我做错了什么?

我已将代码精简到要点,并在下面包含了我的代码

import camelot
from multiprocessing.dummy import Pool as ThreadPool 
import glob
import os


#get a list of all the pdf paths in the directory I am interested in
pdfs = [os.path.abspath(x) for x in os.listdir(r'C:\Users\josiahh\Desktop\threading_learning')]

#format each path to have the r letter in front of it
rpdfs = ["r'" + pdf + "'" for pdf in pdfs]

#function that counts each table in the pdf. THIS IS WHERE SOMETHING IS WRONG...I THINK
listoflengths = []
def len_table5(filepath):    
    tables = camelot.read_pdf(filepath, pages = '1-end',flavor='stream')
    tablelength = len(tables)
    listoflengths.append(tablelength)

#threading code
pool = ThreadPool(5) 
results = pool.map(len_table5, rpdfs)
pool.close() 
pool.join() 

任何帮助将不胜感激。如果我可以澄清任何事情,请告诉我

编辑: 在文件名前面使用 r 时进行回溯

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-57-d38bdf75c567> in <module>
      1 
      2 pool = ThreadPool(5)
----> 3 results = pool.map(len_table5, rpdfs)
      4 pool.close()
      5 pool.join()

~\AppData\Local\Continuum\anaconda3\lib\multiprocessing\pool.py in map(self, func, iterable, chunksize)
    286         in a list that is returned.
    287         '''
--> 288         return self._map_async(func, iterable, mapstar, chunksize).get()
    289 
    290     def starmap(self, func, iterable, chunksize=None):

~\AppData\Local\Continuum\anaconda3\lib\multiprocessing\pool.py in get(self, timeout)
    668             return self._value
    669         else:
--> 670             raise self._value
    671 
    672     def _set(self, i, obj):

~\AppData\Local\Continuum\anaconda3\lib\multiprocessing\pool.py in worker(inqueue, outqueue, initializer, initargs, maxtasks, wrap_exception)
    117         job, i, func, args, kwds = task
    118         try:
--> 119             result = (True, func(*args, **kwds))
    120         except Exception as e:
    121             if wrap_exception and func is not _helper_reraises_exception:

~\AppData\Local\Continuum\anaconda3\lib\multiprocessing\pool.py in mapstar(args)
     42 
     43 def mapstar(args):
---> 44     return list(map(*args))
     45 
     46 def starmapstar(args):

<ipython-input-54-025080eb0d6f> in len_table5(filepath)
      1 listoflengths = []
      2 def len_table5(filepath):
----> 3     tables = camelot.read_pdf(filepath, pages = '1-end',flavor='stream')
      4     tablelength = len(tables)
      5     listoflengths.append(tablelength)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\camelot\io.py in read_pdf(filepath, pages, password, flavor, suppress_stdout, layout_kwargs, **kwargs)
    101 
    102         validate_input(kwargs, flavor=flavor)
--> 103         p = PDFHandler(filepath, pages=pages, password=password)
    104         kwargs = remove_extra(kwargs, flavor=flavor)
    105         tables = p.parse(flavor=flavor, suppress_stdout=suppress_stdout,

~\AppData\Local\Continuum\anaconda3\lib\site-packages\camelot\handlers.py in __init__(self, filepath, pages, password)
     33         self.filepath = filepath
     34         if not filepath.lower().endswith('.pdf'):
---> 35             raise NotImplementedError("File format not supported")
     36 
     37         if password is None:

NotImplementedError: File format not supported

文件路径中不使用 rs 时的回溯

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-59-07744a46a83f> in <module>
      1 
      2 pool = ThreadPool(5)
----> 3 results = pool.map(len_table5, pdfs)
      4 pool.close()
      5 pool.join()

~\AppData\Local\Continuum\anaconda3\lib\multiprocessing\pool.py in map(self, func, iterable, chunksize)
    286         in a list that is returned.
    287         '''
--> 288         return self._map_async(func, iterable, mapstar, chunksize).get()
    289 
    290     def starmap(self, func, iterable, chunksize=None):

~\AppData\Local\Continuum\anaconda3\lib\multiprocessing\pool.py in get(self, timeout)
    668             return self._value
    669         else:
--> 670             raise self._value
    671 
    672     def _set(self, i, obj):

~\AppData\Local\Continuum\anaconda3\lib\multiprocessing\pool.py in worker(inqueue, outqueue, initializer, initargs, maxtasks, wrap_exception)
    117         job, i, func, args, kwds = task
    118         try:
--> 119             result = (True, func(*args, **kwds))
    120         except Exception as e:
    121             if wrap_exception and func is not _helper_reraises_exception:

~\AppData\Local\Continuum\anaconda3\lib\multiprocessing\pool.py in mapstar(args)
     42 
     43 def mapstar(args):
---> 44     return list(map(*args))
     45 
     46 def starmapstar(args):

<ipython-input-58-e6499958826d> in len_table5(filepath)
     14 listoflengths = []
     15 def len_table5(filepath):
---> 16     tables = camelot.read_pdf(filepath, pages = '1-end',flavor='stream')
     17     tablelength = len(tables)
     18     listoflengths.append(tablelength)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\camelot\io.py in read_pdf(filepath, pages, password, flavor, suppress_stdout, layout_kwargs, **kwargs)
    101 
    102         validate_input(kwargs, flavor=flavor)
--> 103         p = PDFHandler(filepath, pages=pages, password=password)
    104         kwargs = remove_extra(kwargs, flavor=flavor)
    105         tables = p.parse(flavor=flavor, suppress_stdout=suppress_stdout,

~\AppData\Local\Continuum\anaconda3\lib\site-packages\camelot\handlers.py in __init__(self, filepath, pages, password)
     41             if sys.version_info[0] < 3:
     42                 self.password = self.password.encode('ascii')
---> 43         self.pages = self._get_pages(self.filepath, pages)
     44 
     45     def _get_pages(self, filepath, pages):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\camelot\handlers.py in _get_pages(self, filepath, pages)
     64             page_numbers.append({'start': 1, 'end': 1})
     65         else:
---> 66             infile = PdfFileReader(open(filepath, 'rb'), strict=False)
     67             if infile.isEncrypted:
     68                 infile.decrypt(self.password)

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\josiahh\\camelot - Copy (2) - Copy.pdf'

最佳答案

有一些事情需要考虑:

  1. 不要硬编码路径字符串,它不灵活而且最有可能 无法在不同的机器上运行。
  2. os.listdir() 仅返回文件夹中的文件名,因此 os.path.abspath() 不会为您提供正确的结果。
  3. 不确定是否要向文件名添加前缀,您真的需要这个吗?

更正后的版本为:

import os
from multiprocessing.dummy import Pool as ThreadPool

import camelot

BASE_PATH = os.path.dirname((os.path.abspath(__file__)))

FOLDER_PATH = os.path.join(BASE_PATH, "threading_learning")

pdfs = [os.path.join(FOLDER_PATH, file_name) for file_name in os.listdir(FOLDER_PATH)]

listoflengths = []


def len_table5(filepath):
    tables = camelot.read_pdf(filepath, pages='1-end', flavor='stream')
    tablelength = len(tables)
    listoflengths.append(tablelength)


# threading code
pool = ThreadPool(5)
results = pool.map(len_table5, pdfs)
pool.close()
pool.join()

print(listoflengths)

关于python - "File format is not supported"同时多线程处理 pdf 文件列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55093331/

相关文章:

python - python 3中导入函数的范围是什么

python - 如何从 Python 3 中的无限循环线程获取实时返回值

python-3.x - 线程和异步: Task was destroyed but it is pending

python - 如何用自定义 __len__() 过程替换类 @property 的 len(..) 返回值?

python - 在执行任何导入和/或其他语句之前用虚拟对象替换对象

django - 为 python3 安装 django 时出现 SSL 错误

python - 与子流程模块并发。我怎样才能做到这一点?

python - 在 python3 中使用 os.walk 时文件读取不正确

python - 匹配电话号码,正则表达式

python - Python 中的 Youtube 数据 API nextPageToken 循环