forked from python-openxml/python-docx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext.py
More file actions
77 lines (64 loc) · 1.64 KB
/
text.py
File metadata and controls
77 lines (64 loc) · 1.64 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
# encoding: utf-8
"""
Text-related proxy types for python-docx, such as Paragraph and Run.
"""
class Paragraph(object):
"""
Proxy object wrapping ``<w:p>`` element.
"""
def __init__(self, p_elm):
super(Paragraph, self).__init__()
self._p = p_elm
def add_run(self):
"""
Append a run to this paragraph.
"""
r = self._p.add_r()
return Run(r)
@property
def runs(self):
"""
Sequence of |Run| instances corresponding to the <w:r> elements in
this paragraph.
"""
return [Run(r) for r in self._p.r_lst]
@property
def style(self):
"""
Paragraph style for this paragraph. Read/Write.
"""
style = self._p.style
return style if style is not None else 'Normal'
@style.setter
def style(self, style):
self._p.style = None if style == 'Normal' else style
class Run(object):
"""
Proxy object wrapping ``<w:r>`` element.
"""
def __init__(self, r_elm):
super(Run, self).__init__()
self._r = r_elm
def add_text(self, text):
"""
Add a text element to this run.
"""
t = self._r.add_t(text)
return Text(t)
@property
def text(self):
"""
A string formed by concatenating all the <w:t> elements present in
this run.
"""
text = ''
for t in self._r.t_lst:
text += t.text
return text
class Text(object):
"""
Proxy object wrapping ``<w:t>`` element.
"""
def __init__(self, t_elm):
super(Text, self).__init__()
self._t = t_elm