• 技术文章 >Python技术 >Python基础教程

    如何用Python合并多个pdf 的文件夹

    2020-10-29 15:59:12原创2185
    当我们下载了需要学习的资料时,发现每篇都是一个独立的章节用起来很不方便,这时候我们可以用Python把它们合并,接下来就一起看看操作方法吧。

    一、文件图



    二、合并效果



    三、python代码


    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    <p style="line-height: 1.75em;"><span style="font-family: 微软雅黑, "Microsoft YaHei";"># -*- coding:utf-8*-

    import sys

    reload(sys)

    sys.setdefaultencoding('utf-8')

    import os

    import os.path

    from pyPdf import PdfFileReader,PdfFileWriter

    import time

    time1=time.time()

    # 使用os模块walk函数,搜索出某目录下的全部pdf文件

    ######################获取同一个文件夹下的所有PDF文件名#######################

    def getFileName(filepath):

      file_list = []

      for root,dirs,files in os.walk(filepath):

        for filespath in files:

          # print(os.path.join(root,filespath))

          file_list.append(os.path.join(root,filespath))

      return file_list

    ##########################合并同一个文件夹下所有PDF文件########################

    def MergePDF(filepath,outfile):

      output=PdfFileWriter()

      outputPages=0

      pdf_fileName=getFileName(filepath)

      for each in pdf_fileName:

        print each

        # 读取源pdf文件

        input = PdfFileReader(file(each, "rb"))

        # 如果pdf文件已经加密,必须首先解密才能使用pyPdf

        if input.isEncrypted == True:

          input.decrypt("map")

        # 获得源pdf文件中页面总数

        pageCount = input.getNumPages()

        outputPages += pageCount

        print pageCount

        # 分别将page添加到输出output中

        for iPage in range(0, pageCount):

          output.addPage(input.getPage(iPage))

      print "All Pages Number:"+str(outputPages)

      # 最后写pdf文件

      outputStream=file(filepath+outfile,"wb")

      output.write(outputStream)

      outputStream.close()

      print "finished"

    if __name__ == '__main__':

      file_dir = r'D:/course/'

      out=u"第一周.pdf"

      MergePDF(file_dir,out)

      time2 = time.time()

      print u'总共耗时:' + str(time2 - time1) + 's'<br></span></p>

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    <p style="line-height: 1.75em;"><span style="font-family: 微软雅黑, "Microsoft YaHei";">"D:\Program Files\Python27\python.exe" D:/PycharmProjects/learn2017/合并多个PDF文件.py

    D:/course/C1W1L01 Welcome.pdf

    3

    D:/course/C1W1L02 WhatIsNN.pdf

    4

    D:/course/C1W1L03 SupLearnWithNN.pdf

    4

    D:/course/C1W1L04 WhyIsDLTakingOff.pdf

    3

    D:/course/C1W1L05 AboutThisCourse.pdf

    3

    D:/course/C1W1L06 CourseResources.pdf

    3

    All Pages Number:20

    finished

    总共耗时:0.128000020981s

    Process finished with exit code 0<br></span></p>

    合并好的pdf是不是方便查阅了呢~更多Python学习推荐:PyThon学习网教学中心

    专题推荐:python合并多个pdf
    上一篇:看不懂pdf中的英文?就用Python 下一篇:python如何添加安装library库

    相关文章推荐

    • python中怎么将pdf内容显示出来?• pdf如何用python读取?• 进阶PDF,就用Python(pdfminer.six和pdfplumber模块)• Python如何读取pdf中的图片• Python教程:pdfplumber提取pdf中的表格数据• 如何用Python把pdf转换成word• Python中怎样把html网页转换成pdf• Python中用PyPDF2拆分pdf提取页面• 详解利用python提取pdf文本数字• 看不懂pdf中的英文?就用Python

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网