Validate document by converting to HTML with soffice.
(doc_path)
| 88 | |
| 89 | |
| 90 | def validate_document(doc_path): |
| 91 | """Validate document by converting to HTML with soffice.""" |
| 92 | # Determine the correct filter based on file extension |
| 93 | match doc_path.suffix.lower(): |
| 94 | case ".docx": |
| 95 | filter_name = "html:HTML" |
| 96 | case ".pptx": |
| 97 | filter_name = "html:impress_html_Export" |
| 98 | case ".xlsx": |
| 99 | filter_name = "html:HTML (StarCalc)" |
| 100 | |
| 101 | with tempfile.TemporaryDirectory() as temp_dir: |
| 102 | try: |
| 103 | result = subprocess.run( |
| 104 | [ |
| 105 | "soffice", |
| 106 | "--headless", |
| 107 | "--convert-to", |
| 108 | filter_name, |
| 109 | "--outdir", |
| 110 | temp_dir, |
| 111 | str(doc_path), |
| 112 | ], |
| 113 | capture_output=True, |
| 114 | timeout=10, |
| 115 | text=True, |
| 116 | ) |
| 117 | if not (Path(temp_dir) / f"{doc_path.stem}.html").exists(): |
| 118 | error_msg = result.stderr.strip() or "Document validation failed" |
| 119 | print(f"Validation error: {error_msg}", file=sys.stderr) |
| 120 | return False |
| 121 | return True |
| 122 | except FileNotFoundError: |
| 123 | print("Warning: soffice not found. Skipping validation.", file=sys.stderr) |
| 124 | return True |
| 125 | except subprocess.TimeoutExpired: |
| 126 | print("Validation error: Timeout during conversion", file=sys.stderr) |
| 127 | return False |
| 128 | except Exception as e: |
| 129 | print(f"Validation error: {e}", file=sys.stderr) |
| 130 | return False |
| 131 | |
| 132 | |
| 133 | def condense_xml(xml_file): |