import tkinter as tk标签:01,sheet,16,text,num,tk,2023,path,root From: https://www.cnblogs.com/MMD-Ali/p/17055700.html
from tkinter.filedialog import askdirectory
import os
from docx import Document
from xlwt import Workbook
import tkinter.messagebox
import sys
global Filelist,filenameList,Falselist,Falsename_list,path_w
Filelist = []
filenameList = []
Falselist = []
Falsename_list = []
path_w = []
def selectPath():
path_ = askdirectory()
path_w.append(path_)
# print("path_w :",path_w)
path.set(path_)
def display_messagebox(key):
if key == 'excel':
true_num = len(Filelist)
tk.messagebox.showinfo(title='',
message='委托单全部登记成功!共'+str(true_num)+'个委托单。') # 消息提醒弹窗,点击确定返回值为 ok
def display_messagebox2(key):
if key == 'excel_txt':
false_num = len(Falselist)
sub_num = len(Filelist)
true_num = sub_num-false_num
tk.messagebox.showinfo(title='',
message='共'+str(sub_num)+'个委托单。\r'+str(true_num)+'个登记成功,'+str(false_num)+'个委托单登记失败!') # 消息提醒弹窗,点击确定返回值为 ok
def text_call():
for home, dirs, files in os.walk(path_w[0]):
for filename1 in files:
# print("成功获取地址!")
# 文件名列表,包含完整路径
fn = os.path.join(home, filename1)
# print("fn: ",fn)
if fn.rsplit('.')[-1] == 'docx': # 判断是否是docx文件
Filelist.append(fn)
filenameList.append(filename1.replace(".docx", ""))
else:
pass
book = Workbook() # 新建工作簿
table = book.add_sheet('Over', cell_overwrite_ok=True) # 如果对同一单元格重复操作会发生overwrite Exception,cell_overwrite_ok为可覆盖
sheet = book.add_sheet('优赛课题统计') # 添加工作页
sheet.write(0, 0, '序号') # 行,列,属性值 (1,1)为B2元素,从0开始计数
sheet.write(0, 1, '项目号') # 行,列,属性值 (1,1)为B2元素,从0开始计数
sheet.write(0, 2, '项目名称') # 行,列,属性值 (1,1)为B2元素,从0开始计数
sheet.write(0, 3, '委托人') # 行,列,属性值 (1,1)为B2元素,从0开始计数
sheet.write(0, 4, '文件名') # 行,列,属性值 (1,1)为B2元素,从0开始计数
row_in = 0
i = len(Filelist)
j = 0
finish_sub = 0
while j < i:
path_word = Filelist[j]
filename = filenameList[j]
row_in = row_in + 1
try:
doc = Document(path_word)
sheet.write(row_in, 4, filename) # 行,列,属性值 (1,1)为B2元素,从0开始计数
print("文件名:", filename)
subject_name = doc.tables[0].rows[0].cells[3] # 获取第1张表第1行第3列数据
sheet.write(row_in, 2, subject_name.text) # 行,列,属性值 (1,1)为B2元素,从0开始计数
print("查新项目名称:", subject_name.text)
jigou_name = doc.tables[0].rows[2].cells[3] # 获取第一张表第一行第一列数据
sheet.write(row_in, 3, jigou_name.text) # 行,列,属性值 (1,1)为B2元素,从0开始计数
print("机构名称:", jigou_name.text)
finish_sub = finish_sub + 1
except:
Falselist.append(path_word.replace("\\","/"))
# print('Falselist:',path_word.replace("\\","/"))
Falsename_list.append(filename)
sheet.write(row_in, 5, path_word.replace("\\","/")) # 行,列,属性值 (1,1)为B2元素,从0开始计数
pass
j = j + 1
excel_load = str(path_w[0]) + '/优赛课题统计表_自动登记.xls'
book.save(filename_or_stream=excel_load) # 一定要保存
# print("ok!")
if finish_sub < i :
# print('ok<')
# with open(path_w[0]+'/自动登记失败文件路径.txt','w') as ft:
# ft.write(str(Falselist[:]))
display_messagebox2('excel_txt')
else:
# print('ok else')
display_messagebox('excel')
def JieShu():
result = tk.messagebox.askokcancel(title='', message='确认关闭?')
if result:
sys.exit(0)
else:
pass
#销毁root窗口
def opendir():
if path_w[0] == '':
pass
else:
dir = path_w[-1]
os.system(r"start "+ dir)
root = tk.Tk() # 创建窗口:实例化一个窗口对象。
root.geometry("800x400+400+250") # 调整窗口的大小+位置
root.title("委托单登记")
path = tk.StringVar()
"""text参数用于指定显示的文本;font参数用于指定字体大小和字体样式;fg参数用于指定字体颜色;"""
label2 = tk.Label(root, textvariable=path, font=("微软雅黑", 10), fg="green",
wraplength=700) # 添加标签控件
label2.place(relx=0.1, rely=0.3)
button1 = tk.Button(root, text='选择PDF文件', font=("微软雅黑", 15), command=selectPath)
button1.place(relx=0.1, rely=0.2, relwidth=0.3, relheight=0.1)
button2 = tk.Button(root, text='委托单登记', font=("微软雅黑", 15), command=text_call)
button2.place(relx=0.1, rely=0.5, relwidth=0.3, relheight=0.1)
label3 = tk.Label(root, text="使用说明:\r1、点击“打开文件夹”按钮选择待登记委托单所在的文件夹;\r"
"2、登记操作完成时会有弹窗提示,请耐心等待!"
, font=("微软雅黑", 15), fg="blue", justify="left") # 添加标签控件
label3.place(relx=0, rely=0.65)
button4 = tk.Button(root,text='打开所在文件夹', font=("微软雅黑", 15),command=opendir)
button4.place(relx=0.5, rely=0.2, relwidth=0.3, relheight=0.1)
root.protocol("WM_DELETE_WINDOW", JieShu)
root.mainloop()