python之pdf合并

有一个朋友向我提到了pdf合并的事情。在我印象中python可以处理pdf。于是故事就开始了。下面这是搬砖行为。
参考链接:Python之合并PDF文件

1. 输入文件

在这里插入图片描述

2. python代码

import os
from PyPDF2 import PdfFileReader, PdfFileWriter

# 参数       文件路径  输出文件名
def pdfUnit(filePath,outPdfName):
    totalFile=os.listdir(filePath)
    output = PdfFileWriter()
    outputPages=0
    for each in totalFile:
        eachFilePath=os.path.join(filePath, each)
        inputPdf = PdfFileReader(eachFilePath, "rb")
        # 获得源PDF文件中页面总数
        pageNumber = inputPdf.getNumPages()
        outputPages += pageNumber
        print("页数:%d"%pageNumber)
        # 分别将page添加到输出output中
        for index in range(pageNumber):
            output.addPage(inputPdf.getPage(index))
    outputStream = open(os.path.join(filePath, outPdfName), "wb")
    output.write(outputStream)
    outputStream.close()

filePath=r"C:\Users\xxx\Desktop\pdf"
pdfUnit(filePath,'total.pdf')

3. 输出结果

在这里插入图片描述
下一篇文章:python之pdf分页

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值