Skip to content

Commit

Permalink
para: add Paragraph.alignment getter
Browse files Browse the repository at this point in the history
  • Loading branch information
onlyjus authored and Steve Canny committed Jun 27, 2014
1 parent d7f2bef commit 1375dc9
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 10 deletions.
3 changes: 2 additions & 1 deletion docx/oxml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def OxmlElement(nsptag_str, attrs=None, nsdecls=None):
register_element_cls('w:tr', CT_Row)

from docx.oxml.text import (
CT_Br, CT_P, CT_PPr, CT_R, CT_RPr, CT_Text, CT_Underline
CT_Br, CT_Jc, CT_P, CT_PPr, CT_R, CT_RPr, CT_Text, CT_Underline
)
register_element_cls('w:b', CT_OnOff)
register_element_cls('w:bCs', CT_OnOff)
Expand All @@ -137,6 +137,7 @@ def OxmlElement(nsptag_str, attrs=None, nsdecls=None):
register_element_cls('w:i', CT_OnOff)
register_element_cls('w:iCs', CT_OnOff)
register_element_cls('w:imprint', CT_OnOff)
register_element_cls('w:jc', CT_Jc)
register_element_cls('w:noProof', CT_OnOff)
register_element_cls('w:oMath', CT_OnOff)
register_element_cls('w:outline', CT_OnOff)
Expand Down
34 changes: 32 additions & 2 deletions docx/oxml/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
(CT_R).
"""

from ..enum.text import WD_UNDERLINE
from ..enum.text import WD_ALIGN_PARAGRAPH, WD_UNDERLINE
from .ns import qn
from .simpletypes import ST_BrClear, ST_BrType
from .xmlchemy import (
BaseOxmlElement, OptionalAttribute, OxmlElement, ZeroOrMore, ZeroOrOne
BaseOxmlElement, OptionalAttribute, OxmlElement, RequiredAttribute,
ZeroOrMore, ZeroOrOne
)


Expand All @@ -21,6 +22,13 @@ class CT_Br(BaseOxmlElement):
clear = OptionalAttribute('w:clear', ST_BrClear)


class CT_Jc(BaseOxmlElement):
"""
``<w:jc>`` element, specifying paragraph justification.
"""
val = RequiredAttribute('w:val', WD_ALIGN_PARAGRAPH)


class CT_P(BaseOxmlElement):
"""
``<w:p>`` element, containing the properties and text for a paragraph.
Expand All @@ -40,6 +48,17 @@ def add_p_before(self):
self.addprevious(new_p)
return new_p

@property
def alignment(self):
"""
The value of the ``<w:jc>`` grandchild element or |None| if not
present.
"""
pPr = self.pPr
if pPr is None:
return None
return pPr.alignment

def clear_content(self):
"""
Remove all child elements, except the ``<w:pPr>`` element if present.
Expand Down Expand Up @@ -96,12 +115,23 @@ class CT_PPr(BaseOxmlElement):
)
pStyle = ZeroOrOne('w:pStyle')
numPr = ZeroOrOne('w:numPr', successors=__child_sequence__[7:])
jc = ZeroOrOne('w:jc', successors=__child_sequence__[27:])
sectPr = ZeroOrOne('w:sectPr', successors=('w:pPrChange',))

def _insert_pStyle(self, pStyle):
self.insert(0, pStyle)
return pStyle

@property
def alignment(self):
"""
The value of the ``<w:jc>`` child element or |None| if not present.
"""
jc = self.jc
if jc is None:
return None
return jc.val

@property
def style(self):
"""
Expand Down
11 changes: 11 additions & 0 deletions docx/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ def add_run(self, text=None, style=None):
run.style = style
return run

@property
def alignment(self):
"""
A member of the :ref:`WdParagraphAlignment` enumeration specifying
the justification setting for this paragraph. A value of |None|
indicates the paragraph has no directly-applied alignment value and
will inherit its alignment value from its style hierarchy. Assigning
|None| to this property removes any directly-applied alignment value.
"""
return self._p.alignment

def clear(self):
"""
Return this same paragraph after removing all its content.
Expand Down
14 changes: 12 additions & 2 deletions tests/oxml/unitdata/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ def __init__(self, tag):
super(CT_EmptyBuilder, self).__init__()


class CT_JcBuilder(BaseBuilder):
__tag__ = 'w:jc'
__nspfxs__ = ('w',)
__attrs__ = ('w:val',)


class CT_PBuilder(BaseBuilder):
__tag__ = 'w:p'
__nspfxs__ = ('w',)
Expand Down Expand Up @@ -63,7 +69,7 @@ def with_space(self, value):
return self


class CT_Underline(BaseBuilder):
class CT_UnderlineBuilder(BaseBuilder):
__tag__ = 'w:u'
__nspfxs__ = ('w',)
__attrs__ = (
Expand Down Expand Up @@ -99,6 +105,10 @@ def a_dstrike():
return CT_OnOffBuilder('w:dstrike')


def a_jc():
return CT_JcBuilder()


def a_noProof():
return CT_OnOffBuilder('w:noProof')

Expand Down Expand Up @@ -156,7 +166,7 @@ def a_t():


def a_u():
return CT_Underline()
return CT_UnderlineBuilder()


def an_emboss():
Expand Down
28 changes: 23 additions & 5 deletions tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
absolute_import, division, print_function, unicode_literals
)

from docx.enum.text import WD_BREAK, WD_UNDERLINE
from docx.enum.text import WD_ALIGN_PARAGRAPH, WD_BREAK, WD_UNDERLINE
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
from docx.oxml.text import CT_P, CT_R
Expand All @@ -18,10 +18,11 @@

from .oxml.parts.unitdata.document import a_body
from .oxml.unitdata.text import (
a_b, a_bCs, a_br, a_caps, a_cr, a_cs, a_dstrike, a_p, a_pPr, a_pStyle,
a_shadow, a_smallCaps, a_snapToGrid, a_specVanish, a_strike, a_t, a_tab,
a_u, a_vanish, a_webHidden, an_emboss, an_i, an_iCs, an_imprint,
an_oMath, a_noProof, an_outline, an_r, an_rPr, an_rStyle, an_rtl
a_b, a_bCs, a_br, a_caps, a_cr, a_cs, a_dstrike, a_jc, a_p, a_pPr,
a_pStyle, a_shadow, a_smallCaps, a_snapToGrid, a_specVanish, a_strike,
a_t, a_tab, a_u, a_vanish, a_webHidden, an_emboss, an_i, an_iCs,
an_imprint, an_oMath, a_noProof, an_outline, an_r, an_rPr, an_rStyle,
an_rtl
)
from .unitutil import call, class_mock, instance_mock, Mock

Expand All @@ -41,6 +42,10 @@ def it_can_add_a_run_to_itself(self, add_run_fixture):
assert isinstance(run, Run)
assert run._r is paragraph._p.r_lst[0]

def it_knows_its_alignment_value(self, alignment_get_fixture):
paragraph, expected_value = alignment_get_fixture
assert paragraph.alignment == expected_value

def it_knows_its_paragraph_style(self):
cases = (
(Mock(name='p_elm', style='foobar'), 'foobar'),
Expand Down Expand Up @@ -102,6 +107,19 @@ def add_run_fixture(self, request, paragraph):
expected_xml = a_p().with_nsdecls().with_child(r_bldr).xml()
return paragraph, text, style, expected_xml

@pytest.fixture(params=[
('center', WD_ALIGN_PARAGRAPH.CENTER),
(None, None),
])
def alignment_get_fixture(self, request):
jc_val, expected_alignment_value = request.param
p_bldr = a_p().with_nsdecls()
if jc_val is not None:
p_bldr.with_child(a_pPr().with_child(a_jc().with_val(jc_val)))
p = p_bldr.element
paragraph = Paragraph(p)
return paragraph, expected_alignment_value

@pytest.fixture
def clear_fixture(self, request):
"""
Expand Down

0 comments on commit 1375dc9

Please sign in to comment.