Spaces:
Sleeping
Sleeping
Remove deprecated tkinter desktop app
Browse filesDelete auto_form_generator.py (the tkinter desktop GUI) and drop its
references from README.md and the form_logic.py docstring. The Streamlit
web app is now the only entry point.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- README.md +2 -3
- auto_form_generator.py +0 -449
- form_logic.py +1 -3
README.md
CHANGED
|
@@ -50,9 +50,8 @@ streamlit run streamlit_app.py
|
|
| 50 |
├── README.md # This file (with HF YAML frontmatter)
|
| 51 |
├── requirements.txt # Python dependencies
|
| 52 |
├── streamlit_app.py # Web UI (main entry point)
|
| 53 |
-
├── form_logic.py #
|
| 54 |
-
├──
|
| 55 |
-
├── excel_lookup.py # Excel data lookup helper
|
| 56 |
├── sample_po_data.xlsx # Sample data for demo
|
| 57 |
├── .streamlit/
|
| 58 |
│ └── config.toml # Theme & toolbar config
|
|
|
|
| 50 |
├── README.md # This file (with HF YAML frontmatter)
|
| 51 |
├── requirements.txt # Python dependencies
|
| 52 |
├── streamlit_app.py # Web UI (main entry point)
|
| 53 |
+
├── form_logic.py # Document generation logic
|
| 54 |
+
├── excel_lookup.py # Excel data lookup helper (legacy, unused by web app)
|
|
|
|
| 55 |
├── sample_po_data.xlsx # Sample data for demo
|
| 56 |
├── .streamlit/
|
| 57 |
│ └── config.toml # Theme & toolbar config
|
auto_form_generator.py
DELETED
|
@@ -1,449 +0,0 @@
|
|
| 1 |
-
# -*- coding: utf-8 -*-
|
| 2 |
-
"""
|
| 3 |
-
ระบบสร้างเอกสารอัตโนมัติ (Auto Form Generator) - ปตท.
|
| 4 |
-
=====================================================
|
| 5 |
-
สร้างหนังสือราชการอัตโนมัติจากแบบฟอร์มต้นฉบับ 5 ประเภท
|
| 6 |
-
+ ดึงข้อมูล PO อัตโนมัติจากไฟล์ Excel (รวมถึงไฟล์บน OneDrive/SharePoint)
|
| 7 |
-
|
| 8 |
-
วิธีใช้งาน:
|
| 9 |
-
1. ติดตั้ง: pip install python-docx openpyxl
|
| 10 |
-
2. ตั้งค่า file_path ใน config.ini ให้ชี้ไปที่ไฟล์ Excel
|
| 11 |
-
3. วางแบบฟอร์มต้นฉบับใน templates/form1.docx ... form5.docx
|
| 12 |
-
4. รัน: python auto_form_generator.py
|
| 13 |
-
"""
|
| 14 |
-
|
| 15 |
-
import os
|
| 16 |
-
import sys
|
| 17 |
-
import datetime
|
| 18 |
-
import threading
|
| 19 |
-
import tkinter as tk
|
| 20 |
-
from tkinter import ttk, filedialog, messagebox
|
| 21 |
-
|
| 22 |
-
# Import pure logic จาก form_logic.py (ใช้ร่วมกับ streamlit_app.py)
|
| 23 |
-
try:
|
| 24 |
-
from form_logic import (
|
| 25 |
-
FORM_TYPES, DOC_TYPE_OPTIONS, DISPLAY_FIELDS,
|
| 26 |
-
generate_form,
|
| 27 |
-
)
|
| 28 |
-
except ImportError as e:
|
| 29 |
-
print(f"กรุณาตรวจสอบว่ามีไฟล์ form_logic.py และติดตั้ง python-docx แล้ว: {e}")
|
| 30 |
-
sys.exit(1)
|
| 31 |
-
|
| 32 |
-
# excel_lookup เป็น optional — ถ้า import ไม่ได้ ปุ่มดึงข้อมูลจะ disabled
|
| 33 |
-
try:
|
| 34 |
-
import excel_lookup
|
| 35 |
-
HAS_LOOKUP = True
|
| 36 |
-
except ImportError:
|
| 37 |
-
HAS_LOOKUP = False
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
# =============================================================================
|
| 41 |
-
# GUI Application
|
| 42 |
-
# =============================================================================
|
| 43 |
-
class AutoFormApp:
|
| 44 |
-
BG = "#F5F7FA"
|
| 45 |
-
PRIMARY = "#1F4E79"
|
| 46 |
-
ACCENT = "#2E75B6"
|
| 47 |
-
SUCCESS = "#2E7D32"
|
| 48 |
-
TEXT = "#222222"
|
| 49 |
-
MUTED = "#666666"
|
| 50 |
-
|
| 51 |
-
def __init__(self, root):
|
| 52 |
-
self.root = root
|
| 53 |
-
self.root.title("ระบบสร้างเอกสารอัตโนมัติ - ปตท. (เชื่อมต่อ Excel)")
|
| 54 |
-
self.root.geometry("780x800")
|
| 55 |
-
self.root.configure(bg=self.BG)
|
| 56 |
-
self.root.minsize(720, 740)
|
| 57 |
-
|
| 58 |
-
script_dir = os.path.dirname(os.path.abspath(__file__))
|
| 59 |
-
self.template_dir = tk.StringVar(value=os.path.join(script_dir, "templates"))
|
| 60 |
-
self.output_dir = tk.StringVar(value=os.path.join(script_dir, "output"))
|
| 61 |
-
|
| 62 |
-
self.form_choice = tk.StringVar(value=FORM_TYPES[0]["label"])
|
| 63 |
-
self.doc_number = tk.StringVar()
|
| 64 |
-
self.po_number = tk.StringVar()
|
| 65 |
-
self.doc_type = tk.StringVar(value=DOC_TYPE_OPTIONS[0])
|
| 66 |
-
self.signer = tk.StringVar()
|
| 67 |
-
|
| 68 |
-
# State สำหรับข้อมูลที่ดึงจาก Excel
|
| 69 |
-
self.lookup_data = None
|
| 70 |
-
self.lookup_status_var = tk.StringVar(value="ยังไม่ได้ดึงข้อมูล")
|
| 71 |
-
self.po_number.trace_add("write", lambda *a: self._reset_lookup_state())
|
| 72 |
-
|
| 73 |
-
self._setup_styles()
|
| 74 |
-
self._build_ui()
|
| 75 |
-
self._on_form_change()
|
| 76 |
-
|
| 77 |
-
# -------------------- Styles --------------------
|
| 78 |
-
def _setup_styles(self):
|
| 79 |
-
style = ttk.Style()
|
| 80 |
-
try:
|
| 81 |
-
style.theme_use("clam")
|
| 82 |
-
except tk.TclError:
|
| 83 |
-
pass
|
| 84 |
-
style.configure("TLabel", background=self.BG, foreground=self.TEXT,
|
| 85 |
-
font=("Tahoma", 11))
|
| 86 |
-
style.configure("Header.TLabel", background=self.PRIMARY, foreground="white",
|
| 87 |
-
font=("Tahoma", 16, "bold"), padding=12)
|
| 88 |
-
style.configure("SubHeader.TLabel", background=self.BG, foreground=self.MUTED,
|
| 89 |
-
font=("Tahoma", 10))
|
| 90 |
-
style.configure("Field.TLabel", background=self.BG, foreground=self.TEXT,
|
| 91 |
-
font=("Tahoma", 11, "bold"))
|
| 92 |
-
style.configure("TEntry", padding=6, fieldbackground="white")
|
| 93 |
-
style.configure("TCombobox", padding=4, fieldbackground="white")
|
| 94 |
-
style.configure("Primary.TButton", background=self.ACCENT, foreground="white",
|
| 95 |
-
font=("Tahoma", 11, "bold"), padding=(20, 10), borderwidth=0)
|
| 96 |
-
style.map("Primary.TButton",
|
| 97 |
-
background=[("active", self.PRIMARY), ("pressed", self.PRIMARY)])
|
| 98 |
-
style.configure("Secondary.TButton", background="#E0E0E0",
|
| 99 |
-
foreground=self.TEXT, font=("Tahoma", 10),
|
| 100 |
-
padding=(14, 8), borderwidth=0)
|
| 101 |
-
style.map("Secondary.TButton", background=[("active", "#CCCCCC")])
|
| 102 |
-
style.configure("Fetch.TButton", background=self.SUCCESS, foreground="white",
|
| 103 |
-
font=("Tahoma", 10, "bold"), padding=(10, 6), borderwidth=0)
|
| 104 |
-
style.map("Fetch.TButton", background=[("active", "#1B5E20")])
|
| 105 |
-
style.configure("TRadiobutton", background=self.BG, foreground=self.TEXT,
|
| 106 |
-
font=("Tahoma", 11))
|
| 107 |
-
|
| 108 |
-
# -------------------- UI Layout --------------------
|
| 109 |
-
def _build_ui(self):
|
| 110 |
-
header = ttk.Label(self.root,
|
| 111 |
-
text="ระบบสร้างเอกสารอัตโนมัติ - ปตท.",
|
| 112 |
-
style="Header.TLabel", anchor="center")
|
| 113 |
-
header.pack(fill="x")
|
| 114 |
-
ttk.Label(self.root,
|
| 115 |
-
text="กรอกเลข PO แล้วกด \"ดึงข้อมูล\" "
|
| 116 |
-
"เพื่อเติมข้อมูลอัตโนมัติจาก Excel",
|
| 117 |
-
style="SubHeader.TLabel", anchor="center").pack(pady=(8, 12))
|
| 118 |
-
|
| 119 |
-
main = tk.Frame(self.root, bg=self.BG)
|
| 120 |
-
main.pack(fill="both", expand=True, padx=24, pady=(0, 12))
|
| 121 |
-
main.columnconfigure(1, weight=1)
|
| 122 |
-
row = 0
|
| 123 |
-
|
| 124 |
-
# ----- ประเภทแบบฟอร์ม -----
|
| 125 |
-
ttk.Label(main, text="ประเภทแบบฟอร์ม:", style="Field.TLabel")\
|
| 126 |
-
.grid(row=row, column=0, sticky="w", pady=(6, 4))
|
| 127 |
-
form_combo = ttk.Combobox(
|
| 128 |
-
main, textvariable=self.form_choice,
|
| 129 |
-
values=[f["label"] for f in FORM_TYPES],
|
| 130 |
-
state="readonly", width=60, font=("Tahoma", 10))
|
| 131 |
-
form_combo.grid(row=row, column=1, sticky="ew", pady=(6, 4))
|
| 132 |
-
form_combo.bind("<<ComboboxSelected>>", lambda e: self._on_form_change())
|
| 133 |
-
row += 1
|
| 134 |
-
|
| 135 |
-
ttk.Separator(main, orient="horizontal")\
|
| 136 |
-
.grid(row=row, column=0, columnspan=2, sticky="ew", pady=10)
|
| 137 |
-
row += 1
|
| 138 |
-
|
| 139 |
-
# ----- เลข PO + ปุ่มดึงข้อมูล -----
|
| 140 |
-
ttk.Label(main, text="เลข PO: *", style="Field.TLabel")\
|
| 141 |
-
.grid(row=row, column=0, sticky="w", pady=6)
|
| 142 |
-
po_frame = tk.Frame(main, bg=self.BG)
|
| 143 |
-
po_frame.grid(row=row, column=1, sticky="ew", pady=6)
|
| 144 |
-
po_frame.columnconfigure(0, weight=1)
|
| 145 |
-
self.po_entry = ttk.Entry(po_frame, textvariable=self.po_number,
|
| 146 |
-
font=("Tahoma", 11))
|
| 147 |
-
self.po_entry.grid(row=0, column=0, sticky="ew")
|
| 148 |
-
self.fetch_btn = ttk.Button(po_frame, text="ดึงข้อมูลจาก Excel",
|
| 149 |
-
style="Fetch.TButton",
|
| 150 |
-
command=self._fetch_data)
|
| 151 |
-
self.fetch_btn.grid(row=0, column=1, padx=(8, 0))
|
| 152 |
-
if not HAS_LOOKUP:
|
| 153 |
-
self.fetch_btn.configure(state="disabled")
|
| 154 |
-
row += 1
|
| 155 |
-
|
| 156 |
-
ttk.Label(main, textvariable=self.lookup_status_var,
|
| 157 |
-
style="SubHeader.TLabel")\
|
| 158 |
-
.grid(row=row, column=1, sticky="w")
|
| 159 |
-
row += 1
|
| 160 |
-
|
| 161 |
-
# ----- หมายเลขเอกสาร -----
|
| 162 |
-
ttk.Label(main, text="หมายเลขเอกสาร: *", style="Field.TLabel")\
|
| 163 |
-
.grid(row=row, column=0, sticky="w", pady=6)
|
| 164 |
-
ttk.Entry(main, textvariable=self.doc_number, font=("Tahoma", 11))\
|
| 165 |
-
.grid(row=row, column=1, sticky="ew", pady=6)
|
| 166 |
-
row += 1
|
| 167 |
-
ttk.Label(main, text="ตัวอย่าง: PTT-001/2568",
|
| 168 |
-
style="SubHeader.TLabel")\
|
| 169 |
-
.grid(row=row, column=1, sticky="w")
|
| 170 |
-
row += 1
|
| 171 |
-
|
| 172 |
-
# ----- ประเภทเอกสาร -----
|
| 173 |
-
self.type_label = ttk.Label(main, text="ประเภทเอกสาร:",
|
| 174 |
-
style="Field.TLabel")
|
| 175 |
-
self.type_label.grid(row=row, column=0, sticky="nw", pady=6)
|
| 176 |
-
self.type_frame = tk.Frame(main, bg=self.BG)
|
| 177 |
-
self.type_frame.grid(row=row, column=1, sticky="w", pady=6)
|
| 178 |
-
for i, opt in enumerate(DOC_TYPE_OPTIONS):
|
| 179 |
-
ttk.Radiobutton(self.type_frame, text=opt,
|
| 180 |
-
variable=self.doc_type, value=opt)\
|
| 181 |
-
.grid(row=0, column=i, padx=(0, 20), sticky="w")
|
| 182 |
-
row += 1
|
| 183 |
-
|
| 184 |
-
# ----- ผู้ลงนาม -----
|
| 185 |
-
ttk.Label(main, text="ผู้ลงนาม: *", style="Field.TLabel")\
|
| 186 |
-
.grid(row=row, column=0, sticky="w", pady=6)
|
| 187 |
-
ttk.Entry(main, textvariable=self.signer, font=("Tahoma", 11))\
|
| 188 |
-
.grid(row=row, column=1, sticky="ew", pady=6)
|
| 189 |
-
row += 1
|
| 190 |
-
ttk.Label(main,
|
| 191 |
-
text="(ผู้มีอำนาจอนุมัติ หรือ ประธานกรรมการตรวจรับ)",
|
| 192 |
-
style="SubHeader.TLabel")\
|
| 193 |
-
.grid(row=row, column=1, sticky="w")
|
| 194 |
-
row += 1
|
| 195 |
-
|
| 196 |
-
ttk.Separator(main, orient="horizontal")\
|
| 197 |
-
.grid(row=row, column=0, columnspan=2, sticky="ew", pady=10)
|
| 198 |
-
row += 1
|
| 199 |
-
|
| 200 |
-
# ----- ข้อมูลที่ดึงจาก Excel (preview) -----
|
| 201 |
-
ttk.Label(main, text="ข้อมูลจาก Excel:", style="Field.TLabel")\
|
| 202 |
-
.grid(row=row, column=0, sticky="nw", pady=6)
|
| 203 |
-
preview_frame = tk.Frame(main, bg="white",
|
| 204 |
-
highlightbackground="#CCCCCC",
|
| 205 |
-
highlightthickness=1)
|
| 206 |
-
preview_frame.grid(row=row, column=1, sticky="ew", pady=6)
|
| 207 |
-
self.preview_text = tk.Text(
|
| 208 |
-
preview_frame, height=8, font=("Tahoma", 10),
|
| 209 |
-
bg="white", fg=self.TEXT, relief="flat",
|
| 210 |
-
wrap="word", padx=10, pady=8, state="disabled")
|
| 211 |
-
self.preview_text.pack(fill="both", expand=True)
|
| 212 |
-
self._update_preview(None)
|
| 213 |
-
row += 1
|
| 214 |
-
|
| 215 |
-
# ----- โฟลเดอร์ -----
|
| 216 |
-
ttk.Label(main, text="โฟลเดอร์ Template:", style="Field.TLabel")\
|
| 217 |
-
.grid(row=row, column=0, sticky="w", pady=4)
|
| 218 |
-
tpl_frame = tk.Frame(main, bg=self.BG)
|
| 219 |
-
tpl_frame.grid(row=row, column=1, sticky="ew", pady=4)
|
| 220 |
-
tpl_frame.columnconfigure(0, weight=1)
|
| 221 |
-
ttk.Entry(tpl_frame, textvariable=self.template_dir,
|
| 222 |
-
font=("Tahoma", 10)).grid(row=0, column=0, sticky="ew")
|
| 223 |
-
ttk.Button(tpl_frame, text="เลือก...", style="Secondary.TButton",
|
| 224 |
-
command=lambda: self._browse_dir(self.template_dir))\
|
| 225 |
-
.grid(row=0, column=1, padx=(8, 0))
|
| 226 |
-
row += 1
|
| 227 |
-
|
| 228 |
-
ttk.Label(main, text="โฟลเดอร์บันทึก:", style="Field.TLabel")\
|
| 229 |
-
.grid(row=row, column=0, sticky="w", pady=4)
|
| 230 |
-
out_frame = tk.Frame(main, bg=self.BG)
|
| 231 |
-
out_frame.grid(row=row, column=1, sticky="ew", pady=4)
|
| 232 |
-
out_frame.columnconfigure(0, weight=1)
|
| 233 |
-
ttk.Entry(out_frame, textvariable=self.output_dir,
|
| 234 |
-
font=("Tahoma", 10)).grid(row=0, column=0, sticky="ew")
|
| 235 |
-
ttk.Button(out_frame, text="เลือก...", style="Secondary.TButton",
|
| 236 |
-
command=lambda: self._browse_dir(self.output_dir))\
|
| 237 |
-
.grid(row=0, column=1, padx=(8, 0))
|
| 238 |
-
row += 1
|
| 239 |
-
|
| 240 |
-
# ----- ปุ่มดำเนินการ -----
|
| 241 |
-
btn_frame = tk.Frame(self.root, bg=self.BG)
|
| 242 |
-
btn_frame.pack(fill="x", padx=24, pady=(4, 12))
|
| 243 |
-
ttk.Button(btn_frame, text="ล้างข้อมูล", style="Secondary.TButton",
|
| 244 |
-
command=self._clear_form).pack(side="left")
|
| 245 |
-
ttk.Button(btn_frame, text="สร้างเอกสาร", style="Primary.TButton",
|
| 246 |
-
command=self._generate).pack(side="right")
|
| 247 |
-
|
| 248 |
-
# ----- Status bar -----
|
| 249 |
-
self.status_var = tk.StringVar(value="พร้อมใช้งาน")
|
| 250 |
-
if not HAS_LOOKUP:
|
| 251 |
-
self.status_var.set(
|
| 252 |
-
"พร้อมใช้งาน (Excel: ไม่พบ excel_lookup.py หรือ openpyxl)")
|
| 253 |
-
status_bar = tk.Label(self.root, textvariable=self.status_var,
|
| 254 |
-
bg="#E8EEF4", fg=self.PRIMARY, anchor="w",
|
| 255 |
-
font=("Tahoma", 10), padx=12, pady=8)
|
| 256 |
-
status_bar.pack(fill="x", side="bottom")
|
| 257 |
-
|
| 258 |
-
# -------------------- Helpers --------------------
|
| 259 |
-
def _get_selected_form(self):
|
| 260 |
-
for f in FORM_TYPES:
|
| 261 |
-
if f["label"] == self.form_choice.get():
|
| 262 |
-
return f
|
| 263 |
-
return FORM_TYPES[0]
|
| 264 |
-
|
| 265 |
-
def _on_form_change(self):
|
| 266 |
-
form = self._get_selected_form()
|
| 267 |
-
for child in self.type_frame.winfo_children():
|
| 268 |
-
child.configure(state="normal" if form["uses_doc_type"] else "disabled")
|
| 269 |
-
self.type_label.configure(
|
| 270 |
-
foreground=self.TEXT if form["uses_doc_type"] else "#999999")
|
| 271 |
-
|
| 272 |
-
def _browse_dir(self, var):
|
| 273 |
-
initial = var.get() if os.path.isdir(var.get()) else os.getcwd()
|
| 274 |
-
chosen = filedialog.askdirectory(initialdir=initial,
|
| 275 |
-
title="เลือกโฟลเดอร์")
|
| 276 |
-
if chosen:
|
| 277 |
-
var.set(chosen)
|
| 278 |
-
|
| 279 |
-
def _clear_form(self):
|
| 280 |
-
self.doc_number.set("")
|
| 281 |
-
self.po_number.set("")
|
| 282 |
-
self.signer.set("")
|
| 283 |
-
self.doc_type.set(DOC_TYPE_OPTIONS[0])
|
| 284 |
-
self._reset_lookup_state()
|
| 285 |
-
self.status_var.set("ล้างข้อมูลเรียบร้อย")
|
| 286 |
-
|
| 287 |
-
def _reset_lookup_state(self):
|
| 288 |
-
self.lookup_data = None
|
| 289 |
-
self.lookup_status_var.set("ยังไม่ได้ดึงข้อมูล")
|
| 290 |
-
self._update_preview(None)
|
| 291 |
-
|
| 292 |
-
def _update_preview(self, data):
|
| 293 |
-
self.preview_text.configure(state="normal")
|
| 294 |
-
self.preview_text.delete("1.0", "end")
|
| 295 |
-
if not data:
|
| 296 |
-
self.preview_text.insert("1.0",
|
| 297 |
-
"(กด \"ดึงข้อมูลจาก Excel\" เพื่อค้นหาข้อมูล PO)")
|
| 298 |
-
else:
|
| 299 |
-
lines = []
|
| 300 |
-
for col, label in DISPLAY_FIELDS:
|
| 301 |
-
val = data.get(col)
|
| 302 |
-
if val not in (None, ""):
|
| 303 |
-
lines.append(f"• {label}: {val}")
|
| 304 |
-
extras = [k for k in data.keys()
|
| 305 |
-
if k not in {c for c, _ in DISPLAY_FIELDS}
|
| 306 |
-
and data[k] not in (None, "")]
|
| 307 |
-
if extras:
|
| 308 |
-
lines.append(f"\n(พบเพิ่มเติมอีก {len(extras)} columns)")
|
| 309 |
-
self.preview_text.insert("1.0",
|
| 310 |
-
"\n".join(lines) if lines else "(พบ row แต่ค่าทั้งหมดว่าง)")
|
| 311 |
-
self.preview_text.configure(state="disabled")
|
| 312 |
-
|
| 313 |
-
# -------------------- Data fetch --------------------
|
| 314 |
-
def _fetch_data(self):
|
| 315 |
-
po = self.po_number.get().strip()
|
| 316 |
-
if not po:
|
| 317 |
-
messagebox.showwarning("ข้อมูลไม่ครบ", "กรุณากรอกเลข PO ก่อน")
|
| 318 |
-
return
|
| 319 |
-
if not HAS_LOOKUP:
|
| 320 |
-
messagebox.showerror(
|
| 321 |
-
"Excel lookup ไม่พร้อมใช้งาน",
|
| 322 |
-
"ไม่พบโมดูล excel_lookup\n"
|
| 323 |
-
"กรุณาตรวจสอบว่ามีไฟล์ excel_lookup.py "
|
| 324 |
-
"และติดตั้ง: pip install openpyxl")
|
| 325 |
-
return
|
| 326 |
-
|
| 327 |
-
self.fetch_btn.configure(state="disabled")
|
| 328 |
-
self.lookup_status_var.set("⏳ กำลังอ่านไฟล์ Excel...")
|
| 329 |
-
self.status_var.set(f"กำลังค้นหา PO: {po}")
|
| 330 |
-
self.root.update_idletasks()
|
| 331 |
-
|
| 332 |
-
threading.Thread(target=self._fetch_worker, args=(po,),
|
| 333 |
-
daemon=True).start()
|
| 334 |
-
|
| 335 |
-
def _fetch_worker(self, po):
|
| 336 |
-
try:
|
| 337 |
-
data = excel_lookup.lookup_po(po)
|
| 338 |
-
self.root.after(0, self._fetch_done, po, data, None)
|
| 339 |
-
except Exception as exc:
|
| 340 |
-
self.root.after(0, self._fetch_done, po, None, str(exc))
|
| 341 |
-
|
| 342 |
-
def _fetch_done(self, po, data, error):
|
| 343 |
-
self.fetch_btn.configure(state="normal")
|
| 344 |
-
if error:
|
| 345 |
-
self.lookup_status_var.set("❌ ดึงข้อมูลไม่สำเร็จ")
|
| 346 |
-
self.status_var.set("Excel: ผิดพลาด")
|
| 347 |
-
messagebox.showerror("เกิดข้อผิดพลาด",
|
| 348 |
-
f"ไม่สามารถดึงข้อมูลได้:\n\n{error}")
|
| 349 |
-
return
|
| 350 |
-
if not data:
|
| 351 |
-
self.lookup_data = None
|
| 352 |
-
self.lookup_status_var.set(f"⚠ ไม่พบ PO: {po}")
|
| 353 |
-
self.status_var.set(f"ไม่พบ PO {po} ในไฟล์ Excel")
|
| 354 |
-
self._update_preview(None)
|
| 355 |
-
messagebox.showinfo("ไม่พบข้อมูล",
|
| 356 |
-
f"ไม่พบ PO หมายเลข {po} ในไฟล์ Excel")
|
| 357 |
-
return
|
| 358 |
-
self.lookup_data = data
|
| 359 |
-
vendor = data.get("VendorName", "")
|
| 360 |
-
self.lookup_status_var.set(
|
| 361 |
-
f"✓ พบข้อมูล: {vendor or '(no vendor)'} — "
|
| 362 |
-
f"จะเติม Vendor / ชื่องาน อัตโนมัติตอนสร้างเอกสาร")
|
| 363 |
-
self.status_var.set(f"ดึงข้อมูล PO {po} สำเร็จ")
|
| 364 |
-
self._update_preview(data)
|
| 365 |
-
|
| 366 |
-
# -------------------- Generate --------------------
|
| 367 |
-
def _validate(self, form):
|
| 368 |
-
if not self.doc_number.get().strip():
|
| 369 |
-
messagebox.showwarning("ข้อมูลไม่ครบ", "กรุณากรอกหมายเลขเอกสาร")
|
| 370 |
-
return False
|
| 371 |
-
if not self.signer.get().strip():
|
| 372 |
-
messagebox.showwarning("ข้อมูลไม่ครบ", "กรุณากรอกชื่อผู้ลงนาม")
|
| 373 |
-
return False
|
| 374 |
-
if form["uses_po"] and not self.po_number.get().strip():
|
| 375 |
-
messagebox.showwarning("ข้อมูลไม่ครบ", "กรุณากรอกเลข PO")
|
| 376 |
-
return False
|
| 377 |
-
if not os.path.isdir(self.template_dir.get()):
|
| 378 |
-
messagebox.showerror("ไม่พบโฟลเดอร์ Template",
|
| 379 |
-
f"ไม่พบโฟลเดอร์: {self.template_dir.get()}")
|
| 380 |
-
return False
|
| 381 |
-
template_path = os.path.join(self.template_dir.get(), form["filename"])
|
| 382 |
-
if not os.path.isfile(template_path):
|
| 383 |
-
messagebox.showerror("ไม่พบไฟล์ Template",
|
| 384 |
-
f"ไม่พบ {form['filename']} ในโฟลเดอร์:\n{self.template_dir.get()}")
|
| 385 |
-
return False
|
| 386 |
-
return True
|
| 387 |
-
|
| 388 |
-
def _generate(self):
|
| 389 |
-
form = self._get_selected_form()
|
| 390 |
-
if not self._validate(form):
|
| 391 |
-
return
|
| 392 |
-
|
| 393 |
-
os.makedirs(self.output_dir.get(), exist_ok=True)
|
| 394 |
-
template_path = os.path.join(self.template_dir.get(), form["filename"])
|
| 395 |
-
|
| 396 |
-
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 397 |
-
safe_doc_num = (self.doc_number.get()
|
| 398 |
-
.replace("/", "-").replace("\\", "-").strip())
|
| 399 |
-
out_filename = (f"form{form['id']}_{safe_doc_num}_{timestamp}.docx"
|
| 400 |
-
if safe_doc_num else f"form{form['id']}_{timestamp}.docx")
|
| 401 |
-
output_path = os.path.join(self.output_dir.get(), out_filename)
|
| 402 |
-
|
| 403 |
-
try:
|
| 404 |
-
log = generate_form(
|
| 405 |
-
form_id=form["id"],
|
| 406 |
-
template_path=template_path,
|
| 407 |
-
output_path=output_path,
|
| 408 |
-
doc_number=self.doc_number.get().strip(),
|
| 409 |
-
po_number=self.po_number.get().strip(),
|
| 410 |
-
doc_type=self.doc_type.get() if form["uses_doc_type"] else "",
|
| 411 |
-
signer=self.signer.get().strip(),
|
| 412 |
-
lookup_data=self.lookup_data,
|
| 413 |
-
)
|
| 414 |
-
except Exception as exc:
|
| 415 |
-
messagebox.showerror("เกิดข้อผิดพลาด",
|
| 416 |
-
f"ไม่สามารถสร้างเอกสารได้:\n{exc}")
|
| 417 |
-
self.status_var.set(f"เกิดข้อผิดพลาด: {exc}")
|
| 418 |
-
return
|
| 419 |
-
|
| 420 |
-
self.status_var.set(f"สร้างเอกสารเรียบร้อย: {out_filename}")
|
| 421 |
-
if messagebox.askyesno("สร้างเอกสารสำเร็จ",
|
| 422 |
-
f"บันทึกไฟล์ที่:\n{output_path}\n\n"
|
| 423 |
-
f"รายละเอียด:\n• " + "\n• ".join(log) + "\n\n"
|
| 424 |
-
f"ต้องการเปิดโฟลเดอร์ผลลัพธ์หรือไม่?"):
|
| 425 |
-
self._open_folder(self.output_dir.get())
|
| 426 |
-
|
| 427 |
-
def _open_folder(self, path):
|
| 428 |
-
try:
|
| 429 |
-
if sys.platform.startswith("win"):
|
| 430 |
-
os.startfile(path)
|
| 431 |
-
elif sys.platform == "darwin":
|
| 432 |
-
os.system(f'open "{path}"')
|
| 433 |
-
else:
|
| 434 |
-
os.system(f'xdg-open "{path}"')
|
| 435 |
-
except Exception:
|
| 436 |
-
pass
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
# =============================================================================
|
| 440 |
-
# Entry point
|
| 441 |
-
# =============================================================================
|
| 442 |
-
def main():
|
| 443 |
-
root = tk.Tk()
|
| 444 |
-
AutoFormApp(root)
|
| 445 |
-
root.mainloop()
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
if __name__ == "__main__":
|
| 449 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
form_logic.py
CHANGED
|
@@ -2,9 +2,7 @@
|
|
| 2 |
"""
|
| 3 |
Form Logic — pure logic for document generation (no UI dependencies)
|
| 4 |
=====================================================================
|
| 5 |
-
ใช้
|
| 6 |
-
- auto_form_generator.py (Desktop GUI ผ่าน tkinter)
|
| 7 |
-
- streamlit_app.py (Web app ผ่าน Streamlit)
|
| 8 |
ไม่ import tkinter หรือ streamlit เลย ใช้แค่ python-docx
|
| 9 |
"""
|
| 10 |
|
|
|
|
| 2 |
"""
|
| 3 |
Form Logic — pure logic for document generation (no UI dependencies)
|
| 4 |
=====================================================================
|
| 5 |
+
ใช้โดย streamlit_app.py (Web app ผ่าน Streamlit)
|
|
|
|
|
|
|
| 6 |
ไม่ import tkinter หรือ streamlit เลย ใช้แค่ python-docx
|
| 7 |
"""
|
| 8 |
|