|
@@ -0,0 +1,312 @@
|
|
|
|
|
+#!/usr/bin/env python3
|
|
|
|
|
+# -*- coding: utf-8 -*-
|
|
|
|
|
+"""
|
|
|
|
|
+PDF 条码合成工具(带缩放与偏移配置)
|
|
|
|
|
+"""
|
|
|
|
|
+
|
|
|
|
|
+import tkinter as tk
|
|
|
|
|
+from tkinter import ttk, filedialog, scrolledtext
|
|
|
|
|
+import os
|
|
|
|
|
+import json
|
|
|
|
|
+import re
|
|
|
|
|
+import threading
|
|
|
|
|
+import fitz # PyMuPDF
|
|
|
|
|
+from PIL import Image
|
|
|
|
|
+
|
|
|
|
|
+CONFIG_FILE = "pdf_merge_config.json"
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class PDFMergeApp:
|
|
|
|
|
+ def __init__(self, root):
|
|
|
|
|
+ self.root = root
|
|
|
|
|
+ self.root.title("PDF 条码合成工具")
|
|
|
|
|
+ self.root.geometry("780x600")
|
|
|
|
|
+ self.root.minsize(700, 500)
|
|
|
|
|
+
|
|
|
|
|
+ # 路径变量
|
|
|
|
|
+ self.input_var = tk.StringVar()
|
|
|
|
|
+ self.template_var = tk.StringVar()
|
|
|
|
|
+ self.output_var = tk.StringVar()
|
|
|
|
|
+
|
|
|
|
|
+ # 缩放与偏移变量
|
|
|
|
|
+ self.scale_var = tk.StringVar(value="100") # 相对于模板宽度的百分比
|
|
|
|
|
+ self.offset_x_var = tk.StringVar(value="0") # X 偏移(点)
|
|
|
|
|
+ self.offset_y_var = tk.StringVar(value="10") # Y 偏移(点)
|
|
|
|
|
+
|
|
|
|
|
+ self.build_ui()
|
|
|
|
|
+ self.load_config()
|
|
|
|
|
+
|
|
|
|
|
+ def build_ui(self):
|
|
|
|
|
+ main_frame = ttk.Frame(self.root, padding="12")
|
|
|
|
|
+ main_frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
|
|
|
|
|
+
|
|
|
|
|
+ self.root.columnconfigure(0, weight=1)
|
|
|
|
|
+ self.root.rowconfigure(0, weight=1)
|
|
|
|
|
+ main_frame.columnconfigure(1, weight=1)
|
|
|
|
|
+
|
|
|
|
|
+ # 输入文件夹
|
|
|
|
|
+ ttk.Label(main_frame, text="输入文件夹:").grid(row=0, column=0, sticky=tk.W, pady=6)
|
|
|
|
|
+ ttk.Entry(main_frame, textvariable=self.input_var).grid(row=0, column=1, sticky=(tk.W, tk.E), padx=6)
|
|
|
|
|
+ ttk.Button(main_frame, text="浏览…", width=10, command=self.select_input).grid(row=0, column=2, padx=4)
|
|
|
|
|
+
|
|
|
|
|
+ # 模板文件
|
|
|
|
|
+ ttk.Label(main_frame, text="模板文件:").grid(row=1, column=0, sticky=tk.W, pady=6)
|
|
|
|
|
+ ttk.Entry(main_frame, textvariable=self.template_var).grid(row=1, column=1, sticky=(tk.W, tk.E), padx=6)
|
|
|
|
|
+ ttk.Button(main_frame, text="浏览…", width=10, command=self.select_template).grid(row=1, column=2, padx=4)
|
|
|
|
|
+
|
|
|
|
|
+ # 输出文件夹
|
|
|
|
|
+ ttk.Label(main_frame, text="输出文件夹:").grid(row=2, column=0, sticky=tk.W, pady=6)
|
|
|
|
|
+ ttk.Entry(main_frame, textvariable=self.output_var).grid(row=2, column=1, sticky=(tk.W, tk.E), padx=6)
|
|
|
|
|
+ ttk.Button(main_frame, text="浏览…", width=10, command=self.select_output).grid(row=2, column=2, padx=4)
|
|
|
|
|
+
|
|
|
|
|
+ # 参数配置区域
|
|
|
|
|
+ param_frame = ttk.LabelFrame(main_frame, text="合成参数(自动保存)", padding="10")
|
|
|
|
|
+ param_frame.grid(row=3, column=0, columnspan=3, sticky=(tk.W, tk.E), pady=10)
|
|
|
|
|
+
|
|
|
|
|
+ ttk.Label(param_frame, text="缩放比例(% 模板宽):").grid(row=0, column=0, sticky=tk.W, padx=4)
|
|
|
|
|
+ scale_entry = ttk.Entry(param_frame, textvariable=self.scale_var, width=10)
|
|
|
|
|
+ scale_entry.grid(row=0, column=1, sticky=tk.W, padx=4)
|
|
|
|
|
+ ttk.Label(param_frame, text="例:100 = 宽度填满模板").grid(row=0, column=2, sticky=tk.W, padx=4)
|
|
|
|
|
+
|
|
|
|
|
+ ttk.Label(param_frame, text="X 偏移(点):").grid(row=1, column=0, sticky=tk.W, padx=4, pady=6)
|
|
|
|
|
+ ttk.Entry(param_frame, textvariable=self.offset_x_var, width=10).grid(row=1, column=1, sticky=tk.W, padx=4, pady=6)
|
|
|
|
|
+ ttk.Label(param_frame, text="左上角水平偏移,0 表示贴左边").grid(row=1, column=2, sticky=tk.W, padx=4, pady=6)
|
|
|
|
|
+
|
|
|
|
|
+ ttk.Label(param_frame, text="Y 偏移(点):").grid(row=2, column=0, sticky=tk.W, padx=4)
|
|
|
|
|
+ ttk.Entry(param_frame, textvariable=self.offset_y_var, width=10).grid(row=2, column=1, sticky=tk.W, padx=4)
|
|
|
|
|
+ ttk.Label(param_frame, text="左上角垂直偏移,0 表示贴顶部").grid(row=2, column=2, sticky=tk.W, padx=4)
|
|
|
|
|
+
|
|
|
|
|
+ # 开始按钮
|
|
|
|
|
+ btn_frame = ttk.Frame(main_frame)
|
|
|
|
|
+ btn_frame.grid(row=4, column=0, columnspan=3, pady=10)
|
|
|
|
|
+ self.start_btn = ttk.Button(btn_frame, text="▶ 开始处理", command=self.start_process)
|
|
|
|
|
+ self.start_btn.pack()
|
|
|
|
|
+
|
|
|
|
|
+ # 日志区域
|
|
|
|
|
+ ttk.Label(main_frame, text="处理日志:").grid(row=5, column=0, sticky=tk.W, pady=(8, 0))
|
|
|
|
|
+ self.log_text = scrolledtext.ScrolledText(
|
|
|
|
|
+ main_frame, height=18, wrap=tk.WORD, state=tk.NORMAL, font=("Consolas", 10)
|
|
|
|
|
+ )
|
|
|
|
|
+ self.log_text.grid(row=6, column=0, columnspan=3, sticky=(tk.W, tk.E, tk.N, tk.S), pady=4)
|
|
|
|
|
+ main_frame.rowconfigure(6, weight=1)
|
|
|
|
|
+
|
|
|
|
|
+ # 绑定参数变更自动保存
|
|
|
|
|
+ for var in (self.scale_var, self.offset_x_var, self.offset_y_var):
|
|
|
|
|
+ var.trace_add("write", lambda *args: self.save_config())
|
|
|
|
|
+
|
|
|
|
|
+ # ---------- 路径选择 ----------
|
|
|
|
|
+ def select_input(self):
|
|
|
|
|
+ folder = filedialog.askdirectory()
|
|
|
|
|
+ if folder:
|
|
|
|
|
+ self.input_var.set(folder)
|
|
|
|
|
+ self.save_config()
|
|
|
|
|
+
|
|
|
|
|
+ def select_template(self):
|
|
|
|
|
+ file_path = filedialog.askopenfilename(filetypes=[("PDF 文件", "*.pdf")])
|
|
|
|
|
+ if file_path:
|
|
|
|
|
+ self.template_var.set(file_path)
|
|
|
|
|
+ self.save_config()
|
|
|
|
|
+
|
|
|
|
|
+ def select_output(self):
|
|
|
|
|
+ folder = filedialog.askdirectory()
|
|
|
|
|
+ if folder:
|
|
|
|
|
+ self.output_var.set(folder)
|
|
|
|
|
+ self.save_config()
|
|
|
|
|
+
|
|
|
|
|
+ # ---------- 配置持久化 ----------
|
|
|
|
|
+ def save_config(self):
|
|
|
|
|
+ config = {
|
|
|
|
|
+ "input_dir": self.input_var.get(),
|
|
|
|
|
+ "template": self.template_var.get(),
|
|
|
|
|
+ "output_dir": self.output_var.get(),
|
|
|
|
|
+ "scale_percent": self.scale_var.get(),
|
|
|
|
|
+ "offset_x": self.offset_x_var.get(),
|
|
|
|
|
+ "offset_y": self.offset_y_var.get(),
|
|
|
|
|
+ }
|
|
|
|
|
+ try:
|
|
|
|
|
+ with open(CONFIG_FILE, "w", encoding="utf-8") as f:
|
|
|
|
|
+ json.dump(config, f, ensure_ascii=False, indent=2)
|
|
|
|
|
+ except Exception:
|
|
|
|
|
+ pass
|
|
|
|
|
+
|
|
|
|
|
+ def load_config(self):
|
|
|
|
|
+ if not os.path.exists(CONFIG_FILE):
|
|
|
|
|
+ return
|
|
|
|
|
+ try:
|
|
|
|
|
+ with open(CONFIG_FILE, "r", encoding="utf-8") as f:
|
|
|
|
|
+ config = json.load(f)
|
|
|
|
|
+ self.input_var.set(config.get("input_dir", ""))
|
|
|
|
|
+ self.template_var.set(config.get("template", ""))
|
|
|
|
|
+ self.output_var.set(config.get("output_dir", ""))
|
|
|
|
|
+ self.scale_var.set(config.get("scale_percent", "100"))
|
|
|
|
|
+ self.offset_x_var.set(config.get("offset_x", "0"))
|
|
|
|
|
+ self.offset_y_var.set(config.get("offset_y", "0"))
|
|
|
|
|
+ except Exception:
|
|
|
|
|
+ pass
|
|
|
|
|
+
|
|
|
|
|
+ # ---------- 日志 ----------
|
|
|
|
|
+ def log(self, msg):
|
|
|
|
|
+ self.log_text.insert(tk.END, msg + "\n")
|
|
|
|
|
+ self.log_text.see(tk.END)
|
|
|
|
|
+ self.root.update_idletasks()
|
|
|
|
|
+
|
|
|
|
|
+ # ---------- 文字提取 ----------
|
|
|
|
|
+ def extract_text(self, page):
|
|
|
|
|
+ text = page.get_text().strip()
|
|
|
|
|
+ print(f"Extracted text: {text}") # Debug print
|
|
|
|
|
+ if len(text) >= 2:
|
|
|
|
|
+ return text
|
|
|
|
|
+ try:
|
|
|
|
|
+ import pytesseract
|
|
|
|
|
+ pix = page.get_pixmap(dpi=300)
|
|
|
|
|
+ mode = "RGBA" if pix.alpha else "RGB"
|
|
|
|
|
+ img = Image.frombytes(mode, [pix.width, pix.height], pix.samples)
|
|
|
|
|
+ if mode == "RGBA":
|
|
|
|
|
+ img = img.convert("RGB")
|
|
|
|
|
+ text = pytesseract.image_to_string(img, lang="chi_sim+eng").strip()
|
|
|
|
|
+ return text
|
|
|
|
|
+ except Exception:
|
|
|
|
|
+ return ""
|
|
|
|
|
+
|
|
|
|
|
+ def sanitize_filename(self, text):
|
|
|
|
|
+ if not text:
|
|
|
|
|
+ return ""
|
|
|
|
|
+ lines = [ln.strip() for ln in text.splitlines() if ln.strip()]
|
|
|
|
|
+ name = lines[0] if lines else text
|
|
|
|
|
+ name = name[:60]
|
|
|
|
|
+ name = re.sub(r'[\\/:*?"<>|\s]+', "_", name)
|
|
|
|
|
+ return name.strip("._")
|
|
|
|
|
+
|
|
|
|
|
+ # ---------- 核心处理 ----------
|
|
|
|
|
+ def start_process(self):
|
|
|
|
|
+ t = threading.Thread(target=self.process, daemon=True)
|
|
|
|
|
+ t.start()
|
|
|
|
|
+
|
|
|
|
|
+ def process(self):
|
|
|
|
|
+ self.start_btn.config(state=tk.DISABLED)
|
|
|
|
|
+ self.log_text.delete(1.0, tk.END)
|
|
|
|
|
+
|
|
|
|
|
+ input_dir = self.input_var.get()
|
|
|
|
|
+ template_file = self.template_var.get()
|
|
|
|
|
+ output_dir = self.output_var.get()
|
|
|
|
|
+
|
|
|
|
|
+ # 解析参数
|
|
|
|
|
+ try:
|
|
|
|
|
+ scale_percent = float(self.scale_var.get().strip() or "100")
|
|
|
|
|
+ offset_x = float(self.offset_x_var.get().strip() or "0")
|
|
|
|
|
+ offset_y = float(self.offset_y_var.get().strip() or "0")
|
|
|
|
|
+ except ValueError:
|
|
|
|
|
+ self.log("❌ 错误:缩放比例、X 偏移、Y 偏移必须是有效数字")
|
|
|
|
|
+ self.start_btn.config(state=tk.NORMAL)
|
|
|
|
|
+ return
|
|
|
|
|
+
|
|
|
|
|
+ if not all([input_dir, template_file, output_dir]):
|
|
|
|
|
+ self.log("❌ 错误:请完整填写输入文件夹、模板文件和输出文件夹")
|
|
|
|
|
+ self.start_btn.config(state=tk.NORMAL)
|
|
|
|
|
+ return
|
|
|
|
|
+ if not os.path.isdir(input_dir):
|
|
|
|
|
+ self.log(f"❌ 错误:输入文件夹不存在:{input_dir}")
|
|
|
|
|
+ self.start_btn.config(state=tk.NORMAL)
|
|
|
|
|
+ return
|
|
|
|
|
+ if not os.path.isfile(template_file):
|
|
|
|
|
+ self.log(f"❌ 错误:模板文件不存在:{template_file}")
|
|
|
|
|
+ self.start_btn.config(state=tk.NORMAL)
|
|
|
|
|
+ return
|
|
|
|
|
+ if not os.path.exists(output_dir):
|
|
|
|
|
+ os.makedirs(output_dir, exist_ok=True)
|
|
|
|
|
+
|
|
|
|
|
+ pdf_files = [f for f in os.listdir(input_dir) if f.lower().endswith(".pdf")]
|
|
|
|
|
+ if not pdf_files:
|
|
|
|
|
+ self.log("⚠ 输入文件夹中没有找到 PDF 文件")
|
|
|
|
|
+ self.start_btn.config(state=tk.NORMAL)
|
|
|
|
|
+ return
|
|
|
|
|
+
|
|
|
|
|
+ self.log(f"📁 共找到 {len(pdf_files)} 个 PDF 文件")
|
|
|
|
|
+ self.log(f"⚙ 参数:缩放 {scale_percent}% 宽,偏移 ({offset_x}, {offset_y})\n")
|
|
|
|
|
+
|
|
|
|
|
+ try:
|
|
|
|
|
+ template_doc = fitz.open(template_file)
|
|
|
|
|
+ if len(template_doc) == 0:
|
|
|
|
|
+ self.log("❌ 错误:模板文件为空")
|
|
|
|
|
+ self.start_btn.config(state=tk.NORMAL)
|
|
|
|
|
+ return
|
|
|
|
|
+
|
|
|
|
|
+ template_page = template_doc[0]
|
|
|
|
|
+ template_rect = template_page.rect
|
|
|
|
|
+ template_w = template_rect.width
|
|
|
|
|
+ template_h = template_rect.height
|
|
|
|
|
+
|
|
|
|
|
+ for pdf_name in pdf_files:
|
|
|
|
|
+ pdf_path = os.path.join(input_dir, pdf_name)
|
|
|
|
|
+ self.log(f"📄 正在处理:{pdf_name}")
|
|
|
|
|
+
|
|
|
|
|
+ try:
|
|
|
|
|
+ doc = fitz.open(pdf_path)
|
|
|
|
|
+ odd_indices = list(range(0, len(doc), 2))
|
|
|
|
|
+
|
|
|
|
|
+ if not odd_indices:
|
|
|
|
|
+ self.log(" ⚠ 该文件没有奇数页,已跳过")
|
|
|
|
|
+ doc.close()
|
|
|
|
|
+ continue
|
|
|
|
|
+
|
|
|
|
|
+ safe_name = self.sanitize_filename(self.extract_text(doc[odd_indices[0]]))
|
|
|
|
|
+ if not safe_name:
|
|
|
|
|
+ safe_name = os.path.splitext(pdf_name)[0]
|
|
|
|
|
+
|
|
|
|
|
+ new_doc = fitz.open()
|
|
|
|
|
+
|
|
|
|
|
+ for idx in odd_indices:
|
|
|
|
|
+ page = doc[idx]
|
|
|
|
|
+ page_num = idx + 1
|
|
|
|
|
+
|
|
|
|
|
+ new_page = new_doc.new_page(width=template_w, height=template_h)
|
|
|
|
|
+
|
|
|
|
|
+ # 1) 模板背景
|
|
|
|
|
+ new_page.show_pdf_page(template_rect, template_doc, 0, overlay=False)
|
|
|
|
|
+
|
|
|
|
|
+ # 2) 计算输入页目标矩形
|
|
|
|
|
+ input_rect = page.rect
|
|
|
|
|
+ # 按模板宽度的百分比计算目标宽度
|
|
|
|
|
+ target_w = template_w * (scale_percent / 100.0)
|
|
|
|
|
+ # 等比例缩放高度
|
|
|
|
|
+ scale = target_w / input_rect.width
|
|
|
|
|
+ target_h = input_rect.height * scale
|
|
|
|
|
+
|
|
|
|
|
+ target_rect = fitz.Rect(offset_x, offset_y, offset_x + target_w, offset_y + target_h)
|
|
|
|
|
+
|
|
|
|
|
+ # 3) 叠加输入页(前景)
|
|
|
|
|
+ new_page.show_pdf_page(target_rect, doc, idx, overlay=True)
|
|
|
|
|
+
|
|
|
|
|
+ self.log(f" ✔ 已合成第 {page_num} 页 → 尺寸 {target_w:.1f}×{target_h:.1f} 位置 ({offset_x}, {offset_y})")
|
|
|
|
|
+
|
|
|
|
|
+ # 保存
|
|
|
|
|
+ out_name = f"{safe_name}.pdf"
|
|
|
|
|
+ out_path = os.path.join(output_dir, out_name)
|
|
|
|
|
+ counter = 1
|
|
|
|
|
+ while os.path.exists(out_path):
|
|
|
|
|
+ stem, ext = os.path.splitext(out_name)
|
|
|
|
|
+ out_path = os.path.join(output_dir, f"{stem}_{counter}{ext}")
|
|
|
|
|
+ counter += 1
|
|
|
|
|
+
|
|
|
|
|
+ new_doc.save(out_path)
|
|
|
|
|
+ new_doc.close()
|
|
|
|
|
+ doc.close()
|
|
|
|
|
+
|
|
|
|
|
+ self.log(f" 💾 已保存:{os.path.basename(out_path)}\n")
|
|
|
|
|
+
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ self.log(f" ❌ 处理失败:{str(e)}\n")
|
|
|
|
|
+
|
|
|
|
|
+ template_doc.close()
|
|
|
|
|
+ self.log("🎉 全部处理完成!")
|
|
|
|
|
+
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ self.log(f"❌ 发生错误:{str(e)}")
|
|
|
|
|
+
|
|
|
|
|
+ self.start_btn.config(state=tk.NORMAL)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+if __name__ == "__main__":
|
|
|
|
|
+ root = tk.Tk()
|
|
|
|
|
+ app = PDFMergeApp(root)
|
|
|
|
|
+ root.mainloop()
|