PDF工具箱-分割提取合并
发布日期:2025-05-01 23:54:45 浏览次数:12 分类:精选文章

本文共 3262 字,大约阅读时间需要 10 分钟。

Python PDF工具开发指南:分割、合并与提取功能实现

一、PyPDF2基础使用说明

PyPDF2 是一个开源的 PDF 操作库,支持 PDF 文件的读取、写入、合并与分割等操作。开发者可以通过它轻松实现 PDF 文件的多种业务需求。本文将基于 PyPDF2 开发一个功能齐全的 PDF工具,支持文件的分割、合并与特定页数的提取。

二、Tkinter GUI界面设计

本工具采用 Tkinter 作为 GUI框架,提供用户友好的操作界面。主要功能包括:

  • 文件管理:支持多个 PDF 文件的添加与清空
  • 分割功能:将 PDF 文件按页数分割为多个单页 PDF
  • 合并功能:将多个 PDF 文件按顺序合并为一个 PDF
  • 提取功能:根据指定页码提取特定页数,生成新的 PDF 文件
  • 三、功能实现细节

    1. PDF 分割功能

    import osfrom PyPDF2 import PdfFileReader, PdfFileWriterdef splitPDF(inFile, outPath):    """拆分PDF文件为单页"""    if not os.path.exists(outPath):        os.makedirs(outPath)    f = open(inFile, 'rb')    reader = PdfFileReader(f)    numPages = reader.getNumPages()    for i in range(numPages):        writer = PdfFileWriter()        writer.addPage(reader.getPage(i))        outFile = os.path.join(outPath, str(i+1)+'.pdf')        with open(outFile, 'wb') as outf:            writer.write(outf)    f.close()

    说明:该函数接收源 PDF 文件路径与输出目录,将 PDF 按页数拆分为多个单页 PDF 文件保存。

    2. PDF 合并功能

    from PyPDF2 import PdfFileMergerdef mergePDF(inFileList, outFileName):    """合并多个PDF文件为一个PDF"""    pdfMerger = PdfFileMerger()    for infile in inFileList:        with open(infile, 'rb') as f:            pdfMerger.append(f)    with open(outFileName, 'wb') as f:        pdfMerger.write(f)

    说明:将指定列表中的 PDF 文件合并为一个新 PDF 文件,依次添加并写入输出文件。

    3. PDF 提取功能

    def extractPDF(inFile, outFileName, pages):    """提取特定页,生成新的PDF文件"""    pages = sorted(pages)    f = open(inFile, 'rb')    reader = PdfFileReader(f)    writer = PdfFileWriter()    numPages = reader.getNumPages()    for i in range(numPages):        if (i+1) in pages:            writer.addPage(reader.getPage(i))    with open(outFileName, 'wb') as outf:        writer.write(outf)    f.close()

    说明:根据指定页码列表提取对应的 PDF 页面,生成新的 PDF 文件。

    四、Tkinter 界面实现

    1. 界面布局

    root = tkinter.Tk()root.geometry('800x400')root.title('PDF合并分割工具')info_LABEL = tkinter.Label(root, text='等待添加文件', justify=tkinter.LEFT, fg='red')info_LABEL.place(relx=0.3, rely=0.05, relwidth=0.4, relheight=0.1)# 添加文件按钮add_BUTTON = tkinter.Button(root, text='添加文件\n(一次可添加多个,\n可多次添加)', command=addFiles_TK)add_BUTTON.place(relx=0.1, rely=0.1, relwidth=0.2, relheight=0.2)# 清空文件按钮clear_BUTTON = tkinter.Button(root, text='清空文件', command=clearFiles_TK)clear_BUTTON.place(relx=0.1, rely=0.35, relwidth=0.2, relheight=0.1)# 功能按钮split_BUTTON = tkinter.Button(root, text='分割成单页', command=split_TK)split_BUTTON.place(relx=0.65, rely=0.1, relwidth=0.2, relheight=0.1)extract_BUTTON = tkinter.Button(root, text='提取指定页', command=extract_TK)extract_BUTTON.place(relx=0.65, rely=0.225, relwidth=0.2, relheight=0.1)merge_BUTTON = tkinter.Button(root, text='合并多个PDF', command=merge_TK)merge_BUTTON.place(relx=0.65, rely=0.35, relwidth=0.2, relheight=0.1)# 显示提示信息的文本框output1_TEXT = tkinter.Text(root)output1_TEXT.place(relx=0.1, rely=0.5, relwidth=0.8, relheight=0.4)root.mainloop()

    2. 功能实现

  • 文件管理

    • addFiles_TK():弹出文件选择对话框,添加 PDF 文件到列表。
    • clearFiles_TK():清空文件列表和提示信息。
  • 分割功能

    • split_TK():验证文件列表,调用 splitPDF 函数执行分割操作。
  • 合并功能

    • merge_TK():验证文件列表,弹出保存对话框,调用 mergePDF 函数执行合并操作。
  • 提取功能

    • extract_TK():弹出子窗体,用户输入页码,调用 extractPDF 函数执行提取操作。
  • 五、使用说明

  • 安装依赖:

    • 使用 pip 安装 PyPDF2:pip install PyPDF2
  • 执行脚本:

    • 双击运行 PDFTools.py 文件
  • 使用方法:

    • 添加 PDF 文件到工具中
    • 根据需求选择合适的功能
    • 指定输出路径或文件名
    • 完成操作后查看输出结果
  • 六、注意事项

    • 确保所有操作前已备份 PDF 文件
    • 输出文件路径需有权限
    • 提取页码时请确保格式正确
    • 合并文件时请保持文件顺序一致

    通过以上实现,用户可以轻松完成 PDF 文件的分割、合并与特定页数的提取,提升工作效率。

    上一篇:pdf打印骑缝章
    下一篇:pdf做成翻页电子书_第一弹:常见BOOX电子书阅读器问题解答,这些技能你都会吗?...

    发表评论

    最新留言

    能坚持,总会有不一样的收获!
    [***.219.124.196]2026年06月19日 12时34分10秒