Загрузка данных
#---------------------------------------------------ГЕНЕРАТОР БИЛЕТОВ---------------------------------------------------
import tkinter as tk
from tkinter import filedialog, messagebox
import docx
import random
import os
from datetime import datetime
def get_questions_from_docx(file_path):
try:
doc = docx.Document(file_path)
questions = [p.text.strip() for p in doc.paragraphs if p.text.strip()]
return questions
except Exception as e:
messagebox.showerror("Ошибка", f"Не удалось прочитать файл: {e}")
return []
def generate_ticket():
file1 = entry_file1.get()
file2 = entry_file2.get()
if not os.path.exists(file1) or not os.path.exists(file2):
messagebox.showwarning("Внимание", "Пожалуйста, выберите оба файла!")
return
questions1 = get_questions_from_docx(file1)
questions2 = get_questions_from_docx(file2)
if questions1 and questions2:
q1 = random.choice(questions1)
q2 = random.choice(questions2)
result_text.config(state=tk.NORMAL)
result_text.delete(1.0, tk.END)
ticket_content = (
f"ЭКЗАМЕНАЦИОННЫЙ БИЛЕТ\n"
f"Дата генерации: {datetime.now().strftime('%d.%m.%Y %H:%M')}\n"
f"--------------------------------------\n"
f"Вопрос №1:\n{q1}\n\n"
f"Вопрос №2:\n{q2}"
)
result_text.insert(tk.END, ticket_content)
result_text.config(state=tk.DISABLED)
btn_save.config(state=tk.NORMAL)
else:
messagebox.showwarning("Ошибка", "Один из файлов пуст.")
def save_to_file():
content = result_text.get(1.0, tk.END).strip()
if not content:
return
save_path = filedialog.asksaveasfilename(
defaultextension=".docx",
filetypes=[("Word Document", "*.docx")],
initialfile="Экзаменационный_билет.docx"
)
if save_path:
try:
new_doc = docx.Document()
new_doc.add_heading('Экзаменационный билет', 0)
lines = content.split('\n')
for line in lines[2:]:
new_doc.add_paragraph(line)
new_doc.save(save_path)
messagebox.showinfo("Успех", f"Билет успешно сохранен:\n{save_path}")
except Exception as e:
messagebox.showerror("Ошибка", f"Не удалось сохранить файл: {e}")
root = tk.Tk()
root.title("Генератор билетов v2.0")
root.geometry("550x550")
root.configure(padx=20, pady=20)
tk.Label(root, text="Файл с первым блоком вопросов:", font=("Arial", 10, "bold")).pack(anchor="w")
frame1 = tk.Frame(root)
frame1.pack(fill="x", pady=5)
entry_file1 = tk.Entry(frame1, width=50)
entry_file1.pack(side="left", padx=2)
tk.Button(frame1, text="Обзор", command=lambda: [entry_file1.delete(0, tk.END), entry_file1.insert(0,
filedialog.askopenfilename(
filetypes=[
("Word files",
"*.docx")]))]).pack(
side="left")
tk.Label(root, text="Файл со вторым блоком вопросов:", font=("Arial", 10, "bold")).pack(anchor="w", pady=(10, 0))
frame2 = tk.Frame(root)
frame2.pack(fill="x", pady=5)
entry_file2 = tk.Entry(frame2, width=50)
entry_file2.pack(side="left", padx=2)
tk.Button(frame2, text="Обзор", command=lambda: [entry_file2.delete(0, tk.END), entry_file2.insert(0,
filedialog.askopenfilename(
filetypes=[
("Word files",
"*.docx")]))]).pack(
side="left")
btn_frame = tk.Frame(root)
btn_frame.pack(pady=20)
tk.Button(btn_frame, text="СГЕНЕРИРОВАТЬ БИЛЕТ", command=generate_ticket,
bg="#2ecc71", fg="white", font=("Arial", 10, "bold"), padx=10).pack(side="left", padx=5)
btn_save = tk.Button(btn_frame, text="СОХРАНИТЬ В DOCX", command=save_to_file,
state=tk.DISABLED, bg="#3498db", fg="white", font=("Arial", 10, "bold"), padx=10)
btn_save.pack(side="left", padx=5)
tk.Label(root, text="Предварительный просмотр:").pack(anchor="w")
result_text = tk.Text(root, height=12, width=60, wrap=tk.WORD, state=tk.DISABLED, bg="#f9f9f9")
result_text.pack(pady=5)
root.mainloop()