encoding:utf-8

import pandas as pd
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx import Document
from docx.shared import Pt
from docx.oxml.ns import qn
import random
import os
from docx.shared import Pt,RGBColor
import tkinter as tk
from tkinter import filedialog
import threading
window = tk.Tk()
window.title('随机生成题库器')
window.geometry("400x540") # 窗口大小(长*宽)
label = tk.Label(window,text='题库数量', #text为显示的文本内容

             bg='black',fg='white',justify='left')         #bg为背景色,fg为前景色

label.pack(padx=50,pady=2)
textExample = tk.Text(window, width=20, height=2) # 文本输入框
textExample.pack(padx=50,pady=10) # 把Text放在window下面,显示Text这个控件

textExample.insert("insert","请输出须要多少题为一个文件库")

label = tk.Label(window,text="题库文档数量", #text为显示的文本内容

             bg='black',fg='white',justify='left')         #bg为背景色,fg为前景色

label.pack(padx=50,pady=2)
textExample2 = tk.Text(window, width=20, height=2) # 文本输入框
textExample2.pack(padx=50,pady=10) # 金融期货把Text放在window下面,显示Text这个控件
label = tk.Label(window,text="文档名称", #text为显示的文本内容

             bg='black',fg='white',justify='left')         #bg为背景色,fg为前景色

label.pack(padx=50,pady=2)
textExample3 = tk.Text(window, width=20, height=2) # 文本输入框
textExample3.pack(padx=50,pady=10) # 把Text放在window下面,显示Text这个控件
def readfile(filepath):

"""读取EXCEL文件"""datalist = []for d in filepath:    data = pd.read_excel(d)    data = data.values.tolist()    for d in data:        title = d[0]        content = d[1]        tc = [title, content]        datalist.append(tc)return datalist

def wordDoc(readfile,filename,filpaths,generateNumber):

"""写入word文档filename: 文件名称generateNumber:随机生成多少题"""data = readfiledataramdom = random.sample(data,int(generateNumber))doctitle = filenamedoc = Document()doc.styles['Normal'].font.name = u'宋体'doc.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')# doc.styles['Normal'].font.color.rgb = RGBColor(0, 0, 0)#输出文档题目t = doc.add_paragraph()t1 = t.add_run(doctitle)  # 文档总题目t1.font.size = Pt(15)t.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTERt1.font.color.rgb = RGBColor(255, 0, 0)#输出文档内容for dr in dataramdom:     title = dr[0]    content = dr[1]    content = str(content).replace('\n','',1)    t2 = doc.add_paragraph().add_run(title) #输出题目    t2.font.color.rgb = RGBColor(0, 0, 255)    t2.bold = True    doc.add_paragraph(content) #输出内容doc.save(f'{filpaths}.docx')

def makefolder(filepath):

"""创立文件夹"""while True:    if os.path.isdir(filepath):        break    else:        os.makedirs(filepath)

def upload_file():

"""获取文件地址"""selectFile = tk.filedialog.askopenfilenames()  # askopenfilename 1次上传1个;askopenfilenames1次上传多个selectFile = list(selectFile)upload_entry.insert(0, selectFile)return selectFile

def getTextInput():

"""获取输出"""global textExample3countdoc1 = textExample.get("1.0", "end")  # 获取多少题countdoc2 = textExample2.get("1.0", "end") # 造成多少题库文档countdoc3 = textExample3.get("1.0", "end")countdoc1 = str(countdoc1).replace('\n','')countdoc2 = str(countdoc2).replace('\n','')countdoc3 = str(countdoc3).replace('\n','')filelist = upload_file()r = readfile(filelist)folderpath = './生成的题库'docName = countdoc3folderpath = os.path.join(folderpath, docName)makefolder(folderpath)  # 创立文件夹n = int(countdoc2)m = 0while True:    m += 1    if m > n:        break    else:        filename = docName + "_" + str(m)        filapathName = os.path.join(folderpath, filename)        print(f"{filename} 生成胜利")        wordDoc(r,countdoc3,filapathName,countdoc1)

def thread_it(func):

'''将函数打包进线程'''# 创立t = threading.Thread(target=func)# 守护 !!!t.setDaemon(True)# 启动t.start()# 阻塞--卡死界面!

输出文件

upload = tk.Frame(window)
upload.pack(padx=20,pady=20)
upload_entry = tk.Entry(upload, width='40')
upload_entry.pack(padx=50,pady=10)
import time
nowtime = time.time()
nowtime1 = int(time.time())
nowtime2 = int(time.time())+3000
btnRead = tk.Button(window, height=1, width=10, text="开始程序", command=lambda:thread_it(getTextInput))
btnRead.pack(padx=50,pady=10) # 显示按钮
window.mainloop()