Skillfarm / skills · skillfarm.ai / converted / docx / scripts / accept_changes.py
| 1 | """Accept all tracked changes in a DOCX file using LibreOffice. |
| 2 | |
| 3 | Requires LibreOffice (soffice) to be installed. |
| 4 | """ |
| 5 | |
| 6 | import argparse |
| 7 | import logging |
| 8 | import shutil |
| 9 | import subprocess |
| 10 | from pathlib import Path |
| 11 | |
| 12 | from office.soffice import get_soffice_env |
| 13 | |
| 14 | logger = logging.getLogger(__name__) |
| 15 | |
| 16 | LIBREOFFICE_PROFILE = "/tmp/libreoffice_docx_profile" |
| 17 | MACRO_DIR = f"{LIBREOFFICE_PROFILE}/user/basic/Standard" |
| 18 | |
| 19 | ACCEPT_CHANGES_MACRO = """<?xml version="1.0" encoding="UTF-8"?> |
| 20 | <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd"> |
| 21 | <script:module xmlns:script="http://openoffice.org/2000/script" script:name="Module1" script:language="StarBasic"> |
| 22 | Sub AcceptAllTrackedChanges() |
| 23 | Dim document As Object |
| 24 | Dim dispatcher As Object |
| 25 | |
| 26 | document = ThisComponent.CurrentController.Frame |
| 27 | dispatcher = createUnoService("com.sun.star.frame.DispatchHelper") |
| 28 | |
| 29 | dispatcher.executeDispatch(document, ".uno:AcceptAllTrackedChanges", "", 0, Array()) |
| 30 | ThisComponent.store() |
| 31 | ThisComponent.close(True) |
| 32 | End Sub |
| 33 | </script:module>""" |
| 34 | |
| 35 | |
| 36 | def accept_changes( |
| 37 | input_file: str, |
| 38 | output_file: str, |
| 39 | ) -> tuple[None, str]: |
| 40 | input_path = Path(input_file) |
| 41 | output_path = Path(output_file) |
| 42 | |
| 43 | if not input_path.exists(): |
| 44 | return None, f"Error: Input file not found: {input_file}" |
| 45 | |
| 46 | if not input_path.suffix.lower() == ".docx": |
| 47 | return None, f"Error: Input file is not a DOCX file: {input_file}" |
| 48 | |
| 49 | try: |
| 50 | output_path.parent.mkdir(parents=True, exist_ok=True) |
| 51 | shutil.copy2(input_path, output_path) |
| 52 | except Exception as e: |
| 53 | return None, f"Error: Failed to copy input file to output location: {e}" |
| 54 | |
| 55 | if not _setup_libreoffice_macro(): |
| 56 | return None, "Error: Failed to setup LibreOffice macro" |
| 57 | |
| 58 | cmd = [ |
| 59 | "soffice", |
| 60 | "--headless", |
| 61 | f"-env:UserInstallation=file://{LIBREOFFICE_PROFILE}", |
| 62 | "--norestore", |
| 63 | "vnd.sun.star.script:Standard.Module1.AcceptAllTrackedChanges?language=Basic&location=application", |
| 64 | str(output_path.absolute()), |
| 65 | ] |
| 66 | |
| 67 | try: |
| 68 | result = subprocess.run( |
| 69 | cmd, |
| 70 | capture_output=True, |
| 71 | text=True, |
| 72 | timeout=30, |
| 73 | check=False, |
| 74 | env=get_soffice_env(), |
| 75 | ) |
| 76 | except subprocess.TimeoutExpired: |
| 77 | return ( |
| 78 | None, |
| 79 | f"Successfully accepted all tracked changes: {input_file} -> {output_file}", |
| 80 | ) |
| 81 | |
| 82 | if result.returncode != 0: |
| 83 | return None, f"Error: LibreOffice failed: {result.stderr}" |
| 84 | |
| 85 | return ( |
| 86 | None, |
| 87 | f"Successfully accepted all tracked changes: {input_file} -> {output_file}", |
| 88 | ) |
| 89 | |
| 90 | |
| 91 | def _setup_libreoffice_macro() -> bool: |
| 92 | macro_dir = Path(MACRO_DIR) |
| 93 | macro_file = macro_dir / "Module1.xba" |
| 94 | |
| 95 | if macro_file.exists() and "AcceptAllTrackedChanges" in macro_file.read_text(): |
| 96 | return True |
| 97 | |
| 98 | if not macro_dir.exists(): |
| 99 | subprocess.run( |
| 100 | [ |
| 101 | "soffice", |
| 102 | "--headless", |
| 103 | f"-env:UserInstallation=file://{LIBREOFFICE_PROFILE}", |
| 104 | "--terminate_after_init", |
| 105 | ], |
| 106 | capture_output=True, |
| 107 | timeout=10, |
| 108 | check=False, |
| 109 | env=get_soffice_env(), |
| 110 | ) |
| 111 | macro_dir.mkdir(parents=True, exist_ok=True) |
| 112 | |
| 113 | try: |
| 114 | macro_file.write_text(ACCEPT_CHANGES_MACRO) |
| 115 | return True |
| 116 | except Exception as e: |
| 117 | logger.warning(f"Failed to setup LibreOffice macro: {e}") |
| 118 | return False |
| 119 | |
| 120 | |
| 121 | if __name__ == "__main__": |
| 122 | parser = argparse.ArgumentParser( |
| 123 | description="Accept all tracked changes in a DOCX file" |
| 124 | ) |
| 125 | parser.add_argument("input_file", help="Input DOCX file with tracked changes") |
| 126 | parser.add_argument( |
| 127 | "output_file", help="Output DOCX file (clean, no tracked changes)" |
| 128 | ) |
| 129 | args = parser.parse_args() |
| 130 | |
| 131 | _, message = accept_changes(args.input_file, args.output_file) |
| 132 | print(message) |
| 133 | |
| 134 | if "Error" in message: |
| 135 | raise SystemExit(1) |