Skip to content

Commit 1b7ddf0

Browse files
committed
Merge tag 'v0.2.12' into feature/paragraph_all_runs
2 parents 1e8add7 + 0d4ba9a commit 1b7ddf0

6 files changed

Lines changed: 52 additions & 6 deletions

File tree

README.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,19 @@ Usage
3232

3333
run.add_comment('comment') # add a comment only for the run text
3434

35+
run.add_comment('comment2')
36+
37+
run_comments = run.comments
38+
3539
paragraph.add_footnote('footnote text') # add a footnote
3640

3741

42+
Donation
43+
------------
44+
::
45+
46+
bitcoin: bc1q9dftn4ndufwzyzkm8ryu0kky0v8y7w00zdah9r
47+
3848

3949
License
4050
-------

docx/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from docx.api import Document # noqa
44

5-
__version__ = '0.2.10'
5+
__version__ = '0.2.12'
66

77

88
# register custom Part classes with opc package reader

docx/oxml/comments.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ def paragraph(self):
5252
return Paragraph(self.p, self)
5353

5454

55-
56-
5755
class CT_Comments(BaseOxmlElement):
5856
"""
5957
A ``<w:comments>`` element, a container for Comments properties

docx/text/comment.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from ..shared import Parented
2+
3+
class Comment(Parented):
4+
"""[summary]
5+
6+
:param Parented: [description]
7+
:type Parented: [type]
8+
"""
9+
def __init__(self, com, parent):
10+
super(Comment, self).__init__(parent)
11+
self._com = self._element = self.element = com
12+
13+
@property
14+
def paragraph(self):
15+
return self.element.paragraph
16+
17+
@property
18+
def text(self):
19+
return self.element.paragraph.text
20+
21+
@text.setter
22+
def text(self, text):
23+
self.element.paragraph.text = text

docx/text/paragraph.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ def delete(self):
4848
self._p.getparent().remove(self._p)
4949
self._p = self._element = None
5050

51-
def add_comment(self, text, author='python-docx', initials='pd', dtime=None ,rangeStart=0, rangeEnd=0):
52-
comment_part = self.part._comments_part.element
51+
def add_comment(self, text, author='python-docx', initials='pd', dtime=None ,rangeStart=0, rangeEnd=0, comment_part=None):
52+
if comment_part is None:
53+
comment_part = self.part._comments_part.element
5354
if dtime is None:
5455
dtime = str( datetime.now() ).replace(' ', 'T')
5556
comment = self._p.add_comm(author, comment_part, initials, dtime, text, rangeStart, rangeEnd)
@@ -224,6 +225,11 @@ def footnotes(self):
224225
else :
225226
return False
226227

228+
@property
229+
def comments(self):
230+
runs_comments = [run.comments for run in self.runs]
231+
return [comment for comments in runs_comments for comment in comments]
232+
227233
@text.setter
228234
def text(self, text):
229235
self.clear()

docx/text/run.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from ..shape import InlineShape
1616
from ..shared import Parented
1717

18+
from .comment import Comment
1819

1920
class Run(Parented):
2021
"""
@@ -230,7 +231,15 @@ def get_hyperLink(self):
230231
return '', False
231232
else:
232233
return 'None'
233-
234+
235+
@property
236+
def comments(self):
237+
comment_part = self._parent._parent.part._comments_part.element
238+
comment_refs = self._element.findall(qn('w:commentReference'))
239+
ids = [int(ref.get(qn('w:id'))) for ref in comment_refs]
240+
coms = [com for com in comment_part if com._id in ids]
241+
return [Comment(com, comment_part) for com in coms]
242+
234243

235244
class _Text(object):
236245
"""

0 commit comments

Comments
 (0)