Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3657421
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

全部博文(365)

文章存档

2023年(8)

2022年(130)

2021年(155)

2020年(50)

2019年(22)

我的朋友

分类: Python/Ruby

2021-09-27 17:32:24

#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)  # 外汇跟单gendan5.comText放在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()

阅读(1649) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~