Toc
TOC class for “text:table-of-content” tag and TabStopStyle, TocEntryTemplate, IndexBody, IndexTitle, IndexTitleTemplate related classes.
Classes:
| Name | Description |
|---|---|
IndexBody |
Represents the “text:index-body” element, which contains the content of an index. |
IndexTitle |
Represents the title of an index, “text:index-title”. |
IndexTitleTemplate |
Represents a template style for an index title, “text:index-title-template”. |
TOC |
A table of content, “text:table-of-content”. |
TabStopStyle |
Style for a tab-stop in a TOC entry, represented by “style:tab-stop”. |
TocEntryTemplate |
Template for a TOC entry, “text:table-of-content-entry-template”. |
Functions:
| Name | Description |
|---|---|
default_toc_level_style |
Generate an automatic default style for the given TOC level. |
IndexBody
Bases: ListMixin, TocMixin, SectionMixin
Represents the “text:index-body” element, which contains the content of an index.
This element is used for all types of indexes within an ODF document and holds the generated index content.
Source code in odfdo/toc.py
583 584 585 586 587 588 589 590 591 | |
_properties
class-attribute
instance-attribute
_properties: tuple[PropDef | PropDefBool, ...] = ()
_tag
class-attribute
instance-attribute
_tag: str = 'text:index-body'
IndexTitle
Bases: ListMixin, TocMixin, SectionMixin
Represents the title of an index, “text:index-title”.
This element contains the title for an index, and its properties define how the title is displayed and protected.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The name of the index title. |
style |
str
|
The style name applied to the index title. |
xml_id |
str
|
A unique XML identifier. |
protected |
bool
|
Indicates if the index title is protected. |
protection_key |
str
|
The protection key if the title is protected. |
protection_key_digest_algorithm |
str
|
The algorithm used for the protection key. |
Methods:
| Name | Description |
|---|---|
__init__ |
Create an IndexTitle element. |
set_title_text |
Set the actual text content of the index title. |
Source code in odfdo/toc.py
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 | |
_properties
class-attribute
instance-attribute
_properties: tuple[PropDef | PropDefBool, ...] = (
PropDef("name", "text:name"),
PropDef("style", "text:style-name"),
PropDef("xml_id", "xml:id"),
PropDef("protected", "text:protected"),
PropDef("protection_key", "text:protection-key"),
PropDef(
"protection_key_digest_algorithm",
"text:protection-key-digest-algorithm",
),
)
_tag
class-attribute
instance-attribute
_tag = 'text:index-title'
name
instance-attribute
name = name
style
instance-attribute
style = style
xml_id
instance-attribute
xml_id = xml_id
__init__
__init__(
name: str | None = None,
style: str | None = None,
title_text: str | None = None,
title_text_style: str | None = None,
xml_id: str | None = None,
**kwargs: Any,
) -> None
Create an IndexTitle element.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
The name of the index title. |
None
|
style
|
str | None
|
The style name for the index title. |
None
|
title_text
|
str | None
|
The actual text content of the title. |
None
|
title_text_style
|
str | None
|
The style name for the title text. |
None
|
xml_id
|
str | None
|
A unique XML identifier for the title. |
None
|
**kwargs
|
Any
|
Arbitrary keyword arguments for the Element base class. |
{}
|
Source code in odfdo/toc.py
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 | |
set_title_text
set_title_text(
title_text: str, title_text_style: str | None = None
) -> None
Set the actual text content of the index title.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title_text
|
str
|
The text content to set for the title. |
required |
title_text_style
|
str | None
|
The style name for the title text. |
None
|
Source code in odfdo/toc.py
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 | |
IndexTitleTemplate
Bases: Element
Represents a template style for an index title, “text:index-title-template”.
This element defines the styling for index titles within an ODF document.
Methods:
| Name | Description |
|---|---|
__init__ |
Create an IndexTitleTemplate element. |
Attributes:
| Name | Type | Description |
|---|---|---|
style |
|
Source code in odfdo/toc.py
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 | |
_properties
class-attribute
instance-attribute
_properties: tuple[PropDef | PropDefBool, ...] = (
PropDef("style", "text:style-name"),
)
_tag
class-attribute
instance-attribute
_tag = 'text:index-title-template'
style
instance-attribute
style = style
__init__
__init__(style: str | None = None, **kwargs: Any) -> None
Create an IndexTitleTemplate element.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
style
|
str | None
|
The style name for the template. |
None
|
**kwargs
|
Any
|
Arbitrary keyword arguments for the Element base class. |
{}
|
Source code in odfdo/toc.py
685 686 687 688 689 690 691 692 693 694 | |
TOC
Bases: MDToc, Element
A table of content, “text:table-of-content”.
The “text:table-of-content” element represents a table of contents for a document. The items that can be listed in a table of contents are:
-
Headings (as defined by the outline structure of the document), up to a selected level.
-
Table of contents index marks.
-
Paragraphs formatted with specified paragraph styles.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The name of the table of contents. |
style |
str
|
The style name for the TOC. |
xml_id |
str
|
The XML ID of the TOC. |
protected |
bool
|
Whether the TOC is protected from manual changes. |
protection_key |
str
|
The key for protection. |
protection_key_digest_algorithm |
str
|
The algorithm for the protection key digest. |
Methods:
| Name | Description |
|---|---|
__init__ |
Initializes a TOC (Table of Contents) element. |
__str__ |
|
create_toc_source |
Create the ‘text:table-of-content-source’ element for a TOC. |
fill |
Fill the TOC with titles from the document. |
get_formatted_text |
Returns the formatted text content of the TOC. |
get_title |
Returns the title of the TOC. |
set_toc_title |
Sets the title of the TOC. |
Source code in odfdo/toc.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 | |
_properties
class-attribute
instance-attribute
_properties = (
PropDef("name", "text:name"),
PropDef("style", "text:style-name"),
PropDef("xml_id", "xml:id"),
PropDef("protected", "text:protected"),
PropDef("protection_key", "text:protection-key"),
PropDef(
"protection_key_digest_algorithm",
"text:protection-key-digest-algorithm",
),
)
_tag
class-attribute
instance-attribute
_tag = 'text:table-of-content'
body
property
writable
body: IndexBody | None
The body of the TOC (‘text:index-body’).
This property provides access to the ‘text:index-body’ element, which contains the actual entries of the table of contents.
name
instance-attribute
name = f'{title}1'
outline_level
property
writable
outline_level: int | None
The outline level of the TOC.
This property controls the maximum heading level that will be included in the table of contents. A value of 0 means all levels are included.
protected
instance-attribute
protected = protected
style
instance-attribute
style = style
__init__
__init__(
title: str = "Table of Contents",
name: str | None = None,
protected: bool = True,
outline_level: int = 0,
style: str | None = None,
title_style: str = "Contents_20_Heading",
entry_style: str = "Contents_20_%d",
**kwargs: Any,
) -> None
Initializes a TOC (Table of Contents) element.
Default parameters are what most people use: protected from manual modifications and not limited in title levels.
The name is mandatory and derived automatically from the title if not given. Provide one in case of a conflict with other TOCs in the same document.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
The title of the TOC (e.g., “Table of Contents”). |
'Table of Contents'
|
name
|
str | None
|
The name of the TOC element. If not provided, it’s generated from the title. |
None
|
protected
|
bool
|
If True, the TOC is protected from manual modifications. |
True
|
outline_level
|
int
|
The maximum outline level to include in the TOC. 0 means all levels. |
0
|
style
|
str | None
|
The style name for the TOC container. |
None
|
title_style
|
str
|
The style for the TOC’s main title. |
'Contents_20_Heading'
|
entry_style
|
str
|
A format string for the style of each TOC entry (e.g., “Contents_20_%d”). |
'Contents_20_%d'
|
Source code in odfdo/toc.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |
__str__
__str__() -> str
Source code in odfdo/toc.py
289 290 | |
_header_numbering
staticmethod
_header_numbering(
level_indexes: dict[int, int], level: int
) -> str
Return the header hierarchical number (like “1.2.3.”).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level_indexes
|
dict[int, int]
|
A dictionary to track numbering at each level. |
required |
level
|
int
|
The current header level. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The hierarchical number string. |
Source code in odfdo/toc.py
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | |
create_toc_source
staticmethod
create_toc_source(
title: str,
outline_level: int,
title_style: str,
entry_style: str,
) -> Element
Create the ‘text:table-of-content-source’ element for a TOC.
This element defines the structure and appearance of the TOC.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
The title of the TOC. |
required |
outline_level
|
int
|
The maximum outline level to include. |
required |
title_style
|
str
|
The style for the TOC’s title. |
required |
entry_style
|
str
|
The format string for entry styles. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Element |
Element
|
The newly created ‘text:table-of-content-source’ element. |
Source code in odfdo/toc.py
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | |
fill
fill(
document: Document | None = None,
use_default_styles: bool = True,
) -> None
Fill the TOC with titles from the document.
This method populates the table of contents by scanning the document for headers. It is not contextual and will include all titles from the document, regardless of their position relative to the TOC.
If the TOC is not yet part of a document, you must provide one as an argument.
For a well-formatted TOC, it’s recommended to leave
use_default_styles as True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
document
|
Document | None
|
The document to scan for titles. If not provided, the TOC must already be attached to a document. |
None
|
use_default_styles
|
bool
|
If True, applies default styles to the TOC entries. |
True
|
Source code in odfdo/toc.py
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 | |
get_formatted_text
get_formatted_text(context: dict | None = None) -> str
Returns the formatted text content of the TOC.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
dict | None
|
A context dictionary for formatting. If ‘rst_mode’ is True, it returns a reStructuredText ‘contents’ directive. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The formatted text of the TOC. |
Source code in odfdo/toc.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | |
get_title
get_title() -> str
Returns the title of the TOC.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The title of the TOC. |
Source code in odfdo/toc.py
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | |
set_toc_title
set_toc_title(
title: str,
style: str | None = None,
text_style: str | None = None,
) -> None
Sets the title of the TOC.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
The new title for the TOC. |
required |
style
|
str | None
|
The style for the index title element. |
None
|
text_style
|
str | None
|
The style for the title’s paragraph. |
None
|
Source code in odfdo/toc.py
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 | |
TabStopStyle
Bases: Element
Style for a tab-stop in a TOC entry, represented by “style:tab-stop”.
This class defines the visual properties of tab stops, such as their position, alignment, and leader style.
Attributes:
| Name | Type | Description |
|---|---|---|
style_char |
str
|
The character used for the tab stop. |
leader_color |
str
|
The color of the leader line. |
leader_style |
str
|
The style of the leader line (e.g., ‘dotted’, ‘solid’). |
leader_text |
str
|
The text to use as a leader. |
leader_text_style |
str
|
The style of the leader text. |
leader_type |
str
|
The type of leader (e.g., ‘none’, ‘solid’). |
leader_width |
str
|
The width of the leader line. |
style_position |
str
|
The position of the tab stop (e.g., ‘1.25cm’). |
style_type |
str
|
The alignment type of the tab stop (e.g., ‘left’, ‘right’). |
Methods:
| Name | Description |
|---|---|
__init__ |
Initializes a TabStopStyle element. |
Source code in odfdo/toc.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
_properties
class-attribute
instance-attribute
_properties = (
PropDef("style_char", "style:char"),
PropDef("leader_color", "style:leader-color"),
PropDef("leader_style", "style:leader-style"),
PropDef("leader_text", "style:leader-text"),
PropDef("leader_text_style", "style:leader-text-style"),
PropDef("leader_type", "style:leader-type"),
PropDef("leader_width", "style:leader-width"),
PropDef("style_position", "style:position"),
PropDef("style_type", "style:type"),
)
_tag
class-attribute
instance-attribute
_tag = 'style:tab-stop'
leader_color
instance-attribute
leader_color = leader_color
leader_style
instance-attribute
leader_style = leader_style
leader_text
instance-attribute
leader_text = leader_text
leader_text_style
instance-attribute
leader_text_style = leader_text_style
leader_type
instance-attribute
leader_type = leader_type
leader_width
instance-attribute
leader_width = leader_width
style_char
instance-attribute
style_char = style_char
style_position
instance-attribute
style_position = style_position
style_type
instance-attribute
style_type = style_type
__init__
__init__(
style_char: str | None = None,
leader_color: str | None = None,
leader_style: str | None = None,
leader_text: str | None = None,
leader_text_style: str | None = None,
leader_type: str | None = None,
leader_width: str | None = None,
style_position: str | None = None,
style_type: str | None = None,
**kwargs: Any,
)
Initializes a TabStopStyle element.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
style_char
|
str | None
|
The character for the tab stop. |
None
|
leader_color
|
str | None
|
Color of the leader line. |
None
|
leader_style
|
str | None
|
Style of the leader line. |
None
|
leader_text
|
str | None
|
Text to use as a leader. |
None
|
leader_text_style
|
str | None
|
Style of the leader text. |
None
|
leader_type
|
str | None
|
Type of leader. |
None
|
leader_width
|
str | None
|
Width of the leader line. |
None
|
style_position
|
str | None
|
Position of the tab stop. |
None
|
style_type
|
str | None
|
Alignment type of the tab stop. |
None
|
Source code in odfdo/toc.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
TocEntryTemplate
Bases: Element
Template for a TOC entry, “text:table-of-content-entry-template”.
This element defines the structure for each entry in the table of contents, specifying what information is displayed (e.g., chapter number, text, page number) and how it is styled.
Attributes:
| Name | Type | Description |
|---|---|---|
style |
str
|
The style name for the entry. |
Methods:
| Name | Description |
|---|---|
__init__ |
Initializes a TocEntryTemplate element. |
complete_defaults |
Populates the template with default entry elements. |
Source code in odfdo/toc.py
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 | |
_properties
class-attribute
instance-attribute
_properties = (PropDef('style', 'text:style-name'),)
_tag
class-attribute
instance-attribute
_tag = 'text:table-of-content-entry-template'
outline_level
property
writable
outline_level: int | None
The outline level this template applies to.
style
instance-attribute
style = style
__init__
__init__(
style: str | None = None,
outline_level: int | None = None,
**kwargs: Any,
) -> None
Initializes a TocEntryTemplate element.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
style
|
str | None
|
The style name for the TOC entry. |
None
|
outline_level
|
int | None
|
The outline level this template applies to. |
None
|
Source code in odfdo/toc.py
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 | |
complete_defaults
complete_defaults() -> None
Populates the template with default entry elements.
This method adds standard elements to the template, such as placeholders for chapter number, entry text, and page number, providing a default structure for a TOC entry.
Source code in odfdo/toc.py
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 | |
_toc_entry_style_name
_toc_entry_style_name(level: int) -> str
Return the style name of an entry of the TOC.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
int
|
The outline level of the TOC entry. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The generated style name. |
Source code in odfdo/toc.py
41 42 43 44 45 46 47 48 49 50 | |
default_toc_level_style
default_toc_level_style(level: int) -> Style
Generate an automatic default style for the given TOC level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
int
|
The outline level for which to create the style. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Style |
Style
|
The generated style for the TOC level. |
Source code in odfdo/toc.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |