|
1 | | -# encoding: utf-8 |
2 | | - |
3 | | -""" |
4 | | -Text-related proxy types for python-docx, such as Paragraph and Run. |
5 | | -""" |
6 | | - |
7 | | -from __future__ import absolute_import, print_function, unicode_literals |
8 | | - |
9 | | -from .run import Run |
10 | | -from ..shared import Parented |
11 | | - |
12 | | - |
13 | | -class Paragraph(Parented): |
14 | | - """ |
15 | | - Proxy object wrapping ``<w:p>`` element. |
16 | | - """ |
17 | | - def __init__(self, p, parent): |
18 | | - super(Paragraph, self).__init__(parent) |
19 | | - self._p = p |
20 | | - |
21 | | - def add_run(self, text=None, style=None): |
22 | | - """ |
23 | | - Append a run to this paragraph containing *text* and having character |
24 | | - style identified by style ID *style*. *text* can contain tab |
25 | | - (``\\t``) characters, which are converted to the appropriate XML form |
26 | | - for a tab. *text* can also include newline (``\\n``) or carriage |
27 | | - return (``\\r``) characters, each of which is converted to a line |
28 | | - break. |
29 | | - """ |
30 | | - r = self._p.add_r() |
31 | | - run = Run(r, self) |
32 | | - if text: |
33 | | - run.text = text |
34 | | - if style: |
35 | | - run.style = style |
36 | | - return run |
37 | | - |
38 | | - @property |
39 | | - def alignment(self): |
40 | | - """ |
41 | | - A member of the :ref:`WdParagraphAlignment` enumeration specifying |
42 | | - the justification setting for this paragraph. A value of |None| |
43 | | - indicates the paragraph has no directly-applied alignment value and |
44 | | - will inherit its alignment value from its style hierarchy. Assigning |
45 | | - |None| to this property removes any directly-applied alignment value. |
46 | | - """ |
47 | | - return self._p.alignment |
48 | | - |
49 | | - @alignment.setter |
50 | | - def alignment(self, value): |
51 | | - self._p.alignment = value |
52 | | - |
53 | | - def clear(self): |
54 | | - """ |
55 | | - Return this same paragraph after removing all its content. |
56 | | - Paragraph-level formatting, such as style, is preserved. |
57 | | - """ |
58 | | - self._p.clear_content() |
59 | | - return self |
60 | | - |
61 | | - def insert_paragraph_before(self, text=None, style=None): |
62 | | - """ |
63 | | - Return a newly created paragraph, inserted directly before this |
64 | | - paragraph. If *text* is supplied, the new paragraph contains that |
65 | | - text in a single run. If *style* is provided, that style is assigned |
66 | | - to the new paragraph. |
67 | | - """ |
68 | | - p = self._p.add_p_before() |
69 | | - paragraph = Paragraph(p, self._parent) |
70 | | - if text: |
71 | | - paragraph.add_run(text) |
72 | | - if style is not None: |
73 | | - paragraph.style = style |
74 | | - return paragraph |
75 | | - |
76 | | - @property |
77 | | - def runs(self): |
78 | | - """ |
79 | | - Sequence of |Run| instances corresponding to the <w:r> elements in |
80 | | - this paragraph. |
81 | | - """ |
82 | | - return [Run(r, self) for r in self._p.r_lst] |
83 | | - |
84 | | - @property |
85 | | - def style(self): |
86 | | - """ |
87 | | - Paragraph style for this paragraph. Read/Write. |
88 | | - """ |
89 | | - style = self._p.style |
90 | | - return style if style is not None else 'Normal' |
91 | | - |
92 | | - @style.setter |
93 | | - def style(self, style): |
94 | | - self._p.style = None if style == 'Normal' else style |
95 | | - |
96 | | - @property |
97 | | - def text(self): |
98 | | - """ |
99 | | - String formed by concatenating the text of each run in the paragraph. |
100 | | - Tabs and line breaks in the XML are mapped to ``\\t`` and ``\\n`` |
101 | | - characters respectively. |
102 | | -
|
103 | | - Assigning text to this property causes all existing paragraph content |
104 | | - to be replaced with a single run containing the assigned text. |
105 | | - A ``\\t`` character in the text is mapped to a ``<w:tab/>`` element |
106 | | - and each ``\\n`` or ``\\r`` character is mapped to a line break. |
107 | | - Paragraph-level formatting, such as style, is preserved. All |
108 | | - run-level formatting, such as bold or italic, is removed. |
109 | | - """ |
110 | | - text = '' |
111 | | - for run in self.runs: |
112 | | - text += run.text |
113 | | - return text |
114 | | - |
115 | | - @text.setter |
116 | | - def text(self, text): |
117 | | - self.clear() |
118 | | - self.add_run(text) |
0 commit comments