python按页码切分pdf文件

发现有时候pdf需要切分,以便满足各种大小和上传要求,因此用python写了一个,方便将pdf按页码均等切分。

import PyPDF2
import os


def split_pdf(input_pdf_path, num_splits):
    # Create a PDF reader object
    pdf_reader = PyPDF2.PdfReader(open(input_pdf_path, 'rb'))
    total_pages = len(pdf_reader.pages)

    # Calculate the number of pages per split
    pages_per_split = total_pages // num_splits

    # Get the directory and base name of the input PDF
    base_dir = os.path.dirname(input_pdf_path)
    base_name = os.path.splitext(os.path.basename(input_pdf_path))[0]

    for i in range(num_splits):
        pdf_writer = PyPDF2.PdfWriter()
        start_page = i * pages_per_split
        end_page = start_page + pages_per_split
        if i == num_splits - 1:  # Make sure to include remaining pages in the last split
            end_page = total_pages

        for page in range(start_page, end_page):
            pdf_writer.add_page(pdf_reader.pages[page])

        output_pdf_path = os.path.join(base_dir, f"{base_name}_part_{i + 1}.pdf")
        with open(output_pdf_path, 'wb') as output_pdf:
            pdf_writer.write(output_pdf)

    print(f"PDF split into {num_splits} parts successfully.")


# Example usage
input_pdf_path = 'C:/XXX/XXX.pdf' # 文件路径
num_splits = 4  # 切分数量
split_pdf(input_pdf_path, num_splits)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值