繁体   English   中英

使用 PyPDF2 合并两个 pdf 文件时出错

[英]Error while merging two pdf files using PyPDF2

我为这个问题搜索了很多,但我没有找到这个问题的任何确切解决方案,这就是我问这个问题的原因......

这是我使用 PyPDF2 在 python 中合并两个 pdf 文件的代码:

import os
from PyPDF2 import PdfFileReader, PdfFileMerger

files_dir = "/Users/ajayvictor/"
pdf_files = [f for f in os.listdir(files_dir) if f.endswith("pdf")]
merger = PdfFileMerger()

for filename in pdf_files:
    merger.append(PdfFileReader(os.path.join(files_dir, filename), "rb"))

merger.write(os.path.join(files_dir, "merged_full.pdf"))

我在解释此代码时遇到的错误是:

Traceback (most recent call last):
  File "newtest.py", line 9, in <module>
    merger.append(PdfFileReader(os.path.join(files_dir, filename), "rb"))
  File "/Library/Python/2.7/site-packages/PyPDF2/merger.py", line 203, in append
    self.merge(len(self.pages), fileobj, bookmark, pages, import_bookmarks)
  File "/Library/Python/2.7/site-packages/PyPDF2/merger.py", line 151, in merge
    outline = pdfr.getOutlines()
  File "/Library/Python/2.7/site-packages/PyPDF2/pdf.py", line 1362, in getOutlines
    outline = self._buildOutline(node)
  File "/Library/Python/2.7/site-packages/PyPDF2/pdf.py", line 1449, in _buildOutline
    raise utils.PdfReadError("Unexpected destination %r" % dest)
PyPDF2.utils.PdfReadError: Unexpected destination '/__WKANCHOR_2'

我有一个替代方案..

import pyPdf

filenames=[]
path='/Users/ajayvictor/'
output_filename='merged1.pdf'

for i in range(12153602, 12153604):
    j=str(i)
    filenames.append(j + '.pdf')

output = pyPdf.PdfFileWriter()

for filename in filenames:
    input = pyPdf.PdfFileReader(file(path + filename.strip(), "rb"))
    for page in input.pages:
        output.addPage(page)

print(filenames)
outputstream = file(output_filename, "wb")
output.write(outputstream)
outputstream.close()

我使用了您的初始代码的变体,但我使用 pathlib 构建了路径并在范围内打开了最终的 pdf 文件。

from PyPDF2 import PdfFileMerger
from pathlib import Path

# prefer to use pathlib to build the path, you can change here
files_dir = (
    Path.home()
     / "Documents"
     / "tests"
     / "pdf_files"
    )

pdf_files = list(files_dir.glob("*.pdf"))

# optional, just to show that list() didn't sort
## pdf_files.sort()

pdf_merger = PdfFileMerger()

# get the pdf files path
for path in pdf_files:
    # just to debug
    ## print(path.name)
    pdf_merger.append(fileobj=str(path))

# create the final pdf
with Path("pdf_merged.pdf").open(mode="wb") as output_file:
    pdf_merger.write(output_file)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM