乐趣区

关于python:python-自动化办公-随机生成题库文档

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(“400×540”) # 窗口大小 (长*宽)
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 = readfile
dataramdom = random.sample(data,int(generateNumber))
doctitle = filename
doc = 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.CENTER
t1.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 textExample3
countdoc1 = 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 = countdoc3
folderpath = os.path.join(folderpath, docName)
makefolder(folderpath)  # 创立文件夹
n = int(countdoc2)
m = 0
while 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()

退出移动版