Skip to content

Commit 705391d

Browse files
author
Steve Canny
committed
reorg: extract docx.text.parfmt
1 parent e9ad897 commit 705391d

File tree

8 files changed

+698
-668
lines changed

8 files changed

+698
-668
lines changed

docx/styles/style.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from . import BabelFish
1212
from ..enum.style import WD_STYLE_TYPE
1313
from ..shared import ElementProxy
14-
from ..text.paragraph import ParagraphFormat
14+
from ..text.parfmt import ParagraphFormat
1515
from ..text.run import Font
1616

1717

docx/text/paragraph.py

Lines changed: 2 additions & 283 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
)
1010

1111
from ..enum.style import WD_STYLE_TYPE
12-
from ..enum.text import WD_LINE_SPACING
12+
from .parfmt import ParagraphFormat
1313
from .run import Run
14-
from ..shared import ElementProxy, Emu, Length, Parented, Pt, Twips
14+
from ..shared import Parented
1515

1616

1717
class Paragraph(Parented):
@@ -143,284 +143,3 @@ def _insert_paragraph_before(self):
143143
"""
144144
p = self._p.add_p_before()
145145
return Paragraph(p, self._parent)
146-
147-
148-
class ParagraphFormat(ElementProxy):
149-
"""
150-
Provides access to paragraph formatting such as justification,
151-
indentation, line spacing, space before and after, and widow/orphan
152-
control.
153-
"""
154-
155-
__slots__ = ()
156-
157-
@property
158-
def alignment(self):
159-
"""
160-
A member of the :ref:`WdParagraphAlignment` enumeration specifying
161-
the justification setting for this paragraph. A value of |None|
162-
indicates paragraph alignment is inherited from the style hierarchy.
163-
"""
164-
pPr = self._element.pPr
165-
if pPr is None:
166-
return None
167-
return pPr.jc_val
168-
169-
@alignment.setter
170-
def alignment(self, value):
171-
pPr = self._element.get_or_add_pPr()
172-
pPr.jc_val = value
173-
174-
@property
175-
def first_line_indent(self):
176-
"""
177-
|Length| value specifying the relative difference in indentation for
178-
the first line of the paragraph. A positive value causes the first
179-
line to be indented. A negative value produces a hanging indent.
180-
|None| indicates first line indentation is inherited from the style
181-
hierarchy.
182-
"""
183-
pPr = self._element.pPr
184-
if pPr is None:
185-
return None
186-
return pPr.first_line_indent
187-
188-
@first_line_indent.setter
189-
def first_line_indent(self, value):
190-
pPr = self._element.get_or_add_pPr()
191-
pPr.first_line_indent = value
192-
193-
@property
194-
def keep_together(self):
195-
"""
196-
|True| if the paragraph should be kept "in one piece" and not broken
197-
across a page boundary when the document is rendered. |None|
198-
indicates its effective value is inherited from the style hierarchy.
199-
"""
200-
pPr = self._element.pPr
201-
if pPr is None:
202-
return None
203-
return pPr.keepLines_val
204-
205-
@keep_together.setter
206-
def keep_together(self, value):
207-
self._element.get_or_add_pPr().keepLines_val = value
208-
209-
@property
210-
def keep_with_next(self):
211-
"""
212-
|True| if the paragraph should be kept on the same page as the
213-
subsequent paragraph when the document is rendered. For example, this
214-
property could be used to keep a section heading on the same page as
215-
its first paragraph. |None| indicates its effective value is
216-
inherited from the style hierarchy.
217-
"""
218-
pPr = self._element.pPr
219-
if pPr is None:
220-
return None
221-
return pPr.keepNext_val
222-
223-
@keep_with_next.setter
224-
def keep_with_next(self, value):
225-
self._element.get_or_add_pPr().keepNext_val = value
226-
227-
@property
228-
def left_indent(self):
229-
"""
230-
|Length| value specifying the space between the left margin and the
231-
left side of the paragraph. |None| indicates the left indent value is
232-
inherited from the style hierarchy. Use an |Inches| value object as
233-
a convenient way to apply indentation in units of inches.
234-
"""
235-
pPr = self._element.pPr
236-
if pPr is None:
237-
return None
238-
return pPr.ind_left
239-
240-
@left_indent.setter
241-
def left_indent(self, value):
242-
pPr = self._element.get_or_add_pPr()
243-
pPr.ind_left = value
244-
245-
@property
246-
def line_spacing(self):
247-
"""
248-
|float| or |Length| value specifying the space between baselines in
249-
successive lines of the paragraph. A value of |None| indicates line
250-
spacing is inherited from the style hierarchy. A float value, e.g.
251-
``2.0`` or ``1.75``, indicates spacing is applied in multiples of
252-
line heights. A |Length| value such as ``Pt(12)`` indicates spacing
253-
is a fixed height. The |Pt| value class is a convenient way to apply
254-
line spacing in units of points. Assigning |None| resets line spacing
255-
to inherit from the style hierarchy.
256-
"""
257-
pPr = self._element.pPr
258-
if pPr is None:
259-
return None
260-
return self._line_spacing(pPr.spacing_line, pPr.spacing_lineRule)
261-
262-
@line_spacing.setter
263-
def line_spacing(self, value):
264-
pPr = self._element.get_or_add_pPr()
265-
if value is None:
266-
pPr.spacing_line = None
267-
pPr.spacing_lineRule = None
268-
elif isinstance(value, Length):
269-
pPr.spacing_line = value
270-
if pPr.spacing_lineRule != WD_LINE_SPACING.AT_LEAST:
271-
pPr.spacing_lineRule = WD_LINE_SPACING.EXACTLY
272-
else:
273-
pPr.spacing_line = Emu(value * Twips(240))
274-
pPr.spacing_lineRule = WD_LINE_SPACING.MULTIPLE
275-
276-
@property
277-
def line_spacing_rule(self):
278-
"""
279-
A member of the :ref:`WdLineSpacing` enumeration indicating how the
280-
value of :attr:`line_spacing` should be interpreted. Assigning any of
281-
the :ref:`WdLineSpacing` members :attr:`SINGLE`, :attr:`DOUBLE`, or
282-
:attr:`ONE_POINT_FIVE` will cause the value of :attr:`line_spacing`
283-
to be updated to produce the corresponding line spacing.
284-
"""
285-
pPr = self._element.pPr
286-
if pPr is None:
287-
return None
288-
return self._line_spacing_rule(
289-
pPr.spacing_line, pPr.spacing_lineRule
290-
)
291-
292-
@line_spacing_rule.setter
293-
def line_spacing_rule(self, value):
294-
pPr = self._element.get_or_add_pPr()
295-
if value == WD_LINE_SPACING.SINGLE:
296-
pPr.spacing_line = Twips(240)
297-
pPr.spacing_lineRule = WD_LINE_SPACING.MULTIPLE
298-
elif value == WD_LINE_SPACING.ONE_POINT_FIVE:
299-
pPr.spacing_line = Twips(360)
300-
pPr.spacing_lineRule = WD_LINE_SPACING.MULTIPLE
301-
elif value == WD_LINE_SPACING.DOUBLE:
302-
pPr.spacing_line = Twips(480)
303-
pPr.spacing_lineRule = WD_LINE_SPACING.MULTIPLE
304-
else:
305-
pPr.spacing_lineRule = value
306-
307-
@property
308-
def page_break_before(self):
309-
"""
310-
|True| if the paragraph should appear at the top of the page
311-
following the prior paragraph. |None| indicates its effective value
312-
is inherited from the style hierarchy.
313-
"""
314-
pPr = self._element.pPr
315-
if pPr is None:
316-
return None
317-
return pPr.pageBreakBefore_val
318-
319-
@page_break_before.setter
320-
def page_break_before(self, value):
321-
self._element.get_or_add_pPr().pageBreakBefore_val = value
322-
323-
@property
324-
def right_indent(self):
325-
"""
326-
|Length| value specifying the space between the right margin and the
327-
right side of the paragraph. |None| indicates the right indent value
328-
is inherited from the style hierarchy. Use a |Cm| value object as
329-
a convenient way to apply indentation in units of centimeters.
330-
"""
331-
pPr = self._element.pPr
332-
if pPr is None:
333-
return None
334-
return pPr.ind_right
335-
336-
@right_indent.setter
337-
def right_indent(self, value):
338-
pPr = self._element.get_or_add_pPr()
339-
pPr.ind_right = value
340-
341-
@property
342-
def space_after(self):
343-
"""
344-
|Length| value specifying the spacing to appear between this
345-
paragraph and the subsequent paragraph. |None| indicates this value
346-
is inherited from the style hierarchy. |Length| objects provide
347-
convenience properties, such as :attr:`~.Length.pt` and
348-
:attr:`~.Length.inches`, that allow easy conversion to various length
349-
units.
350-
"""
351-
pPr = self._element.pPr
352-
if pPr is None:
353-
return None
354-
return pPr.spacing_after
355-
356-
@space_after.setter
357-
def space_after(self, value):
358-
self._element.get_or_add_pPr().spacing_after = value
359-
360-
@property
361-
def space_before(self):
362-
"""
363-
|Length| value specifying the spacing to appear between this
364-
paragraph and the prior paragraph. |None| indicates this value is
365-
inherited from the style hierarchy. |Length| objects provide
366-
convenience properties, such as :attr:`~.Length.pt` and
367-
:attr:`~.Length.cm`, that allow easy conversion to various length
368-
units.
369-
"""
370-
pPr = self._element.pPr
371-
if pPr is None:
372-
return None
373-
return pPr.spacing_before
374-
375-
@space_before.setter
376-
def space_before(self, value):
377-
self._element.get_or_add_pPr().spacing_before = value
378-
379-
@property
380-
def widow_control(self):
381-
"""
382-
|True| if the first and last lines in the paragraph remain on the
383-
same page as the rest of the paragraph when Word repaginates the
384-
document. |None| indicates its effective value is inherited from the
385-
style hierarchy.
386-
"""
387-
pPr = self._element.pPr
388-
if pPr is None:
389-
return None
390-
return pPr.widowControl_val
391-
392-
@widow_control.setter
393-
def widow_control(self, value):
394-
self._element.get_or_add_pPr().widowControl_val = value
395-
396-
@staticmethod
397-
def _line_spacing(spacing_line, spacing_lineRule):
398-
"""
399-
Return the line spacing value calculated from the combination of
400-
*spacing_line* and *spacing_lineRule*. Returns a |float| number of
401-
lines when *spacing_lineRule* is ``WD_LINE_SPACING.MULTIPLE``,
402-
otherwise a |Length| object of absolute line height is returned.
403-
Returns |None| when *spacing_line* is |None|.
404-
"""
405-
if spacing_line is None:
406-
return None
407-
if spacing_lineRule == WD_LINE_SPACING.MULTIPLE:
408-
return spacing_line / Pt(12)
409-
return spacing_line
410-
411-
@staticmethod
412-
def _line_spacing_rule(line, lineRule):
413-
"""
414-
Return the line spacing rule value calculated from the combination of
415-
*line* and *lineRule*. Returns special members of the
416-
:ref:`WdLineSpacing` enumeration when line spacing is single, double,
417-
or 1.5 lines.
418-
"""
419-
if lineRule == WD_LINE_SPACING.MULTIPLE:
420-
if line == Twips(240):
421-
return WD_LINE_SPACING.SINGLE
422-
if line == Twips(360):
423-
return WD_LINE_SPACING.ONE_POINT_FIVE
424-
if line == Twips(480):
425-
return WD_LINE_SPACING.DOUBLE
426-
return lineRule

0 commit comments

Comments
 (0)