@blackbelt-technology/pi-dashboard-document-converter 0.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.pi/skills/document-converter/SKILL.md +79 -0
- package/README.md +106 -0
- package/engine/.dockerignore +4 -0
- package/engine/Dockerfile +75 -0
- package/engine/IMAGE_VERSION +1 -0
- package/engine/README.md +65 -0
- package/engine/VENDOR.md +40 -0
- package/engine/build-image.sh +29 -0
- package/engine/document_converter/LICENSE +21 -0
- package/engine/document_converter/__init__.py +365 -0
- package/engine/document_converter/__main__.py +5 -0
- package/engine/document_converter/asciidoc_converter.py +534 -0
- package/engine/document_converter/cli.py +751 -0
- package/engine/document_converter/code_renderer.py +356 -0
- package/engine/document_converter/converter.py +928 -0
- package/engine/document_converter/cover_page.py +448 -0
- package/engine/document_converter/diagram_resizer.py +311 -0
- package/engine/document_converter/diagram_sizing.py +357 -0
- package/engine/document_converter/docx_reader.py +627 -0
- package/engine/document_converter/field_updater.py +277 -0
- package/engine/document_converter/font_manager.py +850 -0
- package/engine/document_converter/frontmatter_parser.py +644 -0
- package/engine/document_converter/graphviz_renderer.py +354 -0
- package/engine/document_converter/image_extractor.py +281 -0
- package/engine/document_converter/image_style.py +237 -0
- package/engine/document_converter/logo_manager.py +1021 -0
- package/engine/document_converter/manifest_manager.py +288 -0
- package/engine/document_converter/mermaid_renderer.py +371 -0
- package/engine/document_converter/meta_mapper.py +398 -0
- package/engine/document_converter/pandoc_styles.py +222 -0
- package/engine/document_converter/pdf_converter.py +735 -0
- package/engine/document_converter/pdf_server.py +283 -0
- package/engine/document_converter/plantuml_renderer.py +460 -0
- package/engine/document_converter/plantuml_server.py +273 -0
- package/engine/document_converter/pyproject.toml +80 -0
- package/engine/document_converter/round_trip.py +312 -0
- package/engine/document_converter/spacing_applier.py +332 -0
- package/engine/document_converter/spacing_config.py +323 -0
- package/engine/document_converter/style_extractor.py +420 -0
- package/engine/document_converter/svg_embedder.py +294 -0
- package/engine/document_converter/system_variables.py +158 -0
- package/engine/document_converter/table_formatter.py +450 -0
- package/engine/document_converter/template_extractor.py +1334 -0
- package/engine/document_converter/template_manager.py +427 -0
- package/engine/document_converter/textbox_extractor.py +551 -0
- package/engine/document_converter/toc_generator.py +559 -0
- package/engine/document_converter/toc_style.py +290 -0
- package/engine/document_converter/tree_renderer.py +328 -0
- package/engine/document_converter/utils.py +134 -0
- package/engine/document_converter/variable_manager.py +394 -0
- package/engine/engine_cli.py +371 -0
- package/engine/frontmatter_filler/doc-meta.sample.yaml +106 -0
- package/engine/frontmatter_filler/fill.py +517 -0
- package/engine/markdown_table_profiler/profile.py +442 -0
- package/engine/nano-banana-styles.yaml +59 -0
- package/engine/tests/test_styled_diagrams.py +109 -0
- package/package.json +32 -0
- package/src/__tests__/engine.test.ts +72 -0
- package/src/__tests__/facade.test.ts +120 -0
- package/src/__tests__/integration.test.ts +101 -0
- package/src/__tests__/ocr.test.ts +34 -0
- package/src/__tests__/provenance.test.ts +89 -0
- package/src/__tests__/routing.test.ts +40 -0
- package/src/engine.ts +187 -0
- package/src/errors.ts +43 -0
- package/src/index.ts +179 -0
- package/src/ocr.ts +43 -0
- package/src/provenance.ts +79 -0
- package/src/routing.ts +49 -0
- package/src/schema.json +69 -0
- package/src/schema.ts +107 -0
- package/src/types.ts +90 -0
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
"""DOCX field update functionality using LibreOffice via Gotenberg.
|
|
2
|
+
|
|
3
|
+
This module provides functions to update field codes (TOC, page numbers, etc.)
|
|
4
|
+
in DOCX files without converting to PDF. It uses the Gotenberg Docker service
|
|
5
|
+
which runs LibreOffice internally.
|
|
6
|
+
|
|
7
|
+
The key insight is that LibreOffice updates all fields when opening and saving
|
|
8
|
+
a document, so we convert DOCX -> DOCX via LibreOffice to trigger field updates.
|
|
9
|
+
"""
|
|
10
|
+
import shutil
|
|
11
|
+
import subprocess
|
|
12
|
+
import tempfile
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from typing import Optional, Union
|
|
15
|
+
import urllib.request
|
|
16
|
+
import urllib.error
|
|
17
|
+
|
|
18
|
+
from .pdf_server import get_server, PDFServerError, check_docker_available
|
|
19
|
+
from .pdf_converter import find_local_libreoffice
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class FieldUpdateError(Exception):
|
|
23
|
+
"""Exception raised when field update fails."""
|
|
24
|
+
pass
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def is_gotenberg_available() -> bool:
|
|
28
|
+
"""Check if Gotenberg Docker service is available.
|
|
29
|
+
|
|
30
|
+
Returns:
|
|
31
|
+
True if Gotenberg is available (container running or can be started).
|
|
32
|
+
"""
|
|
33
|
+
if not check_docker_available():
|
|
34
|
+
return False
|
|
35
|
+
|
|
36
|
+
server = get_server()
|
|
37
|
+
return server.is_container_running() or server.is_container_exists()
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def is_field_update_available() -> bool:
|
|
41
|
+
"""Check if field update functionality is available.
|
|
42
|
+
|
|
43
|
+
Field update requires either:
|
|
44
|
+
- Local LibreOffice installation, OR
|
|
45
|
+
- Gotenberg Docker service
|
|
46
|
+
|
|
47
|
+
Returns:
|
|
48
|
+
True if field update is available.
|
|
49
|
+
"""
|
|
50
|
+
# Check local LibreOffice first
|
|
51
|
+
if find_local_libreoffice():
|
|
52
|
+
return True
|
|
53
|
+
|
|
54
|
+
# Check Gotenberg
|
|
55
|
+
return is_gotenberg_available()
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _update_fields_with_local_libreoffice(
|
|
59
|
+
input_path: Path,
|
|
60
|
+
output_path: Path,
|
|
61
|
+
libreoffice_path: str
|
|
62
|
+
) -> None:
|
|
63
|
+
"""Update DOCX fields using local LibreOffice.
|
|
64
|
+
|
|
65
|
+
This opens the DOCX in LibreOffice headless mode, which automatically
|
|
66
|
+
updates all fields, then saves it back to DOCX format.
|
|
67
|
+
|
|
68
|
+
Args:
|
|
69
|
+
input_path: Path to input DOCX file.
|
|
70
|
+
output_path: Path to output DOCX file.
|
|
71
|
+
libreoffice_path: Path to LibreOffice soffice binary.
|
|
72
|
+
|
|
73
|
+
Raises:
|
|
74
|
+
FieldUpdateError: If update fails.
|
|
75
|
+
"""
|
|
76
|
+
output_dir = output_path.parent
|
|
77
|
+
output_dir.mkdir(parents=True, exist_ok=True)
|
|
78
|
+
|
|
79
|
+
# Create a temp directory for LibreOffice output
|
|
80
|
+
with tempfile.TemporaryDirectory() as tmp_dir:
|
|
81
|
+
tmp_path = Path(tmp_dir)
|
|
82
|
+
|
|
83
|
+
# Copy input to temp dir (LibreOffice outputs to same dir as input)
|
|
84
|
+
tmp_input = tmp_path / input_path.name
|
|
85
|
+
shutil.copy2(input_path, tmp_input)
|
|
86
|
+
|
|
87
|
+
# Convert DOCX to DOCX (this triggers field updates)
|
|
88
|
+
# Using the MS Word 2007 XML format filter
|
|
89
|
+
result = subprocess.run(
|
|
90
|
+
[
|
|
91
|
+
libreoffice_path,
|
|
92
|
+
'--headless',
|
|
93
|
+
'--convert-to', 'docx:"MS Word 2007 XML"',
|
|
94
|
+
'--outdir', str(tmp_path),
|
|
95
|
+
str(tmp_input)
|
|
96
|
+
],
|
|
97
|
+
capture_output=True,
|
|
98
|
+
text=True,
|
|
99
|
+
timeout=120
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
if result.returncode != 0:
|
|
103
|
+
raise FieldUpdateError(
|
|
104
|
+
f"LibreOffice field update failed: {result.stderr}"
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
# LibreOffice names output based on input filename
|
|
108
|
+
expected_output = tmp_path / f"{tmp_input.stem}.docx"
|
|
109
|
+
|
|
110
|
+
if not expected_output.exists():
|
|
111
|
+
raise FieldUpdateError(
|
|
112
|
+
f"LibreOffice did not create expected output file"
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
# Move to final destination
|
|
116
|
+
shutil.move(str(expected_output), str(output_path))
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def _update_fields_with_gotenberg(
|
|
120
|
+
input_path: Path,
|
|
121
|
+
output_path: Path
|
|
122
|
+
) -> None:
|
|
123
|
+
"""Update DOCX fields using Gotenberg Docker service.
|
|
124
|
+
|
|
125
|
+
Gotenberg's LibreOffice endpoint can convert DOCX to DOCX, which
|
|
126
|
+
triggers field updates during the conversion process.
|
|
127
|
+
|
|
128
|
+
Args:
|
|
129
|
+
input_path: Path to input DOCX file.
|
|
130
|
+
output_path: Path to output DOCX file.
|
|
131
|
+
|
|
132
|
+
Raises:
|
|
133
|
+
FieldUpdateError: If update fails.
|
|
134
|
+
PDFServerError: If Gotenberg is not available.
|
|
135
|
+
"""
|
|
136
|
+
server = get_server()
|
|
137
|
+
base_url = server.ensure_running()
|
|
138
|
+
|
|
139
|
+
# Use LibreOffice convert endpoint with DOCX output format
|
|
140
|
+
# Gotenberg API: POST /forms/libreoffice/convert
|
|
141
|
+
url = f"{base_url}/forms/libreoffice/convert"
|
|
142
|
+
|
|
143
|
+
# Read DOCX file
|
|
144
|
+
docx_content = input_path.read_bytes()
|
|
145
|
+
|
|
146
|
+
# Build multipart form data
|
|
147
|
+
boundary = '----WebKitFormBoundary7MA4YWxkTrZu0gW'
|
|
148
|
+
body = []
|
|
149
|
+
|
|
150
|
+
# Add file field
|
|
151
|
+
body.append(f'--{boundary}'.encode())
|
|
152
|
+
body.append(f'Content-Disposition: form-data; name="files"; filename="{input_path.name}"'.encode())
|
|
153
|
+
body.append(b'Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document')
|
|
154
|
+
body.append(b'')
|
|
155
|
+
body.append(docx_content)
|
|
156
|
+
|
|
157
|
+
# Request DOCX output format (not PDF)
|
|
158
|
+
# Gotenberg uses the nativePdfFormat or pdfFormat for PDF-specific options
|
|
159
|
+
# For DOCX output, we need to use a different approach
|
|
160
|
+
# Actually, Gotenberg's LibreOffice endpoint always outputs PDF
|
|
161
|
+
# We need to use a different approach - convert via local LibreOffice or
|
|
162
|
+
# use a workaround
|
|
163
|
+
|
|
164
|
+
body.append(f'--{boundary}--'.encode())
|
|
165
|
+
body_bytes = b'\r\n'.join(body)
|
|
166
|
+
|
|
167
|
+
# Note: Gotenberg's LibreOffice endpoint only outputs PDF
|
|
168
|
+
# For DOCX-to-DOCX conversion, we need local LibreOffice
|
|
169
|
+
# This function will raise an error indicating the limitation
|
|
170
|
+
raise FieldUpdateError(
|
|
171
|
+
"Gotenberg does not support DOCX-to-DOCX conversion. "
|
|
172
|
+
"Please install local LibreOffice for field updates."
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def update_docx_fields(
|
|
177
|
+
input_path: Union[str, Path],
|
|
178
|
+
output_path: Optional[Union[str, Path]] = None,
|
|
179
|
+
auto_start_gotenberg: bool = True
|
|
180
|
+
) -> Path:
|
|
181
|
+
"""Update all field codes in a DOCX file.
|
|
182
|
+
|
|
183
|
+
This function opens the DOCX in LibreOffice (local or via Docker),
|
|
184
|
+
which automatically updates all fields including:
|
|
185
|
+
- Table of Contents (TOC)
|
|
186
|
+
- Page numbers
|
|
187
|
+
- Cross-references
|
|
188
|
+
- Date/time fields
|
|
189
|
+
- Index entries
|
|
190
|
+
|
|
191
|
+
Args:
|
|
192
|
+
input_path: Path to input DOCX file.
|
|
193
|
+
output_path: Path to output DOCX file. If None, updates in place.
|
|
194
|
+
auto_start_gotenberg: If True, automatically start Gotenberg Docker
|
|
195
|
+
if local LibreOffice is not available.
|
|
196
|
+
|
|
197
|
+
Returns:
|
|
198
|
+
Path to the updated DOCX file.
|
|
199
|
+
|
|
200
|
+
Raises:
|
|
201
|
+
FileNotFoundError: If input file does not exist.
|
|
202
|
+
FieldUpdateError: If field update fails.
|
|
203
|
+
|
|
204
|
+
Example:
|
|
205
|
+
>>> update_docx_fields("document.docx") # Update in place
|
|
206
|
+
>>> update_docx_fields("input.docx", "output.docx") # Save to new file
|
|
207
|
+
"""
|
|
208
|
+
input_path = Path(input_path)
|
|
209
|
+
|
|
210
|
+
if not input_path.exists():
|
|
211
|
+
raise FileNotFoundError(f"Input file does not exist: {input_path}")
|
|
212
|
+
|
|
213
|
+
# Determine output path
|
|
214
|
+
if output_path is None:
|
|
215
|
+
output_path = input_path
|
|
216
|
+
else:
|
|
217
|
+
output_path = Path(output_path)
|
|
218
|
+
|
|
219
|
+
# Check for local LibreOffice first (preferred)
|
|
220
|
+
libreoffice_path = find_local_libreoffice()
|
|
221
|
+
|
|
222
|
+
if libreoffice_path:
|
|
223
|
+
# Use local LibreOffice
|
|
224
|
+
if output_path == input_path:
|
|
225
|
+
# Update in place - use temp file
|
|
226
|
+
with tempfile.NamedTemporaryFile(suffix='.docx', delete=False) as tmp:
|
|
227
|
+
tmp_path = Path(tmp.name)
|
|
228
|
+
try:
|
|
229
|
+
_update_fields_with_local_libreoffice(input_path, tmp_path, libreoffice_path)
|
|
230
|
+
shutil.move(str(tmp_path), str(output_path))
|
|
231
|
+
finally:
|
|
232
|
+
if tmp_path.exists():
|
|
233
|
+
tmp_path.unlink()
|
|
234
|
+
else:
|
|
235
|
+
_update_fields_with_local_libreoffice(input_path, output_path, libreoffice_path)
|
|
236
|
+
|
|
237
|
+
return output_path
|
|
238
|
+
|
|
239
|
+
# Fall back to Gotenberg (note: limited support)
|
|
240
|
+
if auto_start_gotenberg and check_docker_available():
|
|
241
|
+
try:
|
|
242
|
+
_update_fields_with_gotenberg(input_path, output_path)
|
|
243
|
+
return output_path
|
|
244
|
+
except FieldUpdateError:
|
|
245
|
+
# Gotenberg doesn't support DOCX-to-DOCX
|
|
246
|
+
pass
|
|
247
|
+
|
|
248
|
+
raise FieldUpdateError(
|
|
249
|
+
"No field update method available. Please install LibreOffice:\n"
|
|
250
|
+
" macOS: brew install --cask libreoffice\n"
|
|
251
|
+
" Linux: sudo apt install libreoffice\n"
|
|
252
|
+
" Windows: Download from https://www.libreoffice.org/"
|
|
253
|
+
)
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def update_fields_if_available(
|
|
257
|
+
docx_path: Union[str, Path],
|
|
258
|
+
output_path: Optional[Union[str, Path]] = None
|
|
259
|
+
) -> bool:
|
|
260
|
+
"""Update DOCX fields if possible, otherwise skip silently.
|
|
261
|
+
|
|
262
|
+
This is a convenience function that attempts to update fields but
|
|
263
|
+
doesn't raise an error if the update method is not available.
|
|
264
|
+
Useful for optional field updates where manual update is acceptable.
|
|
265
|
+
|
|
266
|
+
Args:
|
|
267
|
+
docx_path: Path to DOCX file.
|
|
268
|
+
output_path: Path to output file. If None, updates in place.
|
|
269
|
+
|
|
270
|
+
Returns:
|
|
271
|
+
True if fields were updated, False if skipped.
|
|
272
|
+
"""
|
|
273
|
+
try:
|
|
274
|
+
update_docx_fields(docx_path, output_path, auto_start_gotenberg=False)
|
|
275
|
+
return True
|
|
276
|
+
except (FieldUpdateError, PDFServerError):
|
|
277
|
+
return False
|