forked from python-openxml/python-docx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_numbering_creation.py
More file actions
383 lines (307 loc) · 12.7 KB
/
test_numbering_creation.py
File metadata and controls
383 lines (307 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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
# pyright: reportPrivateUsage=false
"""End-to-end tests for programmatic creation of numbering definitions.
These tests verify that abstract numbering definitions can be created, configured
with various number formats, applied to paragraphs, and survive save/reload cycles.
"""
from __future__ import annotations
from pathlib import Path
import pytest
from docx import Document
from docx.oxml.ns import qn
class DescribeDecimalNumbering:
"""Creating and applying decimal (1. 2. 3.) numbering."""
def it_can_create_a_decimal_numbered_list(self, tmp_path: Path):
doc = Document()
numbering_part = doc.part.numbering_part
# Create abstract numbering definition
abstract_num = numbering_part.add_abstract_num("singleLevel")
abstract_num.add_lvl(
ilvl=0,
num_fmt="decimal",
lvl_text="%1.",
indent_left=720,
indent_hanging=360,
)
# Create concrete num referencing the abstract
num = numbering_part.add_num(abstract_num.abstractNumId)
# Apply to paragraphs
for i in range(3):
p = doc.add_paragraph(f"Item {i + 1}")
pPr = p._element.get_or_add_pPr()
numPr = pPr.get_or_add_numPr()
numPr.get_or_add_numId().val = num.numId
numPr.get_or_add_ilvl().val = 0
# Save and reload
path = tmp_path / "decimal_list.docx"
doc.save(str(path))
doc2 = Document(str(path))
# Verify numbering part exists and has expected content
npart = doc2.part.numbering_part
numbering_elm = npart.numbering_elm
# Check that we have our custom abstract num plus the default ones
abstract_nums = numbering_elm.abstractNum_lst
assert len(abstract_nums) >= 3 # 2 defaults + 1 new
# Find our custom abstract num (it should have the highest abstractNumId)
custom_an = max(abstract_nums, key=lambda an: an.abstractNumId)
assert custom_an.multiLevelType_val == "singleLevel"
assert len(custom_an.lvl_lst) == 1
assert custom_an.lvl_lst[0].numFmt_val == "decimal"
assert custom_an.lvl_lst[0].lvlText_val == "%1."
# Verify paragraphs still have numbering
# Skip default empty paragraph (first paragraph in new document)
numbered_paragraphs = [p for p in doc2.paragraphs if p.text.startswith("Item")]
assert len(numbered_paragraphs) == 3
class DescribeBulletNumbering:
"""Creating and applying bullet list numbering."""
def it_can_create_a_bullet_list(self, tmp_path: Path):
doc = Document()
numbering_part = doc.part.numbering_part
abstract_num = numbering_part.add_abstract_num("singleLevel")
abstract_num.add_lvl(
ilvl=0,
num_fmt="bullet",
lvl_text="\uF0B7",
indent_left=720,
indent_hanging=360,
font_name="Symbol",
)
num = numbering_part.add_num(abstract_num.abstractNumId)
for text in ["Alpha", "Beta", "Gamma"]:
p = doc.add_paragraph(text)
pPr = p._element.get_or_add_pPr()
numPr = pPr.get_or_add_numPr()
numPr.get_or_add_numId().val = num.numId
numPr.get_or_add_ilvl().val = 0
path = tmp_path / "bullet_list.docx"
doc.save(str(path))
# Reload and verify
doc2 = Document(str(path))
npart = doc2.part.numbering_part
abstract_nums = npart.numbering_elm.abstractNum_lst
# Find the bullet abstract num
bullet_ans = [
an for an in abstract_nums
if len(an.lvl_lst) > 0 and an.lvl_lst[0].numFmt_val == "bullet"
and an.lvl_lst[0].rPr is not None
]
assert len(bullet_ans) >= 1
# Verify the bullet has the font specification
bullet_lvl = bullet_ans[-1].lvl_lst[0]
rPr = bullet_lvl.rPr
assert rPr is not None
rFonts = rPr.find(qn("w:rFonts"))
assert rFonts is not None
assert rFonts.get(qn("w:ascii")) == "Symbol"
class DescribeLetterNumbering:
"""Creating letter-based numbering (a. b. c. and A. B. C.)."""
@pytest.mark.parametrize(
("num_fmt", "lvl_text"),
[
("lowerLetter", "%1)"),
("upperLetter", "%1."),
],
)
def it_can_create_a_letter_list(self, num_fmt: str, lvl_text: str, tmp_path: Path):
doc = Document()
numbering_part = doc.part.numbering_part
abstract_num = numbering_part.add_abstract_num("singleLevel")
abstract_num.add_lvl(
ilvl=0,
num_fmt=num_fmt,
lvl_text=lvl_text,
indent_left=720,
indent_hanging=360,
)
num = numbering_part.add_num(abstract_num.abstractNumId)
p = doc.add_paragraph("Test item")
pPr = p._element.get_or_add_pPr()
numPr = pPr.get_or_add_numPr()
numPr.get_or_add_numId().val = num.numId
numPr.get_or_add_ilvl().val = 0
path = tmp_path / f"{num_fmt}_list.docx"
doc.save(str(path))
doc2 = Document(str(path))
abstract_nums = doc2.part.numbering_part.numbering_elm.abstractNum_lst
custom_an = max(abstract_nums, key=lambda an: an.abstractNumId)
assert custom_an.lvl_lst[0].numFmt_val == num_fmt
assert custom_an.lvl_lst[0].lvlText_val == lvl_text
class DescribeRomanNumbering:
"""Creating roman numeral numbering (i. ii. iii. and I. II. III.)."""
@pytest.mark.parametrize(
("num_fmt", "lvl_text"),
[
("lowerRoman", "%1."),
("upperRoman", "%1."),
],
)
def it_can_create_a_roman_list(self, num_fmt: str, lvl_text: str, tmp_path: Path):
doc = Document()
numbering_part = doc.part.numbering_part
abstract_num = numbering_part.add_abstract_num("singleLevel")
abstract_num.add_lvl(
ilvl=0,
num_fmt=num_fmt,
lvl_text=lvl_text,
indent_left=720,
indent_hanging=360,
)
num = numbering_part.add_num(abstract_num.abstractNumId)
p = doc.add_paragraph("Roman item")
pPr = p._element.get_or_add_pPr()
numPr = pPr.get_or_add_numPr()
numPr.get_or_add_numId().val = num.numId
numPr.get_or_add_ilvl().val = 0
path = tmp_path / f"{num_fmt}_list.docx"
doc.save(str(path))
doc2 = Document(str(path))
abstract_nums = doc2.part.numbering_part.numbering_elm.abstractNum_lst
custom_an = max(abstract_nums, key=lambda an: an.abstractNumId)
assert custom_an.lvl_lst[0].numFmt_val == num_fmt
class DescribeMultiLevelNumbering:
"""Creating multi-level numbering definitions with multiple levels."""
def it_can_create_a_multi_level_list(self, tmp_path: Path):
doc = Document()
numbering_part = doc.part.numbering_part
abstract_num = numbering_part.add_abstract_num("multiLevel")
# Define 3 levels
abstract_num.add_lvl(
ilvl=0,
num_fmt="decimal",
lvl_text="%1.",
indent_left=720,
indent_hanging=360,
)
abstract_num.add_lvl(
ilvl=1,
num_fmt="lowerLetter",
lvl_text="%2.",
indent_left=1440,
indent_hanging=360,
)
abstract_num.add_lvl(
ilvl=2,
num_fmt="lowerRoman",
lvl_text="%3.",
indent_left=2160,
indent_hanging=360,
)
num = numbering_part.add_num(abstract_num.abstractNumId)
# Apply levels to paragraphs
for ilvl, text in [(0, "First level"), (1, "Second level"), (2, "Third level")]:
p = doc.add_paragraph(text)
pPr = p._element.get_or_add_pPr()
numPr = pPr.get_or_add_numPr()
numPr.get_or_add_numId().val = num.numId
numPr.get_or_add_ilvl().val = ilvl
path = tmp_path / "multi_level_list.docx"
doc.save(str(path))
doc2 = Document(str(path))
abstract_nums = doc2.part.numbering_part.numbering_elm.abstractNum_lst
custom_an = max(abstract_nums, key=lambda an: an.abstractNumId)
assert custom_an.multiLevelType_val == "multiLevel"
assert len(custom_an.lvl_lst) == 3
assert custom_an.lvl_lst[0].numFmt_val == "decimal"
assert custom_an.lvl_lst[1].numFmt_val == "lowerLetter"
assert custom_an.lvl_lst[2].numFmt_val == "lowerRoman"
def it_can_create_a_nine_level_definition(self, tmp_path: Path):
"""OOXML supports up to 9 levels (ilvl 0-8)."""
doc = Document()
numbering_part = doc.part.numbering_part
abstract_num = numbering_part.add_abstract_num("multiLevel")
num_fmts = [
"decimal", "lowerLetter", "lowerRoman",
"decimal", "lowerLetter", "lowerRoman",
"decimal", "lowerLetter", "lowerRoman",
]
for i in range(9):
abstract_num.add_lvl(
ilvl=i,
num_fmt=num_fmts[i],
lvl_text=f"%{i+1}.",
indent_left=720 * (i + 1),
indent_hanging=360,
)
numbering_part.add_num(abstract_num.abstractNumId)
path = tmp_path / "nine_level_list.docx"
doc.save(str(path))
doc2 = Document(str(path))
abstract_nums = doc2.part.numbering_part.numbering_elm.abstractNum_lst
custom_an = max(abstract_nums, key=lambda an: an.abstractNumId)
assert len(custom_an.lvl_lst) == 9
for i, lvl in enumerate(custom_an.lvl_lst):
assert lvl.ilvl == i
class DescribeMultipleDefinitions:
"""Creating multiple independent numbering definitions in one document."""
def it_supports_multiple_independent_definitions(self, tmp_path: Path):
doc = Document()
numbering_part = doc.part.numbering_part
# Bullet list
an_bullet = numbering_part.add_abstract_num("singleLevel")
an_bullet.add_lvl(
ilvl=0, num_fmt="bullet", lvl_text="\uF0B7",
indent_left=720, indent_hanging=360, font_name="Symbol",
)
num_bullet = numbering_part.add_num(an_bullet.abstractNumId)
# Decimal list
an_decimal = numbering_part.add_abstract_num("singleLevel")
an_decimal.add_lvl(
ilvl=0, num_fmt="decimal", lvl_text="%1.",
indent_left=720, indent_hanging=360,
)
num_decimal = numbering_part.add_num(an_decimal.abstractNumId)
# Apply bullet to first set of paragraphs
for text in ["Bullet A", "Bullet B"]:
p = doc.add_paragraph(text)
pPr = p._element.get_or_add_pPr()
numPr = pPr.get_or_add_numPr()
numPr.get_or_add_numId().val = num_bullet.numId
numPr.get_or_add_ilvl().val = 0
# Apply decimal to next set
for text in ["Number 1", "Number 2"]:
p = doc.add_paragraph(text)
pPr = p._element.get_or_add_pPr()
numPr = pPr.get_or_add_numPr()
numPr.get_or_add_numId().val = num_decimal.numId
numPr.get_or_add_ilvl().val = 0
path = tmp_path / "multiple_defs.docx"
doc.save(str(path))
doc2 = Document(str(path))
npart = doc2.part.numbering_part
# 2 default abstract nums + 2 new ones
assert len(npart.numbering_elm.abstractNum_lst) >= 4
# 2 default nums + 2 new ones
assert len(npart.numbering_elm.num_lst) >= 4
class DescribeLevelOverrides:
"""Level overrides on created numbering definitions."""
def it_can_apply_level_overrides(self, tmp_path: Path):
doc = Document()
numbering_part = doc.part.numbering_part
abstract_num = numbering_part.add_abstract_num("singleLevel")
abstract_num.add_lvl(
ilvl=0,
num_fmt="decimal",
lvl_text="%1.",
start_val=1,
indent_left=720,
indent_hanging=360,
)
num = numbering_part.add_num(abstract_num.abstractNumId)
# Apply level override to restart numbering at 10
lvl_override = num.add_lvlOverride(ilvl=0)
lvl_override.add_startOverride(val=10)
p = doc.add_paragraph("Should start at 10")
pPr = p._element.get_or_add_pPr()
numPr = pPr.get_or_add_numPr()
numPr.get_or_add_numId().val = num.numId
numPr.get_or_add_ilvl().val = 0
path = tmp_path / "level_override.docx"
doc.save(str(path))
# Reload and verify
doc2 = Document(str(path))
npart = doc2.part.numbering_part
# Find our num element
nums = npart.numbering_elm.num_lst
our_num = max(nums, key=lambda n: n.numId)
# Check lvlOverride
overrides = our_num.xpath("./w:lvlOverride")
assert len(overrides) >= 1