将许多PDF合并为一个PDF

时间:2018-02-20 21:14:28

标签: python pdf

我正在尝试将4个PDF文档合并为1个PDF文档但我收到的类型错误表明我无法将'list'对象隐式转换为str。我该如何解决这个问题,下面我尝试的代码是否会将我的所有PDF合并为一个?

# 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()

1 个答案:

答案 0 :(得分:0)

在这一行

 merger.append(path + pdf_files)

你想要

 merger.append(path + files)