PDF文件转换之输出指定页到新的 PDF 文件

背景

一份 PDF 学习资料需要打印其中某几页,文件有几百兆,看到 WPS 有PDF拆分功能,但是需要会员,开了一个月会员后完成了转换。突然想到,会员到期后如果还要拆解的话,怎么办呢?PDF 文件拆解功能,自己写一个呗。

输入需求

Python 实现对 PDF从截取某几页的功能

根据输出的方案,拷贝代码到 PyCharm 编辑器:

bash 复制代码
from PyPDF2 import PdfFileWriter, PdfFileReader

def extract_pages(input_pdf_path, output_pdf_path, start_page, end_page):
    """
    从PDF文件中截取指定页码范围的页面,并保存为新的PDF文件。
    
    :param input_pdf_path: 输入PDF文件路径
    :param output_pdf_path: 输出PDF文件路径
    :param start_page: 开始页码(从0开始)
    :param end_page: 结束页码(不包含该页)
    """
    # 创建PdfFileWriter对象
    output = PdfFileWriter()
    
    # 打开输入PDF文件
    with open(input_pdf_path, "rb") as input_pdf_file:
        pdf_file = PdfFileReader(input_pdf_file)
        pdf_pages_len = pdf_file.getNumPages()
        
        # 检查页码范围是否有效
        if start_page < 0 or end_page > pdf_pages_len or start_page >= end_page:
            raise ValueError("无效的页码范围")
        
        # 添加指定页码范围的页面到输出文件
        for i in range(start_page, end_page):
            output.addPage(pdf_file.getPage(i))
        
        # 保存输出文件
        with open(output_pdf_path, "wb") as output_pdf_file:
            output.write(output_pdf_file)

# 示例调用
input_pdf_path = "input.pdf"
output_pdf_path = "output.pdf"
start_page = 0  # 从第一页开始(页码从0开始)
end_page = 5    # 截止到第5页(不包含第5页)

extract_pages(input_pdf_path, output_pdf_path, start_page, end_page)

依赖安装及运行

第一步,执行命令pip install PyPDF2 完成依赖的安装。

第二步,修改 input_pdf_path 、output_pdf_path 及页码信息。

第三步,执行脚本,有报错:

  1. PdfFileWriter is deprecated and was removed in PyPDF2 3.0.0. Use PdfWriter instead.
  2. PdfFileReader is deprecated and was removed in PyPDF2 3.0.0. Use PdfReader instead.
  3. reader.getNumPages is deprecated and was removed in PyPDF2 3.0.0. Use len(reader.pages) instead.
  4. reader.getPage(pageNumber) is deprecated and was removed in PyPDF2 3.0.0. Use reader.pages[page_number] instead.
  5. addPage is deprecated and was removed in PyPDF2 3.0.0. Use add_page instead.

按照提示,修改废弃方法为最新方法后的代码如下:

bash 复制代码
from PyPDF2 import PdfWriter, PdfReader


def extract_pages(input_pdf_path, output_pdf_path, start_page, end_page):
    """
    从PDF文件中截取指定页码范围的页面,并保存为新的PDF文件。

    :param input_pdf_path: 输入PDF文件路径
    :param output_pdf_path: 输出PDF文件路径
    :param start_page: 开始页码(从0开始)
    :param end_page: 结束页码(不包含该页)
    """
    # 创建PdfWriter对象
    output = PdfWriter()

    # 打开输入PDF文件
    with open(input_pdf_path, "rb") as input_pdf_file:
        pdf_file = PdfReader(input_pdf_file)
        pdf_pages_len = len(pdf_file.pages)

        # 检查页码范围是否有效
        if start_page < 0 or end_page > pdf_pages_len or start_page >= end_page:
            raise ValueError("无效的页码范围")

        # 添加指定页码范围的页面到输出文件
        for i in range(start_page, end_page):
            output.add_page(pdf_file.pages[i])

        # 保存输出文件
        with open(output_pdf_path, "wb") as output_pdf_file:
            output.write(output_pdf_file)


# 示例调用
input_pdf_path = "/Applications/2022MyTextFiles/A.pdf"
output_pdf_path = "/Applications/2022MyTextFiles/B.pdf"
start_page = 0  # 从第一页开始(页码从0开始)
end_page = 5  # 截止到第5页(不包含第5页)

extract_pages(input_pdf_path, output_pdf_path, start_page, end_page)

运行正常,搞定了!

白开了一个月的会员啊!

相关推荐
鄃鳕11 分钟前
python 字典 列表 类比c++【python】
c++·python
可触的未来,发芽的智生17 分钟前
新奇特:黑猫警长的纳米世界,忆阻器与神经网络的智慧
javascript·人工智能·python·神经网络·架构
程序员三藏24 分钟前
Jmeter接口测试与压力测试
自动化测试·软件测试·python·测试工具·jmeter·接口测试·压力测试
烛阴1 小时前
用 Python 揭秘 IP 地址背后的地理位置和信息
前端·python
大宝剑1701 小时前
python环境安装
开发语言·python
Element_南笙1 小时前
吴恩达新课程:Agentic AI(笔记2)
数据库·人工智能·笔记·python·深度学习·ui·自然语言处理
倔强青铜三1 小时前
苦练Python第69天:subprocess模块从入门到上瘾,手把手教你驯服系统命令!
人工智能·python·面试
倔强青铜三1 小时前
苦练 Python 第 68 天:并发狂飙!concurrent 模块让你 CPU 原地起飞
人工智能·python·面试
星期天要睡觉2 小时前
深度学习——循环神经网络(RNN)实战项目:基于PyTorch的文本情感分析
人工智能·python·rnn·深度学习·神经网络
ERROR_LESS2 小时前
【ADS-1】【python基础-2】基本语法与数据结构(列表、字典、集合)
python