[Python] 纯文本查看 复制代码
import ttkbootstrap as ttk
from ttkbootstrap.constants import *
from ttkbootstrap.dialogs import Messagebox
from tkinter.filedialog import askdirectory,askopenfilename
from configparser import ConfigParser
from ttkbootstrap.scrolled import ScrolledText
from multiprocessing.dummy import Process
from ttkbootstrap.scrolled import ScrolledFrame
import os,docx,sys,re,xlwt,time
from win32com.client import Dispatch, DispatchEx
class WordCountClient(ttk.Frame):
def __init__(self, master):
super().__init__(master, padding=15)
self.imp_folder = ttk.StringVar()
self.save_path = ttk.StringVar()
self.pack(fill=BOTH, expand=YES)
# form header
self.create_text_textarea()
self.create_dir_entry('文件夹:',self.imp_folder,self.choose_dir_name)
self.create_dir_entry('导出路径:',self.save_path,self.choose_save_path)
self.create_buttonbox()
menu = self.create_menu()
master.config(menu=menu)
self.arr = []
def create_menu(self):
'''
创建菜单
'''
menu = ttk.Menu(master=self)
menu.add_command(label='版权', command=self.copyright)
return menu
def copyright(self):
screen_width = self.master.winfo_screenwidth()
screen_height = self.master.winfo_screenheight()
width = 600
height = 100
window_size = f'{width}x{height}+{round((screen_width-width)/2)}+{round((screen_height-height)/2)}' #round去掉小数
p = ttk.Toplevel()
p.title("版权")
p.geometry(window_size) #定义弹窗大小及位置,前两个是大小,用字母“x”连接,后面是位置。
# 类目名称
label_1 = ttk.Label(p, text="米易县蓝点科技有限公司 (版权所有)",foreground='#409EFF',justify='center')
label_1.pack(side=LEFT, padx=5, pady=5)
def create_dir_entry(self, label, variable,fun):
"""Create a single form entry"""
container = ttk.Frame(self)
container.pack(fill=X, pady=15)
lbl = ttk.Label(master=container, text=label.title())
lbl.pack(side=LEFT, padx=5)
ent = ttk.Entry(master=container, textvariable=variable,width=25)
ent.pack(side=LEFT, padx=5, fill=X,expand=True)
ent.bind('<Button-1>',fun)
def choose_save_path(self,event):
path = askdirectory()
self.save_path.set(path)
def choose_dir_name(self,event):
path = askdirectory()
self.imp_folder.set(path)
# 创建待替换的文本的文本域
def create_text_textarea(self):
"""Create and add the widget elements"""
style = ttk.Style()
self.imp_text_textbox = ScrolledText(
master=self,
highlightcolor=style.colors.primary,
highlightbackground=style.colors.border,
highlightthickness=1,
height=25,width=100
)
self.imp_text_textbox.pack(side=TOP, padx=5)
def create_form_entry(self, label, variable):
"""Create a single form entry"""
container = ttk.Frame(self)
container.pack(side=LEFT, fill=X, expand=YES, pady=5)
lbl = ttk.Label(master=container, text=label.title())
lbl.pack(side=LEFT, padx=5)
ent = ttk.Entry(master=container, textvariable=variable)
ent.pack(side=LEFT, padx=5, fill=X, expand=YES)
def create_buttonbox(self):
"""Create the application buttonbox"""
container = ttk.Frame(self)
container.pack(fill=X, expand=YES, pady=(15, 10))
export_btn = ttk.Button(
master=container,
text="导出",
command=self.export,
bootstyle=PRIMARY,
width=6,
)
export_btn.pack(side=RIGHT, padx=5)
sub_btn = ttk.Button(
master=container,
text="开始",
command=self.on_submit,
bootstyle=SUCCESS,
width=6,
)
sub_btn.pack(side=RIGHT, padx=5)
sub_btn.focus_set()
def export(self):
if len(self.arr)>0:
save_path = self.save_path.get()
file_name = time.strftime('%Y-%m-%d')+"-"+str(time.time())+'.xls'
full_save_path = os.path.join(save_path,file_name)
workbook = xlwt.Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('文件字数统计')
# 向sheet页中添加数据
worksheet.write(0,0,'文件路径')
worksheet.write(0,1,'数量')
worksheet.col(0).width = 256 * 50
for row,line in enumerate(self.arr):
file_path = line["file_path"]
count = line["count"]
worksheet.write(row+1,0,file_path)
worksheet.write(row+1,1,count)
workbook.save(full_save_path)
Messagebox.show_info('导出成功!')
def log(self, msg):
self.imp_text_textbox.insert(END, msg+'\n')
self.imp_text_textbox.update()
def on_submit(self):
self.arr.clear()
self.imp_text_textbox.delete('1.0',END)
folder = self.imp_folder.get()
folders = os.listdir(folder)
for children_folder in folders:
full_children_folder = os.path.join(folder,children_folder)
files = os.listdir(full_children_folder)
for file in files:
file_path = os.path.join(full_children_folder,file)
suffix = os.path.splitext(file_path)[-1]
if suffix=='.txt':
self.deal_txt(file_path)
elif suffix=='.docx':
self.deal_docx(file_path)
elif suffix =='.doc':
self.deal_doc(file_path)
def deal_txt(self,path):
word_count = 0
with open(path, 'r') as file:
# str_new = re.sub(r"\s+", "", )
word_count = len(file.read())
self.arr.append({
"file_path":path,
"count":word_count
})
msg = path+":"+str(word_count)
self.log(msg)
def deal_docx(self,path):
word = 0
doc = docx.Document(path) # 打开每一个 Word 文档
for j in doc.paragraphs: # 遍历某一个 word 文档的所有段落
# str_new = re.sub(r"\s+", "", j.text)
word += len(j.text) # j.text 为某段落的所有字符,len 即为段落字符数量
self.arr.append({
"file_path":path,
"count":word
})
msg = path+":"+str(word)
self.log(msg)
def deal_doc(self,path):
word = Dispatch('Word.Application') # 打开word应用程序
# word = DispatchEx('Word.Application') # 启动独立的进程
word.Visible = 0 # 后台运行,不显示
word.DisplayAlerts = 0 # 不警告
doc = word.Documents.Open(FileName=path, Encoding='gbk')
count = 0
for para in doc.paragraphs:
count = len(para.Range.Text)+count-1
# for t in doc.Tables:
# for row in t.Rows:
# for cell in row.Cells:
# str_new = re.sub(r"\s+", "", cell.Range.Text)
# print(str_new)
# count = len(str_new)+count
self.arr.append({
"file_path":path,
"count":count
})
msg = path+":"+str(count)
self.log(msg)
doc.Close()
word.Quit
def on_cancel(self):
"""Cancel and close the application."""
self.quit()
if __name__ == '__main__':
app = ttk.Window("字数统计", "yeti")
WordCountClient(app)
app.mainloop()