GreenCoder90
GreenCoder90

Reputation: 363

Merging Many PDFs into One PDF

I am trying to merge 4 PDF documents into 1 PDF document but I am getting a type error which states that I Can't convert 'list' object to str implicitly. How can I fix this issue and will the code that I have attempted below merge all my PDFs into One?

# Script to generate a PDF report containing all PDFs
from PyPDF2 import PdfFileMerger
import os

path = 'H:\College Fourth Year\Development Project\Final Year Project 2018\Forensic Reports'

pdf_files = ['Call Log Data Report.pdf','Canonical Address Data Report.pdf', 'Sim Card Data Report.pdf', 'SMS Data Report.pdf']

merger = PdfFileMerger()

for files in pdf_files:
    merger.append(path + pdf_files)
if not os.path.exists(path + 'Full Report.pdf'):
    merger.write(path + 'Full Report.pdf')
merger.close()

# Stack Trace Error
Traceback (most recent call last):
  File "H:/College Fourth Year/Development Project/Final Year Project 2018/All_Data_Report.py", line 17, in <module>
    merger.append(path + files)
  File "C:\Python34\lib\site-packages\PyPDF2\merger.py", line 203, in append
    self.merge(len(self.pages), fileobj, bookmark, pages, import_bookmarks)
  File "C:\Python34\lib\site-packages\PyPDF2\merger.py", line 114, in merge
    fileobj = file(fileobj, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: 'H:\\College Fourth Year\\Development Project\\Final Year Project 2018\\Forensic ReportsCall Log Data Report.pdf'



# Updated Code
from PyPDF2 import PdfFileMerger
import os

path = r'H:\College Fourth Year\Development Project\Final Year Project 2018\Forensic Reports'

pdf_files = ['Call Log Data Report.pdf','Canonical Address Data Report.pdf', 'Sim Card Data Report.pdf', 'SMS Data Report.pdf']

merger = PdfFileMerger()

for files in pdf_files:
    PdfFileMerger.append(path + files)
if not os.path.exists(path + 'Full Report.pdf'):
    merger.write(path + 'Full Report.pdf')
merger.close()

Upvotes: 0

Views: 7569

Answers (1)

Lou Franco
Lou Franco

Reputation: 89162

On this line

 merger.append(path + pdf_files)

You wanted

 merger.append(path + files)

Upvotes: 1

Related Questions