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)

运行正常,搞定了!

白开了一个月的会员啊!

相关推荐
scan7245 小时前
智能体多个工具调用
python
2401_867623985 小时前
CSS Flex布局中如何设置子元素间距_掌握gap属性的现代用法
jvm·数据库·python
即使再小的船也能远航5 小时前
【Python】安装
开发语言·python
weixin_421725265 小时前
Linux 编程语言全解析:C、C++、Python、Go、Rust 谁更强?
linux·python·go·c·编程语言
没有梦想的咸鱼185-1037-16635 小时前
AI-Python机器学习、深度学习核心技术与前沿应用及OpenClaw、Hermes自动化编程
人工智能·python·深度学习·机器学习·chatgpt·数据挖掘·数据分析
axinawang6 小时前
第3课:变量与输入
python
idingzhi6 小时前
A股量化策略日报()
python
zyk_computer6 小时前
AI 时代,或许 Rust 比 Python 更合适
人工智能·后端·python·ai·rust·ai编程·vibe coding
weixin199701080166 小时前
【保姆级教程】淘宝/天猫商品详情 API(item_get)接入指南:Python/Java/PHP 调用示例与 JSON 返回值解析
java·python·php
萌新小码农‍6 小时前
python装饰器
开发语言·前端·python