zoukankan      html  css  js  c++  java
  • 一个可以选择目录生成doc目录内容的小工具(六) -新思路的pythondox编号方案

    回到一
    目录遍历是一个经典话题,比较有经验的工程师都折腾过。so不啰嗦了,接上(四)小节,我改了一版
    思路就是创建一个arraylist存放编号信息,目录迭代中使用n判断目录广度,depth判断目录深度,在获取目录内容名称的时候直接+上编号形成新的str,使用python-docx写到word文件中。
    ps:对以前的思路感到无比的羞愧~~~

    import os
    
    numlist1 = ['一、','二、','三、','四、','五、','六、','七、','八、','九、','十、']
    numlist2 = ['(一)、', '(二)、', '(三)、', '(四)、', '(五)、', '(六)、', '(七)、', '(八)、', '(九)、', '(十)、']
    numlist3 = ['1、','2、','3、','4、','5、','6、','7、','8、','9、','10、']
    arrylist = [numlist1,numlist2,numlist3]
    
    #n判断目录广度,depth判断目录深度
    def showdir(path, depth,n = 0):
    
        for item in os.scandir(path):
            num = arrylist[depth][n]
            name = num+item.name
            print("|     " * depth + "+--" + name)
            #递归出口
            if item.is_dir():
                showdir(item.path, depth + 1)
            n += 1
    
    if __name__ == '__main__':
        showdir(r'C:UserszhaobwDesktop测试用例',0)
    
    

    完全版的代码(抽象自己做吧)

    
    from tkinter import *
    from tkinter.filedialog import askdirectory
    import tkinter.messagebox
    
    from docx import Document
    from docx.enum.style import WD_STYLE_TYPE
    from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
    from docx.oxml.ns import qn
    from docx.shared import Inches, Pt, RGBColor
    
    import os
    
    #初始化doc对象、
    def Init():
        global doc
        doc = Document()
        doc.styles["Normal"].font.name = u'宋体'
        doc.styles["Normal"].font.size = Pt(12)
        doc.styles["Normal"]._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
        doc.styles["heading 1"].font.size=Pt(20)
    
    
        name = doc.add_paragraph()
        run = name.add_run(r'本周主要内容')
        run.font.size = Pt(25)
        run.font.name = u"宋体"
        run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
        name.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
    
        # head = doc.add_heading("", level=1)
        # run = head.add_run(u"主要内容")
        # run.font.size = Pt(12)
        # run.font.color.rgb = RGBColor(0, 0, 0)
        # run.font.name = u"宋体"
        # run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
        # head.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
    
    def Save(dst):
        global doc
        try:
            doc.save(dst)
            return True
        except IOError as e:
            last_err = e.strerror
            return False
    
    #目录遍历逻辑部分
    numlist1 = ['一、','二、','三、','四、','五、','六、','七、','八、','九、','十、']
    numlist2 = ['(一)、', '(二)、', '(三)、', '(四)、', '(五)、', '(六)、', '(七)、', '(八)、', '(九)、', '(十)、']
    numlist3 = ['1、','2、','3、','4、','5、','6、','7、','8、','9、','10、']
    arrylist = [numlist1,numlist2,numlist3]
    
    #增加标题,本文的目录识别内容全部是标题形式。如果改成正文也是可以的,但是得手工设置缩进。
    def Addhead(text):
        head = doc.add_heading("")
        run = head.add_run(text)
        run.font.name = u"宋体"
        run.font.color.rgb = RGBColor(0, 0, 0)
    
    def AddText(text):
        name = doc.add_paragraph()
        run = name.add_run(text)
        run.font.size = Pt(15)
        run.font.name = u"宋体"
        run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
    
    Init()
    
    def showdir(path, depth = 0,n = 0):
        for item in os.scandir(path):
            num = arrylist[depth][n]
            name = num + item.name
            # print("|     " * depth + "+--" + name)
            #写入word
            # Addhead(name)
            AddText(name)
            #递归出口
            if item.is_dir():
                showdir(item.path, depth + 1)
            n += 1
    
    
    def createdoc(path):
        file = path + os.sep + '本周主要内容.docx'
        Save(file)
    
    
    #GUI
    #选择路径
    def selectPath():
        path_ = askdirectory()
        guipath.set(path_)
    
    def outPut():
        showdir(guipath.get())
        createdoc(guipath.get())
        tkinter.messagebox.showinfo('提示', '导出完成')
        root_window.destroy()
    
    # 创建窗口对象
    root_window = Tk()
    root_window.title('目录内容识别工具')
    root_window.geometry('300x100')
    
    
    #路径地址
    guipath = StringVar()
    
    Label(root_window,text = "目标路径:").grid(row = 0, column = 0)
    
    #textvariable关联一个StringVar类,可以用set()和get()函数去设置和获取控件中的值
    Entry(root_window, textvariable = guipath).grid(row = 0, column = 1)
    
    Button(root_window, text = "选择", command = selectPath,width=6).grid(row = 0, column = 2)
    Button(root_window, text = "导出", command= outPut ,width=6).grid(row = 0, column = 3)
    
    #进入消息循环,没这个不显示
    
    
    
    if __name__ == '__main__':
        root_window.mainloop()
    
    
  • 相关阅读:
    每个部门都有自己的游戏规则
    ssh作为代理,反向登录没有固定公网ip的局域网内的某远程服务器
    x11vnc 作为远程桌面服务器时vnc客户端键盘无法长按连续输入字符
    vim 编译使用ycm启动问题 fixed
    ubuntu设置普通用户也能执行docker命令
    git常见使用
    切图的必要步骤
    css居中
    清除浮动
    Spring-AOP(2)
  • 原文地址:https://www.cnblogs.com/zhaobowen/p/13306548.html
Copyright © 2011-2022 走看看