|
4 | 4 | Custom element classes related to the numbering part |
5 | 5 | """ |
6 | 6 |
|
7 | | -from docx.oxml.shared import OxmlBaseElement, qn |
| 7 | +from docx.oxml.shared import ( |
| 8 | + CT_DecimalNumber, nsmap, OxmlBaseElement, OxmlElement, qn |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +class CT_Num(OxmlBaseElement): |
| 13 | + """ |
| 14 | + ``<w:num>`` element, which represents a concrete list definition |
| 15 | + instance, having a required child <w:abstractNumId> that references an |
| 16 | + abstract numbering definition that defines most of the formatting details. |
| 17 | + """ |
| 18 | + @property |
| 19 | + def abstractNumId(self): |
| 20 | + return self.find(qn('w:abstractNumId')) |
| 21 | + |
| 22 | + def add_lvlOverride(self, ilvl): |
| 23 | + """ |
| 24 | + Return a newly added CT_NumLvl (<w:lvlOverride>) element having its |
| 25 | + ``ilvl`` attribute set to *ilvl*. |
| 26 | + """ |
| 27 | + lvlOverride = CT_NumLvl.new(ilvl) |
| 28 | + self.append(lvlOverride) |
| 29 | + return lvlOverride |
| 30 | + |
| 31 | + @classmethod |
| 32 | + def new(cls, num_id, abstractNum_id): |
| 33 | + """ |
| 34 | + Return a new ``<w:num>`` element having numId of *num_id* and having |
| 35 | + a ``<w:abstractNumId>`` child with val attribute set to |
| 36 | + *abstractNum_id*. |
| 37 | + """ |
| 38 | + abstractNumId = CT_DecimalNumber.new( |
| 39 | + 'w:abstractNumId', abstractNum_id |
| 40 | + ) |
| 41 | + num = OxmlElement('w:num', {qn('w:numId'): str(num_id)}) |
| 42 | + num.append(abstractNumId) |
| 43 | + return num |
| 44 | + |
| 45 | + @property |
| 46 | + def numId(self): |
| 47 | + numId_str = self.get(qn('w:numId')) |
| 48 | + return int(numId_str) |
| 49 | + |
| 50 | + |
| 51 | +class CT_NumLvl(OxmlBaseElement): |
| 52 | + """ |
| 53 | + ``<w:lvlOverride>`` element, which identifies a level in a list |
| 54 | + definition to override with settings it contains. |
| 55 | + """ |
| 56 | + def add_startOverride(self, val): |
| 57 | + """ |
| 58 | + Return a newly added CT_DecimalNumber element having tagname |
| 59 | + ``w:startOverride`` and ``val`` attribute set to *val*. |
| 60 | + """ |
| 61 | + startOverride = CT_DecimalNumber.new('w:startOverride', val) |
| 62 | + self.insert(0, startOverride) |
| 63 | + return startOverride |
| 64 | + |
| 65 | + @classmethod |
| 66 | + def new(cls, ilvl): |
| 67 | + """ |
| 68 | + Return a new ``<w:lvlOverride>`` element having its ``ilvl`` |
| 69 | + attribute set to *ilvl*. |
| 70 | + """ |
| 71 | + return OxmlElement('w:lvlOverride', {qn('w:ilvl'): str(ilvl)}) |
8 | 72 |
|
9 | 73 |
|
10 | 74 | class CT_Numbering(OxmlBaseElement): |
11 | 75 | """ |
12 | 76 | ``<w:numbering>`` element, the root element of a numbering part, i.e. |
13 | 77 | numbering.xml |
14 | 78 | """ |
| 79 | + def add_num(self, abstractNum_id): |
| 80 | + """ |
| 81 | + Return a newly added CT_Num (<w:num>) element that references |
| 82 | + the abstract numbering definition having id *abstractNum_id*. |
| 83 | + """ |
| 84 | + next_num_id = self._next_numId |
| 85 | + num = CT_Num.new(next_num_id, abstractNum_id) |
| 86 | + # insert in proper sequence among children --------- |
| 87 | + successor = self.first_child_found_in('w:numIdMacAtCleanup') |
| 88 | + if successor is not None: |
| 89 | + successor.addprevious(num) |
| 90 | + else: |
| 91 | + self.append(num) |
| 92 | + return num |
| 93 | + |
15 | 94 | @property |
16 | 95 | def num_lst(self): |
17 | 96 | """ |
18 | 97 | List of <w:num> child elements. |
19 | 98 | """ |
20 | 99 | return self.findall(qn('w:num')) |
| 100 | + |
| 101 | + def num_having_numId(self, numId): |
| 102 | + """ |
| 103 | + Return the ``<w:num>`` child element having ``numId`` attribute |
| 104 | + matching *numId*. |
| 105 | + """ |
| 106 | + xpath = './w:num[@w:numId="%d"]' % numId |
| 107 | + try: |
| 108 | + return self.xpath(xpath, namespaces=nsmap)[0] |
| 109 | + except IndexError: |
| 110 | + raise KeyError('no <w:num> element with numId %d' % numId) |
| 111 | + |
| 112 | + @property |
| 113 | + def _next_numId(self): |
| 114 | + """ |
| 115 | + The first ``numId`` unused by a ``<w:num>`` element, starting at |
| 116 | + 1 and filling any gaps in numbering between existing ``<w:num>`` |
| 117 | + elements. |
| 118 | + """ |
| 119 | + numId_strs = self.xpath('./w:num/@w:numId', namespaces=nsmap) |
| 120 | + num_ids = [int(numId_str) for numId_str in numId_strs] |
| 121 | + for num in range(1, len(num_ids)+2): |
| 122 | + if num not in num_ids: |
| 123 | + break |
| 124 | + return num |
0 commit comments