PDF文件拆分, 不限制文件大小

运行前需要确认是否安装pypdf和tkinter

sudo pip isntall pypdf

sudo apt install python-tk

工具使用方式,运行代码后,会弹出文件资源管理器,选中需要拆分的pdf文件后点确认,然后按照提示按照页数拆分还是按照组拆分

c 复制代码
from pypdf import PdfReader, PdfWriter
import os
from tkinter import filedialog, Tk

def split_pdf_custom():
    root = Tk()
    root.withdraw()
    
    # 选文件
    input_file = filedialog.askopenfilename(title="选择PDF文件", filetypes=[("PDF", "*.pdf")])
    if not input_file:
        return
    
    # 选输出文件夹
    output_folder = filedialog.askdirectory(title="选择输出文件夹")
    if not output_folder:
        output_folder = os.path.splitext(input_file)[0] + "_拆分"
    
    os.makedirs(output_folder, exist_ok=True)
    
    # 询问拆分方式
    print("\n选择拆分方式:")
    print("1. 按页数分组(如每50页一个文件)")
    print("2. 按文件数量平分(如拆成10个文件)")
    choice = input("输入1或2: ").strip()
    
    reader = PdfReader(input_file)
    total_pages = len(reader.pages)
    file_size_mb = os.path.getsize(input_file) / 1024 / 1024
    
    if choice == "1":
        n = int(input(f"总{total_pages}页,每多少页拆一个文件? ") or "50")
        pages_per_file = n
        
    else:
        num_files = int(input(f"总{total_pages}页,拆成多少个文件? ") or "5")
        pages_per_file = total_pages // num_files + (1 if total_pages % num_files else 0)
    
    # 执行拆分
    file_count = 0
    for start in range(0, total_pages, pages_per_file):
        writer = PdfWriter()
        end = min(start + pages_per_file, total_pages)
        
        for i in range(start, end):
            writer.add_page(reader.pages[i])
        
        file_count += 1
        output_path = os.path.join(output_folder, f"部分{file_count:03d}_{start+1}-{end}页.pdf")
        
        with open(output_path, 'wb') as f:
            writer.write(f)
        
        est_size = (end - start) / total_pages * file_size_mb
        print(f"✓ 部分{file_count}: {end-start}页, ~{est_size:.1f}MB")
    
    print(f"\n完成!共 {file_count} 个文件,保存在: {output_folder}")

split_pdf_custom()