-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofiles.py
More file actions
106 lines (93 loc) · 3.93 KB
/
profiles.py
File metadata and controls
106 lines (93 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""
profiles.py — Mixin de perfiles (guardar/cargar configuración) para PDFuse.
Un perfil es un archivo JSON que guarda rutas, mapeo de campos y opciones
de salida, de forma que el usuario no tenga que reconfigurar todo en cada sesión.
"""
import json
from pathlib import Path
from tkinter import filedialog, messagebox
import core
class ProfileMixin:
"""
Mixin que provee guardar y cargar perfiles de configuración en JSON.
Debe combinarse con PDFuseApp (ctk.CTk).
"""
def _save_profile(self):
path = filedialog.asksaveasfilename(
title="Guardar perfil de mapeo",
defaultextension=".json",
filetypes=[("Perfil JSON", "*.json"), ("Todos", "*.*")],
)
if not path:
return
profile = {
"pdf_path": self.pdf_path,
"pdf_type": self.pdf_type,
"excel_path": self.excel_path,
"excel_sheet": self._sheet_var.get() if hasattr(self, "_sheet_var") else None,
"field_mapping": self.field_mapping,
"flat_anchors": self.flat_anchors,
"output_dir": self.output_dir,
"filename_prefix": self.filename_prefix,
"filename_suffix": self.filename_suffix,
"filename_col": self.filename_col,
"overwrite": self._overwrite_var.get(),
"flatten": self._flatten_var.get(),
}
try:
with open(path, "w", encoding="utf-8") as f:
json.dump(profile, f, indent=2, ensure_ascii=False)
messagebox.showinfo("Guardado", f"Perfil guardado en:\n{path}")
except Exception as e:
messagebox.showerror("Error al guardar", str(e))
def _load_profile(self):
path = filedialog.askopenfilename(
title="Cargar perfil de mapeo",
filetypes=[("Perfil JSON", "*.json"), ("Todos", "*.*")],
)
if not path:
return
try:
with open(path, "r", encoding="utf-8") as f:
profile = json.load(f)
except Exception as e:
messagebox.showerror("Error al cargar", str(e))
return
self.pdf_path = profile.get("pdf_path")
self.pdf_type = profile.get("pdf_type")
self.excel_path = profile.get("excel_path")
self.field_mapping = profile.get("field_mapping", {})
self.flat_anchors = profile.get("flat_anchors", [])
self.output_dir = profile.get("output_dir")
self.filename_prefix = profile.get("filename_prefix", "doc")
self.filename_suffix = profile.get("filename_suffix", "")
self.filename_col = profile.get("filename_col")
self._overwrite_var.set(profile.get("overwrite", False))
self._flatten_var.set(profile.get("flatten", True))
# Reload Excel data if the path is still valid
xl = self.excel_path
sheet = profile.get("excel_sheet")
if xl and Path(xl).exists():
try:
self.excel_sheets = core.read_excel_sheets(xl)
target_sheet = (
sheet if sheet and sheet in self.excel_sheets
else self.excel_sheets[0] if self.excel_sheets else None
)
if target_sheet:
self.excel_df = core.read_excel_data(xl, target_sheet)
except Exception:
pass
if self.pdf_path and Path(self.pdf_path).exists():
try:
self.pdf_fields = (
core.get_acroform_fields(self.pdf_path)
if self.pdf_type == "acroform" else []
)
self.page_dims = core.get_pdf_page_dimensions(self.pdf_path)
except Exception:
pass
messagebox.showinfo("Perfil cargado",
"Configuración restaurada correctamente.")
self._max_reached_step = 4
self._show_step(1)