|
2 | 2 |
|
3 | 3 | from __future__ import annotations
|
4 | 4 |
|
| 5 | +from warnings import warn |
| 6 | + |
5 | 7 | from copy import deepcopy
|
6 | 8 | from typing import Callable, Iterator, List, Sequence, cast
|
7 | 9 |
|
|
11 | 13 | from docx.enum.section import WD_HEADER_FOOTER, WD_ORIENTATION, WD_SECTION_START
|
12 | 14 | from docx.oxml.ns import nsmap
|
13 | 15 | from docx.oxml.shared import CT_OnOff
|
14 |
| -from docx.oxml.simpletypes import ST_SignedTwipsMeasure, ST_TwipsMeasure, XsdString |
| 16 | +from docx.oxml.simpletypes import ST_SignedTwipsMeasure, ST_TwipsMeasure, XsdString, ST_FtnPos, ST_NumberFormat, ST_RestartNumber |
15 | 17 | from docx.oxml.table import CT_Tbl
|
16 | 18 | from docx.oxml.text.paragraph import CT_P
|
17 | 19 | from docx.oxml.xmlchemy import (
|
|
26 | 28 | BlockElement: TypeAlias = "CT_P | CT_Tbl"
|
27 | 29 |
|
28 | 30 |
|
| 31 | +class CT_FtnPos(BaseOxmlElement): |
| 32 | + """``<w:pos>`` element, footnote placement""" |
| 33 | + val = RequiredAttribute('w:val', ST_FtnPos) |
| 34 | + |
| 35 | + |
| 36 | +class CT_FtnProps(BaseOxmlElement): |
| 37 | + """``<w:footnotePr>`` element, section wide footnote properties""" |
| 38 | + _tag_seq = ( |
| 39 | + 'w:pos', 'w:numFmt', 'w:numStart', 'w:numRestart' |
| 40 | + ) |
| 41 | + pos = ZeroOrOne('w:pos', successors=_tag_seq) |
| 42 | + numFmt = ZeroOrOne('w:numFmt', successors=_tag_seq[1:]) |
| 43 | + numStart = ZeroOrOne('w:numStart', successors=_tag_seq[2:]) |
| 44 | + numRestart = ZeroOrOne('w:numRestart', successors=_tag_seq[3:]) |
| 45 | + |
| 46 | + |
29 | 47 | class CT_HdrFtr(BaseOxmlElement):
|
30 | 48 | """`w:hdr` and `w:ftr`, the root element for header and footer part respectively."""
|
31 | 49 |
|
@@ -57,6 +75,16 @@ class CT_HdrFtrRef(BaseOxmlElement):
|
57 | 75 | rId: str = RequiredAttribute("r:id", XsdString) # pyright: ignore[reportAssignmentType]
|
58 | 76 |
|
59 | 77 |
|
| 78 | +class CT_NumFmt(BaseOxmlElement): |
| 79 | + """``<w:numFmt>`` element, footnote numbering format""" |
| 80 | + val = RequiredAttribute('w:val', ST_NumberFormat) |
| 81 | + |
| 82 | + |
| 83 | +class CT_NumRestart(BaseOxmlElement): |
| 84 | + """``<w:numStart>`` element, footnote numbering restart location""" |
| 85 | + val = RequiredAttribute('w:val', ST_RestartNumber) |
| 86 | + |
| 87 | + |
60 | 88 | class CT_PageMar(BaseOxmlElement):
|
61 | 89 | """``<w:pgMar>`` element, defining page margins."""
|
62 | 90 |
|
@@ -145,6 +173,7 @@ class CT_SectPr(BaseOxmlElement):
|
145 | 173 | titlePg: CT_OnOff | None = ZeroOrOne( # pyright: ignore[reportAssignmentType]
|
146 | 174 | "w:titlePg", successors=_tag_seq[14:]
|
147 | 175 | )
|
| 176 | + footnotePr = ZeroOrOne("w:footnotePr", successors=_tag_seq[1:]) |
148 | 177 | del _tag_seq
|
149 | 178 |
|
150 | 179 | def add_footerReference(self, type_: WD_HEADER_FOOTER, rId: str) -> CT_HdrFtrRef:
|
@@ -211,6 +240,92 @@ def footer(self, value: int | Length | None):
|
211 | 240 | pgMar = self.get_or_add_pgMar()
|
212 | 241 | pgMar.footer = value if value is None or isinstance(value, Length) else Length(value)
|
213 | 242 |
|
| 243 | + @property |
| 244 | + def footnote_number_format(self): |
| 245 | + """ |
| 246 | + The value of the ``w:val`` attribute in the ``<w:numFmt>`` child |
| 247 | + element of ``<w:footnotePr>`` element, as a |String|, or |None| if either the element or the |
| 248 | + attribute is not present. |
| 249 | + """ |
| 250 | + fPr = self.footnotePr |
| 251 | + if fPr is None or fPr.numFmt: |
| 252 | + return None |
| 253 | + return fPr.numFmt.val |
| 254 | + |
| 255 | + @footnote_number_format.setter |
| 256 | + def footnote_number_format(self, value): |
| 257 | + fPr = self.get_or_add_footnotePr() |
| 258 | + numFmt = fPr.get_or_add_numFmt() |
| 259 | + numFmt.val = value |
| 260 | + |
| 261 | + @property |
| 262 | + def footnote_numbering_restart_location(self): |
| 263 | + """ |
| 264 | + The value of the ``w:val`` attribute in the ``<w:numRestart>`` child |
| 265 | + element of ``<w:footnotePr>`` element, as a |String|, or |None| if either the element or the |
| 266 | + attribute is not present. |
| 267 | + """ |
| 268 | + fPr = self.footnotePr |
| 269 | + if fPr is None or fPr.numRestart: |
| 270 | + return None |
| 271 | + return fPr.numRestart.val |
| 272 | + |
| 273 | + @footnote_numbering_restart_location.setter |
| 274 | + def footnote_numbering_restart_location(self, value): |
| 275 | + fPr = self.get_or_add_footnotePr() |
| 276 | + numStart = fPr.get_or_add_numStart() |
| 277 | + numRestart = fPr.get_or_add_numRestart() |
| 278 | + numRestart.val = value |
| 279 | + if numStart is None or len(numStart.values()) == 0: |
| 280 | + numStart.val = 1 |
| 281 | + elif value != 'continuous': |
| 282 | + numStart.val = 1 |
| 283 | + msg = "When ``<w:numRestart> is not 'continuous', then ``<w:numStart>`` must be 1." |
| 284 | + warn(msg, UserWarning, stacklevel=2) |
| 285 | + |
| 286 | + @property |
| 287 | + def footnote_numbering_start_value(self): |
| 288 | + """ |
| 289 | + The value of the ``w:val`` attribute in the ``<w:numStart>`` child |
| 290 | + element of ``<w:footnotePr>`` element, as a |Number|, or |None| if either the element or the |
| 291 | + attribute is not present. |
| 292 | + """ |
| 293 | + fPr = self.footnotePr |
| 294 | + if fPr is None or fPr.numStart: |
| 295 | + return None |
| 296 | + return fPr.numStart.val |
| 297 | + |
| 298 | + @footnote_numbering_start_value.setter |
| 299 | + def footnote_numbering_start_value(self, value): |
| 300 | + fPr = self.get_or_add_footnotePr() |
| 301 | + numStart = fPr.get_or_add_numStart() |
| 302 | + numRestart = fPr.get_or_add_numRestart() |
| 303 | + numStart.val = value |
| 304 | + if numRestart is None or len(numRestart.values()) == 0: |
| 305 | + numRestart.val = 'continuous' |
| 306 | + elif value != 1: |
| 307 | + numRestart.val = 'continuous' |
| 308 | + msg = "When ``<w:numStart> is not 1, then ``<w:numRestart>`` must be 'continuous'." |
| 309 | + warn(msg, UserWarning, stacklevel=2) |
| 310 | + |
| 311 | + @property |
| 312 | + def footnote_position(self): |
| 313 | + """ |
| 314 | + The value of the ``w:val`` attribute in the ``<w:pos>`` child |
| 315 | + element of ``<w:footnotePr>`` element, as a |String|, or |None| if either the element or the |
| 316 | + attribute is not present. |
| 317 | + """ |
| 318 | + fPr = self.footnotePr |
| 319 | + if fPr is None or fPr.pos is None: |
| 320 | + return None |
| 321 | + return fPr.pos.val |
| 322 | + |
| 323 | + @footnote_position.setter |
| 324 | + def footnote_position(self, value): |
| 325 | + fPr = self.get_or_add_footnotePr() |
| 326 | + pos = fPr.get_or_add_pos() |
| 327 | + pos.val = value |
| 328 | + |
214 | 329 | def get_footerReference(self, type_: WD_HEADER_FOOTER) -> CT_HdrFtrRef | None:
|
215 | 330 | """Return footerReference element of `type_` or None if not present."""
|
216 | 331 | path = "./w:footerReference[@w:type='%s']" % WD_HEADER_FOOTER.to_xml(type_)
|
|
0 commit comments