Python 将多个PDF合并为一个PDF

Python 将多个PDF合并为一个PDF,python,pdf,Python,Pdf,我正在尝试将4个PDF文档合并为1个PDF文档,但我遇到一个类型错误,该错误表明我无法将“list”对象隐式转换为str。我如何解决这个问题?我在下面尝试的代码是否会将我的所有PDF合并到一个PDF中 # Script to generate a PDF report containing all PDFs from PyPDF2 import PdfFileMerger import os path = 'H:\College Fourth Year\Development Project\

我正在尝试将4个PDF文档合并为1个PDF文档,但我遇到一个类型错误,该错误表明我无法将“list”对象隐式转换为str。我如何解决这个问题?我在下面尝试的代码是否会将我的所有PDF合并到一个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()
#生成包含所有PDF的PDF报告的脚本
从PyPDF2导入Pdfilemerger
导入操作系统
path='H:\College foury Year\Development Project\definal 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']
合并=Pdfilemerger()
对于pdf_文件中的文件:
合并.append(路径+pdf_文件)
如果不存在os.path.exists(path+'Full Report.pdf'):
merge.write(路径+'Full Report.pdf')
合并.结束()
#堆栈跟踪错误
回溯(最近一次呼叫最后一次):
文件“H:/College 4th Year/Development Project/Final Year Project 2018/All_Data_Report.py”,第17行,在
合并.append(路径+文件)
文件“C:\Python34\lib\site packages\PyPDF2\merge.py”,第203行,在append中
self.merge(len(self.pages)、fileobj、书签、页面、导入书签)
文件“C:\Python34\lib\site packages\PyPDF2\merge.py”,第114行,在merge中
fileobj=file(fileobj,'rb')
FileNotFoundError:[Errno 2]没有这样的文件或目录:“H:\\College fourd Year\\Development Project\\Final Year Project 2018\\Forensic ReportsCall Log Data Report.pdf”
#更新代码
从PyPDF2导入Pdfilemerger
导入操作系统
path=r'H:\大学四年级\发展项目\ 2018年最后一年项目\法医报告'
pdf_files=['Call Log Data Report.pdf'、'Canonical Address Data Report.pdf'、'Sim Card Data Report.pdf'、'SMS Data Report.pdf']
合并=Pdfilemerger()
对于pdf_文件中的文件:
附加(路径+文件)
如果不存在os.path.exists(path+'Full Report.pdf'):
merge.write(路径+'Full Report.pdf')
合并.结束()
在这一行

 merger.append(path + pdf_files)
你想要

 merger.append(path + files)

道歉。我刚刚在错误中看到了“merge.append(path+pdf_文件)”。因此,这一行必须按照建议进行更改。我已将merge.append(path\pdf\u文件)更改为merge.append(path+文件),但我仍然遇到问题。完整的堆栈跟踪显示在上面。我现在得到一个错误,在PdfFileMerger.append(path+files)TypeError:append()缺少1个必需的位置参数:“fileobj”仔细查看堆栈跟踪的这一行:
FileNotFoundError:[Errno 2]没有这样的文件或目录:“H:\\College foury Year\\Development Project\\Final Year Project 2018\\法医报告scall Log Data Report.pdf”
。您所使用的文件名是
法医报告scall Log Data Report.pdf
,还是
Foresic Reports\Call Log Data Report.pdf
?在我看来,您缺少了一个
\
。我缺少了一个'\'。现在开始工作了。我生成了一个合并的PDF报告,每个表的数据都被分割成自己的页面。这正是我所需要的。谢谢你的帮助。