python - 在 pypdf2 中使用 PdfFileMerger() 后的页数

标签 python pypdf

我正在尝试使用 PyPDF2 中的 PdfFileMerger() 来合并 pdf 文件(请参阅代码)。

from PyPDF2 import PdfFileMerger, PdfFileReader

[...]

merger = PdfFileMerger()

if (some condition):
    merger.append(PdfFileReader(file(filename1, 'rb')))
    merger.append(PdfFileReader(file(filename2, 'rb')))
if (test for non-zero file size):
    merger.write("output.pdf")

但是,我的合并命令受某些条件限制,可能不会生成合并的 pdf 文件。我想知道如何确定使用 PdfFileMerger() 执行合并后的页数。如果不出意外,我想知道页数是否非零。维护一个计数器来执行此操作会很麻烦,因为我正在跨多个函数执行合并,并且更喜欢更优雅的解决方案。

最佳答案

也许你可以尝试使用以下内容

if len(merger.pages) > 0

适合您的情况

if (test for non-zero file size)

关于python - 在 pypdf2 中使用 PdfFileMerger() 后的页数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39238648/

相关文章:

python - 在 python Flask 中上传后文件显示零 kb

python - 如何使用分发打包示例脚本?

python - 与python django中的虚拟环境混淆

python - 在 python 3 中解析 markdown 文件中的元素

python - 通过pypdf在pdf文件上读/写xmp元数据

python - K 最短路径 Python 不工作

python - 使用 Python 和 Pypdf2 从 pdf 中提取文本

sas - 如何使用 SAS 程序从 PDF 读取数据

Python 分节阅读 pdf

python - 无法迭代列表 -pyPDF2