Skip to content

Commit 277b46c

Browse files
author
Steve Canny
committed
run: add Run.font
1 parent bcb0907 commit 277b46c

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

docx/text/run.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class Run(Parented):
6565
"""
6666
def __init__(self, r, parent):
6767
super(Run, self).__init__(parent)
68-
self._r = r
68+
self._r = self._element = self.element = r
6969

7070
def add_break(self, break_type=WD_BREAK.LINE):
7171
"""
@@ -199,6 +199,14 @@ def emboss(self):
199199
"""
200200
return 'emboss'
201201

202+
@property
203+
def font(self):
204+
"""
205+
The |Font| object providing access to the character formatting
206+
properties for this run, such as font name and size.
207+
"""
208+
return Font(self._element)
209+
202210
@boolproperty
203211
def hidden(self):
204212
"""

features/run-access-font.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Feature: Access run font
44
I need access to the font of a run
55

66

7-
@wip
87
Scenario: Access the font of a run
98
Given a run
109
Then run.font is the Font object for the run

tests/text/test_run.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ def it_raises_on_assign_invalid_underline_type(
5656
with pytest.raises(ValueError):
5757
run.underline = underline
5858

59+
def it_provides_access_to_its_font(self, font_fixture):
60+
run, Font_, font_ = font_fixture
61+
font = run.font
62+
Font_.assert_called_once_with(run._element)
63+
assert font is font_
64+
5965
def it_can_add_text(self, add_text_fixture):
6066
run, text_str, expected_xml, Text_ = add_text_fixture
6167
_text = run.add_text(text_str)
@@ -257,6 +263,11 @@ def clear_fixture(self, request):
257263
expected_xml = xml(expected_cxml)
258264
return run, expected_xml
259265

266+
@pytest.fixture
267+
def font_fixture(self, Font_, font_):
268+
run = Run(element('w:r'), None)
269+
return run, Font_, font_
270+
260271
@pytest.fixture(params=[
261272
('w:r', None),
262273
('w:r/w:rPr/w:rStyle{w:val=Foobar}', 'Foobar'),
@@ -350,6 +361,14 @@ def underline_raise_fixture(self, request):
350361

351362
# fixture components ---------------------------------------------
352363

364+
@pytest.fixture
365+
def Font_(self, request, font_):
366+
return class_mock(request, 'docx.text.run.Font', return_value=font_)
367+
368+
@pytest.fixture
369+
def font_(self, request):
370+
return instance_mock(request, Font)
371+
353372
@pytest.fixture
354373
def inline_shapes_(self, request, picture_):
355374
inline_shapes_ = instance_mock(request, InlineShapes)

0 commit comments

Comments
 (0)