Last active
April 24, 2026 19:22
-
-
Save me-suzy/6ce9bc37e1c48ecb276412bf7b7f73ed to your computer and use it in GitHub Desktop.
EbookTools_v4.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # -*- coding: utf-8 -*- | |
| """ | |
| EbookTools v4 Installer (Python) | |
| Integrează artefactele din: | |
| - SetupEbookTools_v2.bas | |
| - EbookTools_v4.dotm | |
| - EbookTools_v4_INSTALARE.docx | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import shutil | |
| import sys | |
| from pathlib import Path | |
| SCRIPT_DIR = Path(__file__).resolve().parent | |
| BAS_FILE = SCRIPT_DIR / "SetupEbookTools_v2.bas" | |
| DOTM_FILE = SCRIPT_DIR / "EbookTools_v4.dotm" | |
| GUIDE_FILE = SCRIPT_DIR / "EbookTools_v4_INSTALARE.docx" | |
| TARGET_TEMPLATES_DIR = Path.home() / "AppData" / "Roaming" / "Microsoft" / "Templates" | |
| TARGET_DOTM = TARGET_TEMPLATES_DIR / "EbookTools_v4.dotm" | |
| def _require_sources() -> None: | |
| missing = [p.name for p in (BAS_FILE, DOTM_FILE, GUIDE_FILE) if not p.exists()] | |
| if missing: | |
| raise FileNotFoundError(f"Lipsesc fișiere sursă: {', '.join(missing)}") | |
| def install_by_copy(overwrite: bool) -> None: | |
| TARGET_TEMPLATES_DIR.mkdir(parents=True, exist_ok=True) | |
| if TARGET_DOTM.exists() and not overwrite: | |
| raise FileExistsError( | |
| f"{TARGET_DOTM} există deja. Rulează cu --overwrite pentru suprascriere." | |
| ) | |
| shutil.copy2(DOTM_FILE, TARGET_DOTM) | |
| print(f"[OK] Template copiat: {TARGET_DOTM}") | |
| def copy_guide(destination: Path) -> Path: | |
| destination.mkdir(parents=True, exist_ok=True) | |
| out = destination / GUIDE_FILE.name | |
| shutil.copy2(GUIDE_FILE, out) | |
| print(f"[OK] Ghid copiat: {out}") | |
| return out | |
| def install_by_word_com(visible: bool) -> None: | |
| """ | |
| Rulează macro-ul Install din BAS folosind Word COM. | |
| Necesită: | |
| - Word instalat | |
| - "Trust access to the VBA project object model" activat | |
| - macro-uri permise | |
| """ | |
| try: | |
| import win32com.client # type: ignore | |
| except ImportError as exc: | |
| raise RuntimeError( | |
| "Lipsește pywin32. Instalează cu: pip install pywin32" | |
| ) from exc | |
| word = None | |
| doc = None | |
| try: | |
| word = win32com.client.DispatchEx("Word.Application") | |
| word.Visible = bool(visible) | |
| doc = word.Documents.Add() | |
| vbproject = doc.VBProject | |
| component = vbproject.VBComponents.Import(str(BAS_FILE)) | |
| module_name = component.Name | |
| print(f"[INFO] Modul importat: {module_name}") | |
| print("[INFO] Rulez macro: Install") | |
| word.Run(f"{module_name}.Install") | |
| print("[OK] Macro Install executat cu succes.") | |
| finally: | |
| if doc is not None: | |
| try: | |
| doc.Close(False) | |
| except Exception: | |
| pass | |
| if word is not None: | |
| try: | |
| word.Quit() | |
| except Exception: | |
| pass | |
| def parse_args() -> argparse.Namespace: | |
| parser = argparse.ArgumentParser( | |
| description="Installer Python pentru EbookTools v4 (.dotm + .docx + .bas)" | |
| ) | |
| parser.add_argument( | |
| "--mode", | |
| choices=("copy", "word-com"), | |
| default="copy", | |
| help="copy = copiază direct .dotm; word-com = rulează macro-ul Install din .bas", | |
| ) | |
| parser.add_argument( | |
| "--overwrite", | |
| action="store_true", | |
| help="Suprascrie template-ul existent la modul copy", | |
| ) | |
| parser.add_argument( | |
| "--copy-guide", | |
| action="store_true", | |
| help="Copiază și ghidul .docx în Templates", | |
| ) | |
| parser.add_argument( | |
| "--word-visible", | |
| action="store_true", | |
| help="Afișează Word în modul word-com", | |
| ) | |
| return parser.parse_args() | |
| def main() -> int: | |
| args = parse_args() | |
| try: | |
| _require_sources() | |
| print(f"[INFO] Sursa BAS : {BAS_FILE}") | |
| print(f"[INFO] Sursa DOTM: {DOTM_FILE}") | |
| print(f"[INFO] Sursa DOCX: {GUIDE_FILE}") | |
| print(f"[INFO] Destinație template: {TARGET_DOTM}") | |
| if args.mode == "copy": | |
| install_by_copy(overwrite=args.overwrite) | |
| else: | |
| install_by_word_com(visible=args.word_visible) | |
| if args.copy_guide: | |
| copy_guide(TARGET_TEMPLATES_DIR) | |
| print("[DONE] Instalare finalizată.") | |
| return 0 | |
| except Exception as exc: | |
| print(f"[EROARE] {exc}") | |
| return 1 | |
| if __name__ == "__main__": | |
| sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment