在好例子网,分享、交流、成长!
您当前所在位置:首页Python 开发实例Python语言基础 → 正反面PDF文件合并

正反面PDF文件合并

Python语言基础

下载此实例
  • 开发语言:Python
  • 实例大小:8.88M
  • 下载次数:6
  • 浏览次数:30
  • 发布时间:2024-12-03
  • 实例类别:Python语言基础
  • 发 布 人:mayunzhong
  • 文件格式:.zip
  • 所需积分:2
 相关标签: 文件合并 pdf pd 合并 文件

实例介绍

【实例简介】

针对部分扫描仪,有送纸器,可多页扫描但无正反面扫描功能,可以分别进行正、反面扫描,然后利用本软件进行PDF文件快速自动合并。

【实例截图】

from clipboard

【核心代码】

import PyPDF2  
import tkinter as tk  
from tkinter import filedialog
from tkinter.messagebox import *



def browse_files():  
    # 打开文件浏览对话框  
    global source_pdf1,label1
    source_pdf1 = filedialog.askopenfilename(filetypes=[("PDF文件", "*.pdf")])
    label1.config(text=source_pdf1)
def browse_files2():
 
    # 打开文件浏览对话框  
    global source_pdf2
    source_pdf2 = filedialog.askopenfilename(filetypes=[("PDF文件", "*.pdf")])
    label2.config(text=source_pdf2)
def browse_files3():
    # 创建一个隐藏的顶级窗口,防止tkinter报错(在没有主窗口时调用filedialog)  
    #root = tk.Tk()  
    #root.withdraw()  
    # 打开文件浏览对话框  
    global target_pdf
    target_pdf = filedialog.asksaveasfilename(initialfile="合并PDF文件.pdf",filetypes=[("PDF文件", "*.pdf")])
    if target_pdf and target_pdf.lower()[-4:]!=".pdf":
        target_pdf=target_pdf ".pdf"
    label3.config(text=target_pdf)
def insert_pages_from_pdf_to_pdf():
    if not target_pdf or not source_pdf2 or not source_pdf1:
        showinfo(title = "错误提示", message = "请选择正面PDF文件、反面PDF文件,并设置文件名称。")
        return
    """  
    Insert pages from one PDF to another.  
      sort2:0反面正序;1反面逆序
    :param source_pdf_path: Path to the source PDF file.  :param
    target_pdf_path: Path to the target PDF file where pages will be
    inserted.  :param start_page: The starting page number (0-based
    index) to insert from the source PDF.  :param end_page: The end page
    number (0-based index) to insert from the source PDF.  :param
    insert_position: The position (0-based index) in the target PDF
    where the pages will be inserted.  """
    target_pdf_writer=PyPDF2.PdfWriter()
    # Open the source PDF file1
    with open(source_pdf1, 'rb') as source_file1:
        source_pdf_reader1 = PyPDF2.PdfReader(source_file1)  
        # Open the source PDF file2
        with open(source_pdf2, 'rb') as source_file2:
            source_pdf_reader2 = PyPDF2.PdfReader(source_file2)
            if(len(source_pdf_reader1.pages)!=len(source_pdf_reader2.pages)):
                return
            n=len(source_pdf_reader1.pages)
            for i in range(0,n):
                target_pdf_writer.add_page(source_pdf_reader1.pages[i])
                target_pdf_writer.add_page(source_pdf_reader2.pages[(n-1)*sort2-2*sort2*i i])
    # Write the new PDF content back to the file
    with open(target_pdf, 'wb') as target_file:
        target_pdf_writer.write(target_file)  
        # Truncate the file to the correct size 
        target_file.truncate()
    showinfo(title = "合并完成提示", message = "已按要求完成合并,请到相应文件夹查看合并文件!")

root = tk.Tk()  
root.title("正反面PDF文件合并")

# root.geometry("800x300 600 300")

label0=tk.Label(root,text="正反面PDF文件合并",font=("黑体", 16))
label0.grid(row=0,column=0,columnspan=2)

browse_button1 = tk.Button(root, text="选择正面文件(1,3...n-1)", command=browse_files)  
browse_button1.grid(row=1,column=0)
label1=tk.Label(root,text="")
label1.grid(row=1,column=1)
browse_button2 = tk.Button(root, text="选择反面文件(n...6,4,2)", command=browse_files2)  
browse_button2.grid(row=2,column=0)
label2=tk.Label(root,text="")
label2.grid(row=2,column=1)
browse_button3 = tk.Button(root, text="设置合并文件名称(1,2...n)", command=browse_files3)  
browse_button3.grid(row=3,column=0)
label3=tk.Label(root,text="")
label3.grid(row=3,column=1)
label4=tk.Label(root,text="")
label4.grid(row=5,column=0,columnspan=2,padx=300)
# Example usage
source_pdf1 = ''  # Path to the source PDF file  1
source_pdf2 = ''  # Path to the source PDF file  2
target_pdf = ''  # Path to the target PDF file
sort2=1
browse_button3 = tk.Button(root, text="开始合并PDF文件", command=insert_pages_from_pdf_to_pdf)  
browse_button3.grid(row=4,column=0,columnspan=2)
root.mainloop()

实例下载地址

正反面PDF文件合并

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警