Python合并pdf方法

简介: Python合并pdf方法
  • 安装工具

    pip install PyPDF2
  • 使用代码

    from PyPDF2 import PdfFileWriter, PdfFileReader
    
    
    def get_filename(file_dir, download_type):
        """ 
        获取文件夹中所有指定后缀的文件 
        :param file_dir: 文件目录
        :param download_type: 文件类型
        """
    
        file_list = [os.path.join(dir_path, filespath) for dir_path, dir_names, dir_files in os.walk(file_dir) for filespath
                     in dir_files if str(filespath).endswith(download_type)]
        return file_list
    
    
    def merge_pdf(filepath, outfile):
        """
        合并指定目录下的所有pdf文件
        :param filepath: 需要合并的文件目录
        :param outfile: 合并后保存的pdf路径
        :return:
        """
        output = PdfFileWriter()
        output_pages = 0
        # 获取两次解压后的pdf文件
        pdf_file_name = get_filename(filepath, 'pdf')
        if pdf_file_name:
            for pdf_file in pdf_file_name:
                # 读取pdf文件
                pdf_data = PdfFileReader(open(pdf_file, "rb"))
                # 获得pdf的页面总数
                pdf_pages = pdf_data.getNumPages()
                output_pages += pdf_pages
                # 分别将page添加到输出output中
                for i in range(pdf_pages):
                    output.addPage(pdf_data.getPage(i))
            logger.info("合并后的总页数:%d" % output_pages)
            # 写入到目标pdf文件
            output_stream = open(os.path.join(filepath, outfile), "wb")
            output.write(output_stream)
            output_stream.close()
            return output_stream
        else:
            print("没有可以合并的pdf文件!")
相关文章
|
8天前
|
测试技术 API Python
【10月更文挑战第1天】python知识点100篇系列(13)-几种方法让你的电脑一直在工作
【10月更文挑战第1天】 本文介绍了如何通过Python自动操作鼠标或键盘使电脑保持活跃状态,避免自动息屏。提供了三种方法:1) 使用PyAutoGUI,通过安装pip工具并执行`pip install pyautogui`安装,利用`moveRel()`方法定时移动鼠标;2) 使用Pymouse,通过`pip install pyuserinput`安装,采用`move()`方法移动鼠标绝对位置;3) 使用PyKeyboard,同样需安装pyuserinput,模拟键盘操作。文中推荐使用PyAutoGUI,因其功能丰富且文档详尽。
|
5天前
|
机器学习/深度学习 数据采集 数据挖掘
11种经典时间序列预测方法:理论、Python实现与应用
本文将总结11种经典的时间序列预测方法,并提供它们在Python中的实现示例。
29 2
11种经典时间序列预测方法:理论、Python实现与应用
|
1天前
|
开发者 Python
Python中的魔法方法与运算符重载
在Python的奇妙世界里,魔法方法(Magic Methods)和运算符重载(Operator Overloading)是两个强大的特性,它们允许开发者以更自然、更直观的方式操作对象。本文将深入探讨这些概念,并通过实例展示如何利用它们来增强代码的可读性和表达力。
|
4天前
|
Java Apache Maven
将word文档转换成pdf文件方法
在Java中,将Word文档转换为PDF文件可采用多种方法:1) 使用Apache POI和iText库,适合处理基本转换需求;2) Aspose.Words for Java,提供更高级的功能和性能;3) 利用LibreOffice命令行工具,适用于需要开源解决方案的场景。每种方法都有其适用范围,可根据具体需求选择。
|
4天前
|
Java Apache Maven
Java将word文档转换成pdf文件的方法?
【10月更文挑战第13天】Java将word文档转换成pdf文件的方法?
12 1
|
14天前
|
Python
Python中的push方法详解与实例
Python中的push方法详解与实例
15 3
|
9天前
|
Linux Python
Python获得本机本地ip地址的方法
【10月更文挑战第8天】 socket模块包含了丰富的函数和方法,可以获取主机的ip地址,例如gethostbyname方法可以根据主机名获取ip地址,gethostbyname_ex方法可以获得本机所有ip地址列表,也可以使用netifaces模块获取网卡信息。
9 0
|
10天前
|
SQL 安全 数据库
Python防止SQL注入攻击的方法
Python防止SQL注入攻击的方法
20 0
|
11天前
|
Python
Python中tqdm模块的常用方法和示例
`tqdm` 是一个快速、可扩展的Python进度条库,适用于长循环中添加进度提示。通过封装迭代器 `tqdm(iterator)`,可以轻松实现进度显示。支持自定义描述、宽度及嵌套进度条,适用于多种迭代对象。在Jupyter notebook中,可自动调整显示效果。
19 0
|
11天前
|
Python
Python中threading模块的常用方法和示例
Python 的 `threading` 模块提供了多线程编程的能力,允许同时执行多个线程。主要类包括 `Thread`、`Lock` 和 `Condition`。`Thread` 类用于创建和管理线程,`Lock` 用于同步线程,防止资源竞争,`Condition` 用于线程间协调。本文介绍了这些类的常用方法及示例代码,帮助你更好地理解和使用多线程编程。
19 0