Skip to content

Commit b28b6cd

Browse files
stevecohen42Steve Canny
authored andcommitted
fix: python-openxml#140 warning triggered on add_heading/table()
Legacy style ids were used in Document api methods for heading styles and default table style. Update tests, docstrings, and values to reflect style name instead of style id.
1 parent 3ec35da commit b28b6cd

File tree

2 files changed

+35
-39
lines changed

2 files changed

+35
-39
lines changed

docx/api.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ def __init__(self, docx=None):
3838
def add_heading(self, text='', level=1):
3939
"""
4040
Return a heading paragraph newly added to the end of the document,
41-
populated with *text* and having the heading paragraph style
42-
determined by *level*. If *level* is 0, the style is set to
43-
``'Title'``. If *level* is 1 (or not present), ``'Heading1'`` is used.
44-
Otherwise the style is set to ``'Heading{level}'``. If *level* is
45-
outside the range 0-9, |ValueError| is raised.
41+
containing *text* and having its paragraph style determined by
42+
*level*. If *level* is 0, the style is set to `Title`. If *level* is
43+
1 (or omitted), `Heading 1` is used. Otherwise the style is set to
44+
`Heading {level}`. Raises |ValueError| if *level* is outside the
45+
range 0-9.
4646
"""
4747
if not 0 <= level <= 9:
4848
raise ValueError("level must be in range 0-9, got %d" % level)
49-
style = 'Title' if level == 0 else 'Heading%d' % level
49+
style = 'Title' if level == 0 else 'Heading %d' % level
5050
return self.add_paragraph(text, style)
5151

5252
def add_page_break(self):
@@ -96,15 +96,14 @@ def add_section(self, start_type=WD_SECTION.NEW_PAGE):
9696
"""
9797
return self._document_part.add_section(start_type)
9898

99-
def add_table(self, rows, cols, style='LightShading-Accent1'):
99+
def add_table(self, rows, cols, style='Light Shading Accent 1'):
100100
"""
101101
Add a table having row and column counts of *rows* and *cols*
102102
respectively and table style of *style*. If *style* is |None|, a
103103
table with no style is produced.
104104
"""
105105
table = self._document_part.add_table(rows, cols)
106-
if style:
107-
table.style = style
106+
table.style = style
108107
return table
109108

110109
@property

tests/test_api.py

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,9 @@ def it_should_raise_if_not_a_Word_file(self, Package_, package_, docx_):
5555
Document._open(docx_)
5656

5757
def it_can_add_a_heading(self, add_heading_fixture):
58-
document, add_paragraph_, paragraph_, text, level, style = (
59-
add_heading_fixture
60-
)
58+
document, text, level, style, paragraph_ = add_heading_fixture
6159
paragraph = document.add_heading(text, level)
62-
add_paragraph_.assert_called_once_with(text, style)
60+
document.add_paragraph.assert_called_once_with(text, style)
6361
assert paragraph is paragraph_
6462

6563
def it_should_raise_on_heading_level_out_of_range(self, document):
@@ -69,11 +67,11 @@ def it_should_raise_on_heading_level_out_of_range(self, document):
6967
document.add_heading(level=10)
7068

7169
def it_can_add_a_paragraph(self, add_paragraph_fixture):
72-
document, document_part_, text, style, paragraph_ = (
73-
add_paragraph_fixture
74-
)
70+
document, text, style, paragraph_ = add_paragraph_fixture
7571
paragraph = document.add_paragraph(text, style)
76-
document_part_.add_paragraph.assert_called_once_with(text, style)
72+
document._document_part.add_paragraph.assert_called_once_with(
73+
text, style
74+
)
7775
assert paragraph is paragraph_
7876

7977
def it_can_add_a_page_break(self, add_page_break_fixture):
@@ -101,12 +99,10 @@ def it_can_add_a_section(self, add_section_fixture):
10199
assert section is section_
102100

103101
def it_can_add_a_table(self, add_table_fixture):
104-
document, rows, cols, style, document_part_, expected_style, table_ = (
105-
add_table_fixture
106-
)
102+
document, rows, cols, style, table_ = add_table_fixture
107103
table = document.add_table(rows, cols, style)
108-
document_part_.add_table.assert_called_once_with(rows, cols)
109-
assert table.style == expected_style
104+
document._document_part.add_table.assert_called_once_with(rows, cols)
105+
assert table.style == style
110106
assert table == table_
111107

112108
def it_provides_access_to_the_document_inline_shapes(self, document):
@@ -165,21 +161,25 @@ def it_creates_numbering_part_on_first_access_if_not_present(
165161

166162
@pytest.fixture(params=[
167163
('', None),
168-
('', 'Heading1'),
169-
('foo\rbar', 'BodyText'),
164+
('', 'Heading 1'),
165+
('foo\rbar', 'Body Text'),
170166
])
171-
def add_paragraph_fixture(
172-
self, request, document, document_part_, paragraph_):
167+
def add_paragraph_fixture(self, request, document, document_part_,
168+
paragraph_):
173169
text, style = request.param
174-
return document, document_part_, text, style, paragraph_
170+
return document, text, style, paragraph_
175171

176-
@pytest.fixture(params=[0, 1, 2, 5, 9])
177-
def add_heading_fixture(
178-
self, request, document, add_paragraph_, paragraph_):
179-
level = request.param
172+
@pytest.fixture(params=[
173+
(0, 'Title'),
174+
(1, 'Heading 1'),
175+
(2, 'Heading 2'),
176+
(9, 'Heading 9'),
177+
])
178+
def add_heading_fixture(self, request, document, add_paragraph_,
179+
paragraph_):
180+
level, style = request.param
180181
text = 'Spam vs. Bacon'
181-
style = 'Title' if level == 0 else 'Heading%d' % level
182-
return document, add_paragraph_, paragraph_, text, level, style
182+
return document, text, level, style, paragraph_
183183

184184
@pytest.fixture
185185
def add_page_break_fixture(
@@ -199,14 +199,11 @@ def add_picture_fixture(self, request, run_, picture_):
199199
def add_section_fixture(self, document, start_type_, section_):
200200
return document, start_type_, section_
201201

202-
@pytest.fixture(params=[None, 'LightShading-Accent1', 'foobar'])
202+
@pytest.fixture
203203
def add_table_fixture(self, request, document, document_part_, table_):
204204
rows, cols = 4, 2
205-
style = expected_style = request.param
206-
return (
207-
document, rows, cols, style, document_part_, expected_style,
208-
table_
209-
)
205+
style = 'Light Shading Accent 1'
206+
return document, rows, cols, style, table_
210207

211208
@pytest.fixture
212209
def core_props_fixture(self, document, core_properties_):

0 commit comments

Comments
 (0)