首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将多个PDF合并为一个PDF

将多个PDF合并为一个PDF
EN

Stack Overflow用户
提问于 2018-02-20 21:14:27
回答 1查看 7.3K关注 0票数 0

我试图将4个PDF文档合并成1个PDF文档,但是我得到了一个类型错误,它声明我不能将'list‘对象隐式转换为str。如何解决这个问题,以及我在下面尝试的代码是否会将我所有的PDF合并成一个?

代码语言:javascript
复制
# 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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-20 21:16:34

这条线上

代码语言:javascript
复制
 merger.append(path + pdf_files)

你想

代码语言:javascript
复制
 merger.append(path + files)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48894311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档