pdfplumber TypeError: must be str, not bytes

使用 pdfplumber  读取pdf文件时跑出异常,代码入

pdfplumber.open(file_path, password=password)

异常信息:

Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/python/python3/lib/python3.6/site-packages/pdfplumber/pdf.py", line 56, in open
    inst = cls(fp, **kwargs)
  File "/usr/local/python/python3/lib/python3.6/site-packages/pdfplumber/pdf.py", line 33, in __init__
    self.doc = PDFDocument(PDFParser(stream), password=password)
  File "/usr/local/python/python3/lib/python3.6/site-packages/pdfminer/pdfdocument.py", line 575, in __init__
    self._initialize_password(password)
  File "/usr/local/python/python3/lib/python3.6/site-packages/pdfminer/pdfdocument.py", line 599, in _initialize_password
    handler = factory(docid, param, password)
  File "/usr/local/python/python3/lib/python3.6/site-packages/pdfminer/pdfdocument.py", line 300, in __init__
    self.init()
  File "/usr/local/python/python3/lib/python3.6/site-packages/pdfminer/pdfdocument.py", line 307, in init
    self.init_key()
  File "/usr/local/python/python3/lib/python3.6/site-packages/pdfminer/pdfdocument.py", line 320, in init_key
    self.key = self.authenticate(self.password)
  File "/usr/local/python/python3/lib/python3.6/site-packages/pdfminer/pdfdocument.py", line 368, in authenticate
    key = self.authenticate_user_password(password)
  File "/usr/local/python/python3/lib/python3.6/site-packages/pdfminer/pdfdocument.py", line 374, in authenticate_user_password
    key = self.compute_encryption_key(password)
  File "/usr/local/python/python3/lib/python3.6/site-packages/pdfminer/pdfdocument.py", line 351, in compute_encryption_key
    password = (password + self.PASSWORD_PADDING)[:32]  # 1
TypeError: must be str, not bytes

刚开始一直以为是文件有问题。百度了一会无果。GitHub也有此类问题,但是说修复了,没看到方案,最后还是在博客园看到解决方案。

解决方案:

password = '234334'
pdfplumber.open(file_path, password=bytes(password,encoding='utf8'))

就是将密码字段变成一个 bytes 类型的传入。

原文链接:

https://www.cnblogs.com/moneymaster/p/14827867.html

你可能感兴趣的:(pdf,python,python,linux)