forked from python-openxml/python-docx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparagraph.py
More file actions
145 lines (127 loc) · 4.7 KB
/
paragraph.py
File metadata and controls
145 lines (127 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# encoding: utf-8
"""
Paragraph-related proxy types.
"""
from __future__ import (
absolute_import, division, print_function, unicode_literals
)
from ..enum.style import WD_STYLE_TYPE
from .parfmt import ParagraphFormat
from .run import Run
from ..shared import Parented
class Paragraph(Parented):
"""
Proxy object wrapping ``<w:p>`` element.
"""
def __init__(self, p, parent):
super(Paragraph, self).__init__(parent)
self._p = self._element = p
def add_run(self, text=None, style=None):
"""
Append a run to this paragraph containing *text* and having character
style identified by style ID *style*. *text* can contain tab
(``\\t``) characters, which are converted to the appropriate XML form
for a tab. *text* can also include newline (``\\n``) or carriage
return (``\\r``) characters, each of which is converted to a line
break.
"""
r = self._p.add_r()
run = Run(r, self)
if text:
run.text = text
if style:
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
@alignment.setter
def alignment(self, value):
self._p.alignment = value
def clear(self):
"""
Return this same paragraph after removing all its content.
Paragraph-level formatting, such as style, is preserved.
"""
self._p.clear_content()
return self
def insert_paragraph_before(self, text=None, style=None):
"""
Return a newly created paragraph, inserted directly before this
paragraph. If *text* is supplied, the new paragraph contains that
text in a single run. If *style* is provided, that style is assigned
to the new paragraph.
"""
paragraph = self._insert_paragraph_before()
if text:
paragraph.add_run(text)
if style is not None:
paragraph.style = style
return paragraph
@property
def paragraph_format(self):
"""
The |ParagraphFormat| object providing access to the formatting
properties for this paragraph, such as line spacing and indentation.
"""
return ParagraphFormat(self._element)
@property
def runs(self):
"""
Sequence of |Run| instances corresponding to the <w:r> elements in
this paragraph.
"""
return [Run(r, self) for r in self._p.r_lst]
@property
def style(self):
"""
Read/Write. |_ParagraphStyle| object representing the style assigned
to this paragraph. If no explicit style is assigned to this
paragraph, its value is the default paragraph style for the document.
A paragraph style name can be assigned in lieu of a paragraph style
object. Assigning |None| removes any applied style, making its
effective value the default paragraph style for the document.
"""
style_id = self._p.style
return self.part.get_style(style_id, WD_STYLE_TYPE.PARAGRAPH)
@style.setter
def style(self, style_or_name):
style_id = self.part.get_style_id(
style_or_name, WD_STYLE_TYPE.PARAGRAPH
)
self._p.style = style_id
@property
def text(self):
"""
String formed by concatenating the text of each run in the paragraph.
Tabs and line breaks in the XML are mapped to ``\\t`` and ``\\n``
characters respectively.
Assigning text to this property causes all existing paragraph content
to be replaced with a single run containing the assigned text.
A ``\\t`` character in the text is mapped to a ``<w:tab/>`` element
and each ``\\n`` or ``\\r`` character is mapped to a line break.
Paragraph-level formatting, such as style, is preserved. All
run-level formatting, such as bold or italic, is removed.
"""
text = ''
for run in self.runs:
text += run.text
return text
@text.setter
def text(self, text):
self.clear()
self.add_run(text)
def _insert_paragraph_before(self):
"""
Return a newly created paragraph, inserted directly before this
paragraph.
"""
p = self._p.add_p_before()
return Paragraph(p, self._parent)