Skip to content

Commit 2e68bb3

Browse files
authored
Merge pull request python-openxml#7 from BayooG/feature/add_comments_on_run_level
Feature/add comments on run level
2 parents 94266f2 + db56c4c commit 2e68bb3

7 files changed

Lines changed: 64 additions & 13 deletions

File tree

HISTORY.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
#Release History BayooG/bayoo-docx forked from (python-openxmm/python-docx)
55

66

7-
###0.2.7 (2020-05-01)
8-
7+
0.2.8 (2020-05-02)
98

9+
- add comments implementation on a run level
1010
- fix issue with comments date (comments dates are set to current date)
1111

1212

13-
###0.2.4 (2019-9-4)
13+
0.2.4 (2019-9-4)
1414

1515
- loop over all the document chieldern (Paragraphs, Tables, Sections) with the right order `document.elements`
1616
- addons to Paragraph Object (delete, heading_level, merge_paragraph )

README.rst

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,42 @@
1-
#Bayoo-docx
1+
Bayoo-docx
2+
==========
23

34
Python library forked from `python-docx <github.com/python-openxml/python-docx/>`_.
45

56
The main purpose of the fork was to add implementation for comments and footnotes to the library
67

7-
##Installation
8-
8+
Installation
9+
------------
910

1011
Use the package manager `pip <pypi.org/project/bayoo-docx/>`_ to install bayoo-docx.
1112

1213

1314
`pip install bayoo-docx`
1415

15-
##Usage
16+
Usage
17+
-----
1618

1719
::
1820
1921
import docx
2022
2123
document = docx.Document()
2224

23-
paragraph = document.add_paragraph('text') # create new paragraph
25+
paragraph1 = document.add_paragraph('text') # create new paragraph
26+
27+
comment = paragraph.add_comment('comment',author='Obay Daba',initials= 'od') # add a comment on the entire paragraph
28+
29+
paragraph2 = document.add_paragraph('text') # create another paragraph
30+
31+
run = paragraph2.add_run('texty') add a run to the paragraph
2432

25-
comment = paragraph.add_comment('comment',author='Obay Daba',initials= 'od') # create a comment'
33+
run.add_comment('comment') # add a comment only for the run text
2634

2735
paragraph.add_footnote('footnote text') # add a footnote
2836

2937

3038

31-
##License
39+
License
40+
-------
3241

3342
`MIT <https://choosealicense.com/licenses/mit/>`_

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.7'
5+
__version__ = '0.2.8'
66

77

88
# register custom Part classes with opc package reader

docx/oxml/text/run.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@ def add_drawing(self, inline_or_anchor):
5858
drawing.append(inline_or_anchor)
5959
return drawing
6060

61+
def add_comm(self, author, comment_part, initials, dtime, comment_text):
62+
63+
comment = comment_part.add_comment(author, initials, dtime)
64+
comment._add_p(comment_text)
65+
# _r = self.add_r()
66+
self.add_comment_reference(comment._id)
67+
self.link_comment(comment._id)
68+
69+
return comment
70+
71+
def link_comment(self, _id):
72+
rStart = OxmlElement('w:commentRangeStart')
73+
rStart._id = _id
74+
rEnd = OxmlElement('w:commentRangeEnd')
75+
rEnd._id = _id
76+
self.insert(0,rStart)
77+
self.append(rEnd)
78+
6179
def add_comment_reference(self, _id):
6280
reference = OxmlElement('w:commentReference')
6381
reference._id = _id

docx/text/run.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66

77
from __future__ import absolute_import, print_function, unicode_literals
8+
from datetime import datetime
89

910
from docx.oxml.ns import qn
1011

@@ -82,6 +83,13 @@ def add_text(self, text):
8283
t = self._r.add_t(text)
8384
return _Text(t)
8485

86+
def add_comment(self, text, author='python-docx', initials='pd', dtime=None):
87+
comment_part = self.part._comments_part.element
88+
if dtime is None:
89+
dtime = str( datetime.now() ).replace(' ', 'T')
90+
comment = self._r.add_comm(author, comment_part, initials, dtime, text)
91+
92+
return comment
8593
@property
8694
def bold(self):
8795
"""

setup.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from setuptools import find_packages, setup
77

88

9-
def text_of(relpath):
9+
def text_of(relpath, long_description=False):
1010
"""
1111
Return string containing the contents of the file at *relpath* relative to
1212
this file.
@@ -15,6 +15,9 @@ def text_of(relpath):
1515
file_path = os.path.join(thisdir, os.path.normpath(relpath))
1616
with open(file_path) as f:
1717
text = f.read()
18+
if long_description:
19+
text = re.sub(text, '=+\n', '\n')
20+
text = re.sub(text, '-+\n', '\n')
1821
return text
1922

2023

@@ -58,7 +61,7 @@ def text_of(relpath):
5861
'Topic :: Software Development :: Libraries'
5962
]
6063

61-
LONG_DESCRIPTION = text_of('README.rst') + '\n\n' + text_of('HISTORY.rst')
64+
LONG_DESCRIPTION = text_of('README.rst',long_description=True) + '\n\n' + text_of('HISTORY.rst')
6265

6366

6467
params = {

test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import docx
2+
3+
print(docx.__version__)
4+
5+
d= docx.Document()
6+
7+
p = d.add_paragraph('r1')
8+
9+
r = p.add_run('r2')
10+
11+
r.add_comment('hola comm')
12+
13+
d.save('d.docx')

0 commit comments

Comments
 (0)