@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,559 @@
|
|
|
1
|
+
"""Table of Contents generator for DOCX documents."""
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from typing import List, Tuple, Optional
|
|
4
|
+
from docx import Document
|
|
5
|
+
from docx.oxml.ns import qn, nsmap
|
|
6
|
+
from docx.oxml import OxmlElement
|
|
7
|
+
from docx.shared import Pt, Twips
|
|
8
|
+
import re
|
|
9
|
+
|
|
10
|
+
from .toc_style import TocStyleConfig, TocLevelStyle, TocConfig, TocMode, get_tab_leader_char
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def create_toc_field(style_config: Optional[TocStyleConfig] = None):
|
|
14
|
+
"""Create a TOC field element that Word will recognize and update.
|
|
15
|
+
|
|
16
|
+
Args:
|
|
17
|
+
style_config: Style configuration for the TOC field.
|
|
18
|
+
|
|
19
|
+
Returns:
|
|
20
|
+
A paragraph element containing the TOC field.
|
|
21
|
+
"""
|
|
22
|
+
if style_config is None:
|
|
23
|
+
style_config = TocStyleConfig()
|
|
24
|
+
|
|
25
|
+
# Create paragraph for TOC
|
|
26
|
+
paragraph = OxmlElement('w:p')
|
|
27
|
+
|
|
28
|
+
# Create run
|
|
29
|
+
run = OxmlElement('w:r')
|
|
30
|
+
|
|
31
|
+
# Create field character (begin)
|
|
32
|
+
fldChar_begin = OxmlElement('w:fldChar')
|
|
33
|
+
fldChar_begin.set(qn('w:fldCharType'), 'begin')
|
|
34
|
+
|
|
35
|
+
# Build TOC field code based on style configuration
|
|
36
|
+
# \o "1-N" = include heading levels 1-N
|
|
37
|
+
# \h = hyperlinks
|
|
38
|
+
# \z = hide tab leader and page numbers in Web Layout view
|
|
39
|
+
# \u = use applied paragraph outline level
|
|
40
|
+
# \n = no page numbers (if show_page_numbers is False)
|
|
41
|
+
field_code_parts = [f'TOC \\o "1-{style_config.max_level}"']
|
|
42
|
+
|
|
43
|
+
if style_config.hyperlinks:
|
|
44
|
+
field_code_parts.append('\\h')
|
|
45
|
+
|
|
46
|
+
field_code_parts.append('\\z \\u')
|
|
47
|
+
|
|
48
|
+
if not style_config.show_page_numbers:
|
|
49
|
+
field_code_parts.append('\\n')
|
|
50
|
+
|
|
51
|
+
field_code = ' ' + ' '.join(field_code_parts) + ' '
|
|
52
|
+
|
|
53
|
+
instrText = OxmlElement('w:instrText')
|
|
54
|
+
instrText.set(qn('xml:space'), 'preserve')
|
|
55
|
+
instrText.text = field_code
|
|
56
|
+
|
|
57
|
+
# Create field character (separate)
|
|
58
|
+
fldChar_separate = OxmlElement('w:fldChar')
|
|
59
|
+
fldChar_separate.set(qn('w:fldCharType'), 'separate')
|
|
60
|
+
|
|
61
|
+
# Create placeholder text
|
|
62
|
+
placeholder_run = OxmlElement('w:r')
|
|
63
|
+
placeholder_text = OxmlElement('w:t')
|
|
64
|
+
placeholder_text.text = 'Right-click and select "Update Field" to generate table of contents'
|
|
65
|
+
placeholder_run.append(placeholder_text)
|
|
66
|
+
|
|
67
|
+
# Create field character (end)
|
|
68
|
+
fldChar_end = OxmlElement('w:fldChar')
|
|
69
|
+
fldChar_end.set(qn('w:fldCharType'), 'end')
|
|
70
|
+
|
|
71
|
+
# Assemble the field
|
|
72
|
+
run.append(fldChar_begin)
|
|
73
|
+
paragraph.append(run)
|
|
74
|
+
|
|
75
|
+
run2 = OxmlElement('w:r')
|
|
76
|
+
run2.append(instrText)
|
|
77
|
+
paragraph.append(run2)
|
|
78
|
+
|
|
79
|
+
run3 = OxmlElement('w:r')
|
|
80
|
+
run3.append(fldChar_separate)
|
|
81
|
+
paragraph.append(run3)
|
|
82
|
+
|
|
83
|
+
paragraph.append(placeholder_run)
|
|
84
|
+
|
|
85
|
+
run4 = OxmlElement('w:r')
|
|
86
|
+
run4.append(fldChar_end)
|
|
87
|
+
paragraph.append(run4)
|
|
88
|
+
|
|
89
|
+
return paragraph
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def create_toc_heading(text: str = "Tartalomjegyzék"):
|
|
93
|
+
"""Create a heading paragraph for the TOC.
|
|
94
|
+
|
|
95
|
+
Args:
|
|
96
|
+
text: The heading text (default: "Tartalomjegyzék" - Hungarian for Table of Contents)
|
|
97
|
+
|
|
98
|
+
Returns:
|
|
99
|
+
A paragraph element with heading styling.
|
|
100
|
+
"""
|
|
101
|
+
paragraph = OxmlElement('w:p')
|
|
102
|
+
|
|
103
|
+
# Paragraph properties
|
|
104
|
+
pPr = OxmlElement('w:pPr')
|
|
105
|
+
pStyle = OxmlElement('w:pStyle')
|
|
106
|
+
pStyle.set(qn('w:val'), 'Heading1')
|
|
107
|
+
pPr.append(pStyle)
|
|
108
|
+
|
|
109
|
+
# Add spacing after
|
|
110
|
+
spacing = OxmlElement('w:spacing')
|
|
111
|
+
spacing.set(qn('w:after'), '240') # 12pt after
|
|
112
|
+
pPr.append(spacing)
|
|
113
|
+
|
|
114
|
+
paragraph.append(pPr)
|
|
115
|
+
|
|
116
|
+
# Run with text
|
|
117
|
+
run = OxmlElement('w:r')
|
|
118
|
+
rPr = OxmlElement('w:rPr')
|
|
119
|
+
bold = OxmlElement('w:b')
|
|
120
|
+
sz = OxmlElement('w:sz')
|
|
121
|
+
sz.set(qn('w:val'), '32') # 16pt
|
|
122
|
+
rPr.append(bold)
|
|
123
|
+
rPr.append(sz)
|
|
124
|
+
run.append(rPr)
|
|
125
|
+
|
|
126
|
+
text_elem = OxmlElement('w:t')
|
|
127
|
+
text_elem.text = text
|
|
128
|
+
run.append(text_elem)
|
|
129
|
+
|
|
130
|
+
paragraph.append(run)
|
|
131
|
+
|
|
132
|
+
return paragraph
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def create_page_break():
|
|
136
|
+
"""Create a page break paragraph.
|
|
137
|
+
|
|
138
|
+
Returns:
|
|
139
|
+
A paragraph element containing a page break.
|
|
140
|
+
"""
|
|
141
|
+
paragraph = OxmlElement('w:p')
|
|
142
|
+
run = OxmlElement('w:r')
|
|
143
|
+
br = OxmlElement('w:br')
|
|
144
|
+
br.set(qn('w:type'), 'page')
|
|
145
|
+
run.append(br)
|
|
146
|
+
paragraph.append(run)
|
|
147
|
+
return paragraph
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def find_first_page_break(body) -> tuple:
|
|
151
|
+
"""Find the first page break in the document body.
|
|
152
|
+
|
|
153
|
+
Args:
|
|
154
|
+
body: The document body element.
|
|
155
|
+
|
|
156
|
+
Returns:
|
|
157
|
+
Tuple of (index, element) where page break was found, or (0, None) if not found.
|
|
158
|
+
"""
|
|
159
|
+
w_ns = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'
|
|
160
|
+
|
|
161
|
+
for i, elem in enumerate(body):
|
|
162
|
+
# Check for page break in paragraph
|
|
163
|
+
for br in elem.iter(f'{w_ns}br'):
|
|
164
|
+
if br.get(qn('w:type')) == 'page':
|
|
165
|
+
return i, elem
|
|
166
|
+
|
|
167
|
+
# Also check for section break (continuous or nextPage)
|
|
168
|
+
for sectPr in elem.iter(f'{w_ns}sectPr'):
|
|
169
|
+
sect_type = sectPr.find(f'{w_ns}type')
|
|
170
|
+
if sect_type is not None and sect_type.get(qn('w:val')) in ('nextPage', 'oddPage', 'evenPage'):
|
|
171
|
+
return i, elem
|
|
172
|
+
|
|
173
|
+
return 0, None
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def find_first_heading(doc: Document) -> Optional[int]:
|
|
177
|
+
"""Find the index of the first heading paragraph in the document.
|
|
178
|
+
|
|
179
|
+
Args:
|
|
180
|
+
doc: The Document object.
|
|
181
|
+
|
|
182
|
+
Returns:
|
|
183
|
+
Index of the first heading paragraph, or None if not found.
|
|
184
|
+
"""
|
|
185
|
+
body = doc.element.body
|
|
186
|
+
body_elements = list(body)
|
|
187
|
+
|
|
188
|
+
for i, para in enumerate(doc.paragraphs):
|
|
189
|
+
style_name = para.style.name if para.style else ''
|
|
190
|
+
if style_name.startswith('Heading'):
|
|
191
|
+
# Find the corresponding element index in body
|
|
192
|
+
try:
|
|
193
|
+
return body_elements.index(para._p)
|
|
194
|
+
except ValueError:
|
|
195
|
+
continue
|
|
196
|
+
|
|
197
|
+
return None
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def add_toc(
|
|
201
|
+
docx_path: Path,
|
|
202
|
+
toc_heading: str = "Tartalomjegyzék",
|
|
203
|
+
add_page_break: bool = True,
|
|
204
|
+
after_cover_page: bool = False,
|
|
205
|
+
style_config: Optional[TocStyleConfig] = None
|
|
206
|
+
) -> None:
|
|
207
|
+
"""Add a Table of Contents to a DOCX document.
|
|
208
|
+
|
|
209
|
+
The TOC is a Word field that will be updated when the document is opened
|
|
210
|
+
in Word and the user clicks "Update Table" or presses F9.
|
|
211
|
+
|
|
212
|
+
Args:
|
|
213
|
+
docx_path: Path to the DOCX file.
|
|
214
|
+
toc_heading: Heading text for the TOC (default: "Tartalomjegyzék").
|
|
215
|
+
add_page_break: Whether to add a page break after the TOC (default: True).
|
|
216
|
+
after_cover_page: If True, insert TOC after the first page break (cover page).
|
|
217
|
+
style_config: Style configuration for TOC entries.
|
|
218
|
+
"""
|
|
219
|
+
if style_config is None:
|
|
220
|
+
style_config = TocStyleConfig()
|
|
221
|
+
|
|
222
|
+
doc = Document(docx_path)
|
|
223
|
+
|
|
224
|
+
# Get the document body element
|
|
225
|
+
body = doc.element.body
|
|
226
|
+
|
|
227
|
+
# Determine insertion point
|
|
228
|
+
if after_cover_page:
|
|
229
|
+
# Find first page break and insert after it
|
|
230
|
+
page_break_idx, page_break_elem = find_first_page_break(body)
|
|
231
|
+
if page_break_elem is not None:
|
|
232
|
+
# Insert after the element containing the page break
|
|
233
|
+
insert_after = page_break_elem
|
|
234
|
+
else:
|
|
235
|
+
# No page break found, insert at beginning
|
|
236
|
+
insert_after = None
|
|
237
|
+
else:
|
|
238
|
+
insert_after = None
|
|
239
|
+
|
|
240
|
+
# Create TOC elements
|
|
241
|
+
toc_heading_elem = create_toc_heading(toc_heading)
|
|
242
|
+
toc_field = create_toc_field(style_config)
|
|
243
|
+
|
|
244
|
+
if insert_after is not None:
|
|
245
|
+
# Insert after the cover page (after the page break element)
|
|
246
|
+
next_elem = insert_after.getnext()
|
|
247
|
+
|
|
248
|
+
# Insert TOC heading after the page break
|
|
249
|
+
insert_after.addnext(toc_heading_elem)
|
|
250
|
+
# Insert TOC field after heading
|
|
251
|
+
toc_heading_elem.addnext(toc_field)
|
|
252
|
+
|
|
253
|
+
# Add page break after TOC if requested
|
|
254
|
+
if add_page_break:
|
|
255
|
+
page_break = create_page_break()
|
|
256
|
+
toc_field.addnext(page_break)
|
|
257
|
+
else:
|
|
258
|
+
# Insert at beginning (original behavior)
|
|
259
|
+
first_element = body[0] if len(body) > 0 else None
|
|
260
|
+
|
|
261
|
+
# Insert in reverse order (so they end up in correct order)
|
|
262
|
+
if add_page_break:
|
|
263
|
+
page_break = create_page_break()
|
|
264
|
+
if first_element is not None:
|
|
265
|
+
first_element.addprevious(page_break)
|
|
266
|
+
else:
|
|
267
|
+
body.append(page_break)
|
|
268
|
+
first_element = page_break
|
|
269
|
+
|
|
270
|
+
if first_element is not None:
|
|
271
|
+
first_element.addprevious(toc_field)
|
|
272
|
+
else:
|
|
273
|
+
body.append(toc_field)
|
|
274
|
+
|
|
275
|
+
# Insert heading before TOC field
|
|
276
|
+
toc_field.addprevious(toc_heading_elem)
|
|
277
|
+
|
|
278
|
+
doc.save(docx_path)
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
def extract_headings(doc: Document, max_level: int = 3) -> List[Tuple[int, str, str]]:
|
|
282
|
+
"""Extract headings from a DOCX document.
|
|
283
|
+
|
|
284
|
+
Args:
|
|
285
|
+
doc: The Document object.
|
|
286
|
+
max_level: Maximum heading level to include (1-9).
|
|
287
|
+
|
|
288
|
+
Returns:
|
|
289
|
+
List of tuples: (level, text, bookmark_name)
|
|
290
|
+
"""
|
|
291
|
+
headings = []
|
|
292
|
+
bookmark_counter = 0
|
|
293
|
+
w_ns = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'
|
|
294
|
+
|
|
295
|
+
for para in doc.paragraphs:
|
|
296
|
+
style_name = para.style.name if para.style else ''
|
|
297
|
+
|
|
298
|
+
# Check if it's a heading style
|
|
299
|
+
if style_name.startswith('Heading'):
|
|
300
|
+
try:
|
|
301
|
+
level = int(style_name.replace('Heading ', '').replace('Heading', ''))
|
|
302
|
+
if level <= max_level:
|
|
303
|
+
text = para.text.strip()
|
|
304
|
+
if text:
|
|
305
|
+
# Generate bookmark name
|
|
306
|
+
bookmark_name = f"_Toc{bookmark_counter:08d}"
|
|
307
|
+
bookmark_counter += 1
|
|
308
|
+
headings.append((level, text, bookmark_name))
|
|
309
|
+
|
|
310
|
+
# Add bookmark to the paragraph
|
|
311
|
+
_add_bookmark_to_paragraph(para, bookmark_name)
|
|
312
|
+
except ValueError:
|
|
313
|
+
pass
|
|
314
|
+
|
|
315
|
+
return headings
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
def _add_bookmark_to_paragraph(para, bookmark_name: str) -> None:
|
|
319
|
+
"""Add a bookmark to a paragraph.
|
|
320
|
+
|
|
321
|
+
Args:
|
|
322
|
+
para: The paragraph to add the bookmark to.
|
|
323
|
+
bookmark_name: The bookmark name.
|
|
324
|
+
"""
|
|
325
|
+
# Create bookmark start
|
|
326
|
+
bookmark_start = OxmlElement('w:bookmarkStart')
|
|
327
|
+
bookmark_start.set(qn('w:id'), str(hash(bookmark_name) % 10000))
|
|
328
|
+
bookmark_start.set(qn('w:name'), bookmark_name)
|
|
329
|
+
|
|
330
|
+
# Create bookmark end
|
|
331
|
+
bookmark_end = OxmlElement('w:bookmarkEnd')
|
|
332
|
+
bookmark_end.set(qn('w:id'), str(hash(bookmark_name) % 10000))
|
|
333
|
+
|
|
334
|
+
# Insert at the beginning and end of the paragraph
|
|
335
|
+
para._p.insert(0, bookmark_start)
|
|
336
|
+
para._p.append(bookmark_end)
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
def _get_tab_leader_xml(leader_type: str) -> str:
|
|
340
|
+
"""Convert tab leader type to XML attribute value.
|
|
341
|
+
|
|
342
|
+
Args:
|
|
343
|
+
leader_type: Type of tab leader (dots, dashes, underline, none).
|
|
344
|
+
|
|
345
|
+
Returns:
|
|
346
|
+
XML attribute value for w:leader.
|
|
347
|
+
"""
|
|
348
|
+
leaders = {
|
|
349
|
+
'dots': 'dot',
|
|
350
|
+
'dashes': 'hyphen',
|
|
351
|
+
'underline': 'underscore',
|
|
352
|
+
'none': 'none',
|
|
353
|
+
}
|
|
354
|
+
return leaders.get(leader_type.lower(), 'dot')
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
def create_static_toc_entry(
|
|
358
|
+
level: int,
|
|
359
|
+
text: str,
|
|
360
|
+
bookmark_name: str,
|
|
361
|
+
style_config: Optional[TocStyleConfig] = None,
|
|
362
|
+
level_style: Optional[TocLevelStyle] = None
|
|
363
|
+
) -> OxmlElement:
|
|
364
|
+
"""Create a static TOC entry paragraph with hyperlink.
|
|
365
|
+
|
|
366
|
+
Args:
|
|
367
|
+
level: Heading level (1-9).
|
|
368
|
+
text: The heading text.
|
|
369
|
+
bookmark_name: The bookmark name to link to.
|
|
370
|
+
style_config: Overall TOC style configuration.
|
|
371
|
+
level_style: Specific style for this level.
|
|
372
|
+
|
|
373
|
+
Returns:
|
|
374
|
+
A paragraph element for the TOC entry.
|
|
375
|
+
"""
|
|
376
|
+
if style_config is None:
|
|
377
|
+
style_config = TocStyleConfig()
|
|
378
|
+
|
|
379
|
+
paragraph = OxmlElement('w:p')
|
|
380
|
+
|
|
381
|
+
# Paragraph properties
|
|
382
|
+
pPr = OxmlElement('w:pPr')
|
|
383
|
+
|
|
384
|
+
# Use TOC style
|
|
385
|
+
pStyle = OxmlElement('w:pStyle')
|
|
386
|
+
pStyle.set(qn('w:val'), f'TOC{level}')
|
|
387
|
+
pPr.append(pStyle)
|
|
388
|
+
|
|
389
|
+
# Indentation - use level_style if provided, otherwise calculate from config
|
|
390
|
+
indent = 0
|
|
391
|
+
if level_style and level_style.indent is not None:
|
|
392
|
+
indent = level_style.indent * 20 # points to twips
|
|
393
|
+
else:
|
|
394
|
+
indent = (level - 1) * style_config.indent_per_level * 20 # points to twips
|
|
395
|
+
|
|
396
|
+
ind = OxmlElement('w:ind')
|
|
397
|
+
ind.set(qn('w:left'), str(indent))
|
|
398
|
+
pPr.append(ind)
|
|
399
|
+
|
|
400
|
+
# Spacing
|
|
401
|
+
if level_style:
|
|
402
|
+
spacing = OxmlElement('w:spacing')
|
|
403
|
+
if level_style.spacing_before is not None:
|
|
404
|
+
spacing.set(qn('w:before'), str(level_style.spacing_before * 20))
|
|
405
|
+
if level_style.spacing_after is not None:
|
|
406
|
+
spacing.set(qn('w:after'), str(level_style.spacing_after * 20))
|
|
407
|
+
if level_style.spacing_before is not None or level_style.spacing_after is not None:
|
|
408
|
+
pPr.append(spacing)
|
|
409
|
+
|
|
410
|
+
# Tab stops for dot leader (right-aligned at ~6 inches = 8640 twips)
|
|
411
|
+
tabs = OxmlElement('w:tabs')
|
|
412
|
+
tab = OxmlElement('w:tab')
|
|
413
|
+
tab.set(qn('w:val'), 'right')
|
|
414
|
+
tab.set(qn('w:leader'), _get_tab_leader_xml(style_config.tab_leader))
|
|
415
|
+
tab.set(qn('w:pos'), '9072') # ~6.3 inches
|
|
416
|
+
tabs.append(tab)
|
|
417
|
+
pPr.append(tabs)
|
|
418
|
+
|
|
419
|
+
paragraph.append(pPr)
|
|
420
|
+
|
|
421
|
+
# Create hyperlink to bookmark (if hyperlinks enabled)
|
|
422
|
+
if style_config.hyperlinks:
|
|
423
|
+
container = OxmlElement('w:hyperlink')
|
|
424
|
+
container.set(qn('w:anchor'), bookmark_name)
|
|
425
|
+
container.set(qn('w:history'), '1')
|
|
426
|
+
else:
|
|
427
|
+
container = paragraph # Add run directly to paragraph
|
|
428
|
+
|
|
429
|
+
# Run with text
|
|
430
|
+
run = OxmlElement('w:r')
|
|
431
|
+
rPr = OxmlElement('w:rPr')
|
|
432
|
+
|
|
433
|
+
# Apply level-specific styling
|
|
434
|
+
if level_style:
|
|
435
|
+
if level_style.bold:
|
|
436
|
+
bold = OxmlElement('w:b')
|
|
437
|
+
rPr.append(bold)
|
|
438
|
+
if level_style.italic:
|
|
439
|
+
italic = OxmlElement('w:i')
|
|
440
|
+
rPr.append(italic)
|
|
441
|
+
if level_style.font_size:
|
|
442
|
+
sz = OxmlElement('w:sz')
|
|
443
|
+
sz.set(qn('w:val'), str(level_style.font_size * 2)) # half-points
|
|
444
|
+
rPr.append(sz)
|
|
445
|
+
szCs = OxmlElement('w:szCs')
|
|
446
|
+
szCs.set(qn('w:val'), str(level_style.font_size * 2))
|
|
447
|
+
rPr.append(szCs)
|
|
448
|
+
if level_style.font_name:
|
|
449
|
+
rFonts = OxmlElement('w:rFonts')
|
|
450
|
+
rFonts.set(qn('w:ascii'), level_style.font_name)
|
|
451
|
+
rFonts.set(qn('w:hAnsi'), level_style.font_name)
|
|
452
|
+
rPr.append(rFonts)
|
|
453
|
+
if level_style.color:
|
|
454
|
+
color = OxmlElement('w:color')
|
|
455
|
+
color.set(qn('w:val'), level_style.color)
|
|
456
|
+
rPr.append(color)
|
|
457
|
+
else:
|
|
458
|
+
# Default: bold for level 1
|
|
459
|
+
if level == 1:
|
|
460
|
+
bold = OxmlElement('w:b')
|
|
461
|
+
rPr.append(bold)
|
|
462
|
+
|
|
463
|
+
run.append(rPr)
|
|
464
|
+
|
|
465
|
+
text_elem = OxmlElement('w:t')
|
|
466
|
+
text_elem.text = text
|
|
467
|
+
run.append(text_elem)
|
|
468
|
+
|
|
469
|
+
if style_config.hyperlinks:
|
|
470
|
+
container.append(run)
|
|
471
|
+
paragraph.append(container)
|
|
472
|
+
else:
|
|
473
|
+
paragraph.append(run)
|
|
474
|
+
|
|
475
|
+
return paragraph
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
def add_static_toc(
|
|
479
|
+
docx_path: Path,
|
|
480
|
+
toc_heading: str = "Tartalomjegyzék",
|
|
481
|
+
max_level: int = 3,
|
|
482
|
+
add_page_break: bool = True,
|
|
483
|
+
after_cover_page: bool = False,
|
|
484
|
+
style_config: Optional[TocStyleConfig] = None
|
|
485
|
+
) -> None:
|
|
486
|
+
"""Add a static Table of Contents to a DOCX document.
|
|
487
|
+
|
|
488
|
+
This creates actual TOC entries with hyperlinks to bookmarks,
|
|
489
|
+
which works correctly when converting to PDF without requiring
|
|
490
|
+
Word field updates.
|
|
491
|
+
|
|
492
|
+
Args:
|
|
493
|
+
docx_path: Path to the DOCX file.
|
|
494
|
+
toc_heading: Heading text for the TOC.
|
|
495
|
+
max_level: Maximum heading level to include (default: 3).
|
|
496
|
+
add_page_break: Whether to add a page break after the TOC.
|
|
497
|
+
after_cover_page: If True, insert TOC before the first heading
|
|
498
|
+
(after cover page content like logos).
|
|
499
|
+
style_config: Style configuration for TOC entries.
|
|
500
|
+
"""
|
|
501
|
+
if style_config is None:
|
|
502
|
+
style_config = TocStyleConfig()
|
|
503
|
+
|
|
504
|
+
# Use max_level from style_config if not explicitly overridden
|
|
505
|
+
effective_max_level = style_config.max_level if max_level == 3 else max_level
|
|
506
|
+
|
|
507
|
+
doc = Document(docx_path)
|
|
508
|
+
|
|
509
|
+
# Extract headings and add bookmarks
|
|
510
|
+
headings = extract_headings(doc, effective_max_level)
|
|
511
|
+
|
|
512
|
+
if not headings:
|
|
513
|
+
# No headings found, save and return
|
|
514
|
+
doc.save(docx_path)
|
|
515
|
+
return
|
|
516
|
+
|
|
517
|
+
# Get the document body element
|
|
518
|
+
body = doc.element.body
|
|
519
|
+
|
|
520
|
+
# Determine insertion point
|
|
521
|
+
# Best approach: insert right before the first heading
|
|
522
|
+
# This preserves any content between cover page and first heading (e.g., logo)
|
|
523
|
+
first_heading_idx = find_first_heading(doc)
|
|
524
|
+
|
|
525
|
+
# Build TOC elements in correct order:
|
|
526
|
+
# 1. TOC heading
|
|
527
|
+
# 2. TOC entries (in document order)
|
|
528
|
+
# 3. Page break
|
|
529
|
+
toc_elements = []
|
|
530
|
+
|
|
531
|
+
# TOC heading first
|
|
532
|
+
toc_elements.append(create_toc_heading(toc_heading))
|
|
533
|
+
|
|
534
|
+
# TOC entries in document order
|
|
535
|
+
for level, text, bookmark_name in headings:
|
|
536
|
+
# Get level-specific style if defined
|
|
537
|
+
level_style = style_config.level_styles.get(level)
|
|
538
|
+
toc_entry = create_static_toc_entry(
|
|
539
|
+
level, text, bookmark_name,
|
|
540
|
+
style_config=style_config,
|
|
541
|
+
level_style=level_style
|
|
542
|
+
)
|
|
543
|
+
toc_elements.append(toc_entry)
|
|
544
|
+
|
|
545
|
+
# Page break after TOC
|
|
546
|
+
if add_page_break:
|
|
547
|
+
toc_elements.append(create_page_break())
|
|
548
|
+
|
|
549
|
+
# Insert all elements at the correct position
|
|
550
|
+
if first_heading_idx is not None:
|
|
551
|
+
# Insert before the first heading
|
|
552
|
+
for i, elem in enumerate(toc_elements):
|
|
553
|
+
body.insert(first_heading_idx + i, elem)
|
|
554
|
+
else:
|
|
555
|
+
# No heading found, insert at beginning
|
|
556
|
+
for i, elem in enumerate(toc_elements):
|
|
557
|
+
body.insert(i, elem)
|
|
558
|
+
|
|
559
|
+
doc.save(docx_path)
|