最近学tkinter这个模块,随手写了一个创建office文档的代码

一般我们在桌面创建office文档都是右键新建——>word文档

又或者是直接打开word,点击保存,输入文件名和保存的地方,十分的麻烦,有时候保存位置忘记了容易找不到文件

为了高效的在桌面创建一个office文档(word,excel,ppt)

我随便写了以下代码,可以配合pyinstaller打包成exe放在咱们的桌面,要用的时候直接双击

打包好的exe文件放这拉

链接:https://pan.baidu.com/s/1VbEgFi5fL1yjk4Rjza_0sA 
提取码:xiao
链接:https://pan.quark.cn/s/f30fa13d760e
提取码:CMdy

相应的源码就放这了,我感觉运行有点慢,不知道大家怎么优化的

import tkinter
import os
import winreg


"""
原来的代码中,创建docx,xlsx,pptx文件用到的函数代码几乎相同,只是扩展名不同
于是可以利用闭包函数把重复代码打包成一个函数,
另外还添加了一键删除同名文件的函数
"""

# 建立窗口
ck = tkinter.Tk()
ck.title('新建')
# 获取屏幕长宽
x = ck.winfo_screenwidth()
y = ck.winfo_screenheight()
# 设置窗口位置居中,长宽+位移坐标,顶点为窗口左上角
ck.geometry(f'255x130+{((x - 255) // 2)}+{((y - 130) // 2)}')     # 并不是真居中,左右两边会有7像素点的间隔,只有再-14才能真居中
word = tkinter.Button(ck, text='新建word文档')
ppt = tkinter.Button(ck, text='新建ppt文档')
excel = tkinter.Button(ck, text='新建excel文档')
delete = tkinter.Button(ck, text='删除同名文件')
dewenben = tkinter.Entry(ck)    # 删除文本名
wenben = tkinter.Entry(ck)      # 文本框组件
tishi = tkinter.Label(ck, text='新建文件名:', height=2)
shuiyin = tkinter.Label(ck, text='Make By xiao_hao_zi  v1.0')
# grid布局,也可以用pake()进行自动布局
word.grid(row=0, column=1)
ppt.grid(row=0, column=2)
excel.grid(row=0, column=3)
tishi.grid(row=1, column=1)
wenben.grid(row=1, column=2, columnspan=2)
delete.grid(row=2, column=1)
dewenben.grid(row=2, column=2, columnspan=2)
shuiyin.grid(row=3, column=1, columnspan=3)


# 获取桌面路径
def get_desktop():
    key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders')
    return winreg.QueryValueEx(key, "Desktop")[0]


# 创建office文件函数
def create_file(ext):
    """
    构建闭包
    :param ext: office文件扩展名
    :return:
    """
    def create(kbv):
        """
        kbv:keyboard value的缩写,点击图标<Button-1>后会产生的值,会自动赋给函数
        :param kbv:
        :return:
        """
        desktop_path = get_desktop() + '/'
        title = wenben.get()
        file_name = title + f'.{ext}'
        file_path = desktop_path + file_name
        if not os.path.exists(desktop_path):
            print("获取桌面失败")
        else:
            if not os.path.exists(file_path):
                fd = os.open(file_path, os.O_CREAT | os.O_RDWR)
                os.close(fd)
            else:
                num = 1
                file_path = desktop_path + title + str(num) + f'.{ext}'
                # 用循环来判断重名文件,重名文件自动补上序号
                while os.path.exists(file_path):
                    num += 1
                    file_path = desktop_path + title + str(num) + f'.{ext}'
                fd = os.open(file_path, os.O_CREAT | os.O_RDWR)
                os.close(fd)
                return kbv
    return create


# 删除重名文件函数
def delete_file(ext):
    """
    一键删除同名文件
    :param ext: 拓展名
    :return:
    """
    def delete_(kbv):
        desktop_path = get_desktop() + '/'
        title = dewenben.get()
        file_name = title + f'.{ext}'
        file_path = desktop_path + file_name
        if os.path.exists(file_path):
            os.remove(file_path)
        else:
            num = 1
            file_path = desktop_path + title + str(num) + f'.{ext}'
            # 用循环来判断重名文件,重名文件自动补上序号
            while not os.path.exists(file_path) and num < 20:
                num += 1
                file_path = desktop_path + title + str(num) + f'.{ext}'
            while os.path.exists(file_path):
                os.remove(file_path)
                num += 1
                file_path = desktop_path + title + str(num) + f'.{ext}'
        return kbv
    return delete_


def delete_all(kbv):
    ddoc = delete_file('docx')
    dxls = delete_file('xlsx')
    dppt = delete_file('pptx')
    ddoc(kbv)
    dppt(kbv)
    dxls(kbv)
    return kbv


# 注意这里的函数不要加括号,bind会自动把<Button-1>的值加入到函数当中去
docx = create_file('docx')
xlsx = create_file('xlsx')
pptx = create_file('pptx')


word.bind('<Button-1>', docx)
excel.bind('<Button-1>', xlsx)
ppt.bind('<Button-1>', pptx)
delete.bind('<Button>', delete_all)
tkinter.mainloop()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值