基于tkinter的PDF合并小工具

前言

发现听网课的时候多个pdf看起来并不是很方便,加上日常生活需要使用多个pdf合并成一个pdf方便传送和阅读,于是用python制作一款个人小工具!

打包后

在这里插入图片描述

测试使用效果:

pdf合并之前:
在这里插入图片描述
pdf合成后:
在这里插入图片描述

源代码:

"""
    功能:1)可以通过鼠标形式勾选想要处理的文件夹
         2)可以选择性合并多个pdf文件
         3)可以通过鼠标形式导出到相应的目录
"""
from tkinter import *
from tkinter.filedialog import askopenfilenames,asksaveasfilename
from tkinter import messagebox
import threading
from PyPDF2 import PdfFileReader, PdfFileWriter

# 1、添加选择目标文件集,以元组形式返回所有的文件名
def selectPath():
    my_list = []
    path_ = askopenfilenames()
    path.set(path_)
    my_list.append(path_)
    return my_list

# 2、添加多线程,避免界面卡顿
def thread_it(func):
    '''将函数打包进线程'''
    # 创建
    t = threading.Thread(target=func)
    # 守护 !!!
    t.setDaemon(True)
    # 启动
    t.start()

# 3、添加窗口说明菜单
def about():
    messagebox.showinfo('提示', '本应用写于2020年11月28日,制作人lures!')

# 4、添加保存pdf到目标目录
def savePdf():
    my_list = []
    path_ = asksaveasfilename()
    save.set(path_)
    my_list.append(path_)
    return my_list

# 5、添加pdf合并功能
def merge():
    # 手动选择输出文件的路径
    output = PdfFileWriter()
    outputPages = 0
    pdf_fileName = savePdf()[0]
    my_list = selectPath()
    # [('D:/lures2020-demos/2020_12_神经网络与深度学习Tensorflow2.0实战/课件/0 课程概述.pdf', 'D:/lures2020-demos/2020_12_神经网络与深度学习Tensorflow2.0实战/课件/1.1 人工智能的诞生.pdf')]
    # 以元组的形式呈现,my_list[0][0]/my_list[0][1]这样获取每一个pdf文件
    for i in range(len(my_list[0])):
        # 读取源pdf文件
        input_file = PdfFileReader(open(my_list[0][i],"rb"))
        # 获得源pdf文件中页面总数
        pageCount = input_file.getNumPages()
        outputPages += pageCount
        # 分别将page添加到输出output中
        for ipage in range(pageCount):
            output.addPage(input_file.getPage(ipage))
    outputStream = open(pdf_fileName,"wb")
    output.write(outputStream)
    outputStream.close()
    messagebox.showinfo("提示!","完成合并!祝您生活愉快~")

# 6、添加使用说明文件
def use_function():
    messagebox.showinfo('提示', '使用说明如下:\n首先,需要选择合并PDF后保存的路径及名称(记为:text)\n其次,会弹出一个框让你选择一个要合并的多个PDF文件\n最后,完成合并后会在目标目录生成text\n完成合并后,会弹出一个框显示已经完成合并')

# 开始构造整体框架
if __name__ == '__main__':
    root = Tk()
    path = StringVar()
    save = StringVar()              # 保存路径
    root.title("PDF合并小工具")
    root.geometry('370x120+800+400')
    root.resizable(0, 0)
    # 显示内容部分
    label1 = Label(root, text='目标路径:', font=("仿宋", 12)).grid(row=0, column=0, sticky='e')
    entry1 = Entry(root, textvariable=path, width='30').grid(row=0, column=1)
    label2 = Label(root, text='保存路径:', font=("仿宋", 12)).grid(row=1, column=0, sticky='e')
    entry2 = Entry(root, textvariable=save,width='30').grid(row=1, column=1)
    Label(root, text="").grid(row=2, column=1)
    button1 = Button(root, text="合并PDF", font=("仿宋", 12), command=lambda :thread_it(merge)).grid(row=3, column=0,sticky='e')
    button2 = Button(root, text="退出Exit", font=("仿宋", 12), command=root.quit).grid(row=3, column=2, sticky='w')
    label3 = Label(root, text="lures友情出品,欢迎致电2142781703~~~", font=("仿宋", 10)).grid(row=4, columnspan=3, sticky='e')
    # 添加窗口栏菜单
    menu1 = Menu(root)
    menu1.add_command(label="About", command=about)
    menu1.add_command(label="使用说明", command=use_function)
    root.config(menu=menu1)
    root.mainloop()

从10点多到12点到花了不久时间制作的!具体文档见:点我跳转
原创文章,转载标注来处,谢谢!
微信公众号:空谷小莜蓝
大三学生,准备考研,无心聊天!)
Abstract Describes the Tkinter widget set for constructing graphical user interfaces (GUIs) in the Python programming language. This publication is available in Web form1 and also as a PDF document2. Please forward any comments to tcc-doc@nmt.edu. Table of Contents 1. What is Tkinter?.......................................................................................................................3 2. A minimal application..............................................................................................................3 3. Definitions..............................................................................................................................4 4. Layout management.................................................................................................................5 4.1. The .grid() method....................................................................................................5 4.2. Other grid management methods...................................................................................6 4.3. Configuring column and row sizes.................................................................................7 4.4. Making the root window resizeable................................................................................8 5. Standard attributes...................................................................................................................8 5.1. Dimensions...................................................................................................................9 5.2. The coordinate system...................................................................................................9 5.3. Colors...........................................................................................................................9 5.4. Type fonts...................................................................................................................10 5.5. Anchors......................................................................................................................11 5.6. Relief styles.................................................................................................................12 5.7. Bitmaps.......................................................................................................................12 5.8. Cursors.......................................................................................................................12 5.9. Images........................................................................................................................14 5.10. Geometry strings........................................................................................................14 5.11. Window names...........................................................................................................15 5.12. Cap and join styles.....................................................................................................15 5.13. Dash patterns.............................................................................................................16 5.14. Matching stipple patterns............................................................................................16 6. The Button widget................................................................................................................17 7. The Canvas widget................................................................................................................19 7.1. Canvas coordinates......................................................................................................20 7.2. The Canvas display list................................................................................................20 7.3. Canvas object IDs........................................................................................................21 7.4. Canvas tags................................................................................................................21 1http://www.nmt.edu/tcc/help/pubs/tkinter/ 2http://www.nmt.edu/tcc/help/pubs/tkinter/tkinter.pdf 1 Tkinter reference New Mexico Tech Computer Center 7.5. CanvastagOrId arguments......................................................................................21 7.6. Methods on Canvas widgets........................................................................................21 7.7. Canvas arc objects.......................................................................................................26 7.8. Canvas bitmap objects.................................................................................................28 7.9. Canvas image objects..................................................................................................29 7.10. Canvas line objects.....................................................................................................29 7.11. Canvas oval objects....................................................................................................31 7.12. Canvas polygon objects..............................................................................................32 7.13. Canvas rectangle objects.............................................................................................34 7.14. Canvas text objects.....................................................................................................35 7.15. Canvas window objects..............................................................................................36 8. The Checkbutton widget......................................................................................................37 9. The Entry widget..................................................................................................................40 9.1. Scrolling an Entry widget............................................................................................43 10. The Frame widget................................................................................................................43 11. The Label widget................................................................................................................44 12. The LabelFrame widget......................................................................................................46 13. The Listbox widget............................................................................................................48 13.1. Scrolling a Listbox widget........................................................................................52 14. The Menu widget..................................................................................................................52 14.1. Menu item creation (coption) options.........................................................................55 14.2. Top-level menus.........................................................................................................56 15. The Menubutton widget......................................................................................................57 16. The Message widget............................................................................................................59 17. The OptionMenu widget.......................................................................................................60 18. The PanedWindow widget....................................................................................................61 18.1. PanedWindow child configuration options...................................................................63 19. The Radiobutton widget....................................................................................................64 20. The Scale widget................................................................................................................67 21. The Scrollbar widget........................................................................................................70 21.1. The Scrollbarcommand callback............................................................................72 21.2. Connecting a Scrollbar to another widget................................................................73 22. The Spinbox widget............................................................................................................73 23. The Text widget..................................................................................................................78 23.1. Text widget indices...................................................................................................80 23.2. Text widget marks....................................................................................................81 23.3. Text widget images...................................................................................................82 23.4. Text widget windows...............................................................................................82 23.5. Text widget tags.......................................................................................................82 23.6. Setting tabs in a Text widget......................................................................................83 23.7. The Text widget undo/redo stack..............................................................................83 23.8. Methods on Text widgets..........................................................................................84 24. Toplevel: Top-level window methods..................................................................................91 25. Universal widget methods.....................................................................................................93 26. Standardizing appearance...................................................................................................101 26.1. How to name a widget class......................................................................................102 26.2. How to name a widget instance.................................................................................102 26.3. Resource specification lines.......................................................................................102 26.4. Rules for resource matching......................................................................................103 27. Connecting your application logic to the widgets...................................................................104 28. Control variables: the values behind the widgets...................................................................104 29. Focus: routing keyboard input.............................................................................................106 New Mexico Tech Computer Center Tkinter reference 2 30. Events................................................................................................................................107 30.1. Levels of binding......................................................................................................108 30.2. Event sequences.......................................................................................................109 30.3. Event types..............................................................................................................109 30.4. Event modifiers........................................................................................................110 30.5. Key names...............................................................................................................111 30.6. Writing your handler: The Event class......................................................................113 30.7. The extra arguments trick..........................................................................................115 30.8. Virtual events...........................................................................................................116 31. Pop-up dialogs....................................................................................................................116 31.1. The tkMessageBox dialogs module..........................................................................116 31.2. The tkFileDialog module.....................................................................................118 31.3. The tkColorChooser module.................................................................................119
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值