dissipate

 

import os, time
from PyPDF2 import PdfFileReader, PdfFileWriter


def get_file_list(path):
    file_list = [os.path.join(root, filepath)
                 for root, dirs, files in os.walk(path)
                 for filepath in files
                 if str(filepath).endswith(\'.pdf\')]
    return file_list if file_list else []


def merge_pdf(filepath, outfile):
    output = PdfFileWriter()
    output_pages = 0
    file_list = get_file_list(filepath)
    if file_list:
        for file in file_list:
            print(\'full path: %s\' % file)
            # 读取源pdf文件
            input = PdfFileReader(open(file, \'rb\'))

            # 获取源pdf文件页数
            page_count = input.getNumPages()
            output_pages += page_count
            print(\'page number: %d\' % page_count)

            # 将page添加至output
            for i in range(page_count):
                output.addPage(input.getPage(i))

        print(\'total pages: %d\' % output_pages)

        # 写入到目标pdf文件中
        output_stream = open(os.path.join(filepath, outfile), \'wb\')
        output.write(output_stream)
        output_stream.close()
        print(\'merge pdf done\')
    else:
        print(\'no pdf files\')


def main():
    commence = time.time()
    file_path = r\'e:\python\'  # 存放合并pdf的文件夹
    output_file = \'merged_pdf.pdf\'  # 合并后的pdf文件名
    merge_pdf(file_path, output_file)
    closure = time.time()
    print(\'elapsed %s seconds\' % (closure - commence))


main()

 

分类:

技术点:

相关文章:

  • 2021-10-22
  • 2021-12-06
  • 2021-12-06
  • 2021-12-28
  • 2022-12-23
  • 2022-12-23
  • 2021-10-28
  • 2022-12-23
猜你喜欢
  • 2021-09-05
  • 2021-12-09
  • 2021-11-14
  • 2022-01-14
  • 2021-11-30
  • 2022-12-23
相关资源
相似解决方案