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.pagespage_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)

运行正常,搞定了!

白开了一个月的会员啊!

相关推荐
曲幽7 小时前
FastAPI 身份验证总踩坑?这份 FastAPI Users “避坑指南”请收好
python·fastapi·web·jwt·oauth2·user·authentication
装不满的克莱因瓶7 小时前
掌握 RNN 与 LSTM 模型结构
人工智能·python·rnn·深度学习·神经网络·ai·lstm
何以解忧,唯有..7 小时前
Python包管理工具pip:从入门到精通
开发语言·python·pip
金銀銅鐵7 小时前
用 Tkinter 实现简单的猜数字游戏
后端·python
copyer_xyf8 小时前
Python 模块与包的导入导出
前端·后端·python
ice8130331818 小时前
【Python】Matplotlib折线图绘制
开发语言·python·matplotlib
copyer_xyf8 小时前
Python venv 虚拟环境
前端·后端·python
林爷万福9 小时前
GitHub 开源光谱数据处理项目推荐
python·光纤光谱仪
copyer_xyf10 小时前
Python 如何同时做很多事:进程、线程、协程
前端·后端·python
Full Stack Developme10 小时前
Spring Bean 依赖注入
python·spring·log4j