Why do I receive this error in Python PDFMiner: TypeError: can only concatenate str (not "bytes") to str

Viewed 500

I am new to python and trying to use PDFminer to convert pdf to txt file, and I get this error every time TypeError: can only concatenate str (not "bytes") to str*-

I am very confused because it seems like the error message is suggesting that the error is because of the files that are part of the pdfminer package? I know there are other questions here about this error message specifically but I couldn't figure out my problem based on them- probably mainly because I didn't know what their code was doing and I am a beginner but maybe also because it seems like my problem is due to the files associated with PDFminer specifically.

I am running this code:

from pdfminer.layout import LAParams
from pdfminer.converter import TextConverter
from io import StringIO
from pdfminer.pdfpage import PDFPage

def get_pdf_file_content(path_to_pdf):
    resource_manager = PDFResourceManager(caching=True)
    out_text = StringIO
    laParams = LAParams()
    text_converter = TextConverter(resource_manager, out_text, laparams= laParams)
    fp = open(path_to_pdf, 'rb')
    interpreter = PDFPageInterpreter(resource_manager, text_converter)
    for page in PDFPage.get_pages(fp, pagenos=set(), maxpages=0, password="", caching= True, check_extractable= True):
        interpreter.process_page(page)

    text = out_text.getvalue()

    fp.close()
    text_converter.close()
    out_text.close()

    return text

path_to_pdf = "C:\\files\\raw\\AZO - CALLSTREET REPORT  AutoZone, Inc.(AZO), Q1 2002 Earnings Call, 5-December-2001 10 00 AM ET - 05-Dec-01.pdf"
print(get_pdf_file_content(path_to_pdf))

I receive this error message:

  File "<stdin>", line 1, in <module>
  File "<stdin>", line 8, in get_pdf_file_content
  File "C:\text_analysis\project\lib\site-packages\pdfminer\pdfpage.py", line 122, in get_pages
    doc = PDFDocument(parser, password=password, caching=caching)
  File "C:\text_analysis\project\lib\site-packages\pdfminer\pdfdocument.py", line 575, in __init__
    self._initialize_password(password)
  File "C:\text_analysis\project\lib\site-packages\pdfminer\pdfdocument.py", line 599, in _initialize_password
    handler = factory(docid, param, password)
  File "C:\text_analysis\project\lib\site-packages\pdfminer\pdfdocument.py", line 300, in __init__
    self.init()
  File "C:\text_analysis\project\lib\site-packages\pdfminer\pdfdocument.py", line 307, in init
    self.init_key()
  File "C:\text_analysis\project\lib\site-packages\pdfminer\pdfdocument.py", line 320, in init_key
    self.key = self.authenticate(self.password)
  File "C:\text_analysis\project\lib\site-packages\pdfminer\pdfdocument.py", line 368, in authenticate
    key = self.authenticate_user_password(password)
  File "C:\text_analysis\project\lib\site-packages\pdfminer\pdfdocument.py", line 374, in authenticate_user_password
    key = self.compute_encryption_key(password)
  File "C:\text_analysis\project\lib\site-packages\pdfminer\pdfdocument.py", line 351, in compute_encryption_key
    password = (password + self.PASSWORD_PADDING)[:32]  # 1
TypeError: can only concatenate str (not "bytes") to str```

2 Answers

You have two choices here:

1) You can set the password as bytes and thus end up with

for page in PDFPage.get_pages(fp, pagenos=set(), maxpages=0, password=b"", caching= True, check_extractable= True):
        interpreter.process_page(page)

(note the b before the quotes defining your password)

2) You can get rid of that argument

The password argument isn't mandatory (it has a default value) you can thus get rid of it if you don't specifically need it. You will end up with :

for page in PDFPage.get_pages(fp, pagenos=set(), maxpages=0, caching= True, check_extractable= True):
        interpreter.process_page(page)

I was having this issue previously. I set the password as bytes and the data passed to the parser as bytes and it works for converting multiple PDFs to multiple txt files for me. Here's my code:

    def main():

        for path in Path(PDFS_FOLDER).glob("*.pdf"):
            with path.open("rb") as file:
                 parser = PDFParser(file)
                 document = PDFDocument(parser, b"")
                 if not document.is_extractable:
                    continue

                 manager = PDFResourceManager()
                 params = LAParams()

                 device = PDFPageAggregator(manager, laparams=params)
                 interpreter = PDFPageInterpreter(manager, device)
        
                 password =b""
                 text = ""

                 for page in PDFPage.create_pages(document):
                       interpreter.process_page(page)
                       for obj in device.get_result():
                           if isinstance(obj, LTTextBox) or isinstance(obj, LTTextLine):
                    text += obj.get_text()
             with open(TEXTS_FOLDER + "{}.txt".format(path.stem), "w") as file:
                 file.write(text)
         return 0


     if __name__ == "__main__":
         import sys
         sys.exit(main())
Related