word-comment 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- word_comment-0.1.0/LICENSE +21 -0
- word_comment-0.1.0/PKG-INFO +102 -0
- word_comment-0.1.0/README.md +76 -0
- word_comment-0.1.0/pyproject.toml +45 -0
- word_comment-0.1.0/setup.cfg +4 -0
- word_comment-0.1.0/src/word_comment/__init__.py +3 -0
- word_comment-0.1.0/src/word_comment/__main__.py +5 -0
- word_comment-0.1.0/src/word_comment/cli.py +50 -0
- word_comment-0.1.0/src/word_comment/core.py +309 -0
- word_comment-0.1.0/src/word_comment.egg-info/PKG-INFO +102 -0
- word_comment-0.1.0/src/word_comment.egg-info/SOURCES.txt +13 -0
- word_comment-0.1.0/src/word_comment.egg-info/dependency_links.txt +1 -0
- word_comment-0.1.0/src/word_comment.egg-info/entry_points.txt +2 -0
- word_comment-0.1.0/src/word_comment.egg-info/requires.txt +1 -0
- word_comment-0.1.0/src/word_comment.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: word-comment
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Add Word comments to matching text in .docx files.
|
|
5
|
+
Author: AI-Part-A
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/AI-Part-A/word-comment
|
|
8
|
+
Project-URL: Repository, https://github.com/AI-Part-A/word-comment
|
|
9
|
+
Project-URL: Issues, https://github.com/AI-Part-A/word-comment/issues
|
|
10
|
+
Keywords: docx,word,comments,office,xml
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
18
|
+
Classifier: Operating System :: OS Independent
|
|
19
|
+
Classifier: Topic :: Office/Business
|
|
20
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
21
|
+
Requires-Python: >=3.10
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Requires-Dist: lxml>=5.0.0
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# word-comment
|
|
28
|
+
|
|
29
|
+
`word-comment` adds a Microsoft Word comment to matching text in a `.docx` file.
|
|
30
|
+
|
|
31
|
+
It targets a specific paragraph by `w14:paraId`, finds the first matching occurrence of `target_text` inside that paragraph, and inserts a standard Word comment range plus comment metadata.
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
python -m pip install word-comment
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Python API
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from word_comment import add_comment
|
|
43
|
+
|
|
44
|
+
result = add_comment(
|
|
45
|
+
input_path="input.docx",
|
|
46
|
+
output_path="output.docx",
|
|
47
|
+
para_id="7BD69366",
|
|
48
|
+
target_text="keyword",
|
|
49
|
+
comment_text="comment body",
|
|
50
|
+
author="Auto_Bot",
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
print(result.success)
|
|
54
|
+
print(result.inserted_count)
|
|
55
|
+
print(result.reason)
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## CLI
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
word-comment input.docx output.docx 7BD69366 "keyword" "comment body" --author Auto_Bot
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Behavior
|
|
65
|
+
|
|
66
|
+
- Searches for the paragraph whose `w14:paraId` matches `para_id`
|
|
67
|
+
- Finds the first occurrence of `target_text` in that paragraph
|
|
68
|
+
- Inserts a Word comment range and a `commentReference`
|
|
69
|
+
- Creates `word/comments.xml` when it does not already exist
|
|
70
|
+
- Adds the required relationship and content-type entries when needed
|
|
71
|
+
- Copies the input file unchanged when the paragraph or target text is not found
|
|
72
|
+
|
|
73
|
+
## Return Value
|
|
74
|
+
|
|
75
|
+
`add_comment(...)` returns `AddCommentResult` with:
|
|
76
|
+
|
|
77
|
+
- `success: bool`
|
|
78
|
+
- `inserted_count: int`
|
|
79
|
+
- `para_id: str`
|
|
80
|
+
- `output_path: Path`
|
|
81
|
+
- `reason: str | None`
|
|
82
|
+
|
|
83
|
+
Possible `reason` values:
|
|
84
|
+
|
|
85
|
+
- `paragraph_not_found`
|
|
86
|
+
- `target_text_not_found`
|
|
87
|
+
|
|
88
|
+
## Limitations
|
|
89
|
+
|
|
90
|
+
- The target paragraph must already have a `w14:paraId`
|
|
91
|
+
- `target_text` must exist entirely within a single `w:t` node
|
|
92
|
+
- Only the first match in the selected paragraph is annotated
|
|
93
|
+
- The implementation modifies `word/document.xml` and `word/comments.xml`
|
|
94
|
+
|
|
95
|
+
## Development Release Check
|
|
96
|
+
|
|
97
|
+
Build artifacts locally:
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
python -m build
|
|
101
|
+
python -m twine check dist/*
|
|
102
|
+
```
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# word-comment
|
|
2
|
+
|
|
3
|
+
`word-comment` adds a Microsoft Word comment to matching text in a `.docx` file.
|
|
4
|
+
|
|
5
|
+
It targets a specific paragraph by `w14:paraId`, finds the first matching occurrence of `target_text` inside that paragraph, and inserts a standard Word comment range plus comment metadata.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
python -m pip install word-comment
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Python API
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
from word_comment import add_comment
|
|
17
|
+
|
|
18
|
+
result = add_comment(
|
|
19
|
+
input_path="input.docx",
|
|
20
|
+
output_path="output.docx",
|
|
21
|
+
para_id="7BD69366",
|
|
22
|
+
target_text="keyword",
|
|
23
|
+
comment_text="comment body",
|
|
24
|
+
author="Auto_Bot",
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
print(result.success)
|
|
28
|
+
print(result.inserted_count)
|
|
29
|
+
print(result.reason)
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## CLI
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
word-comment input.docx output.docx 7BD69366 "keyword" "comment body" --author Auto_Bot
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Behavior
|
|
39
|
+
|
|
40
|
+
- Searches for the paragraph whose `w14:paraId` matches `para_id`
|
|
41
|
+
- Finds the first occurrence of `target_text` in that paragraph
|
|
42
|
+
- Inserts a Word comment range and a `commentReference`
|
|
43
|
+
- Creates `word/comments.xml` when it does not already exist
|
|
44
|
+
- Adds the required relationship and content-type entries when needed
|
|
45
|
+
- Copies the input file unchanged when the paragraph or target text is not found
|
|
46
|
+
|
|
47
|
+
## Return Value
|
|
48
|
+
|
|
49
|
+
`add_comment(...)` returns `AddCommentResult` with:
|
|
50
|
+
|
|
51
|
+
- `success: bool`
|
|
52
|
+
- `inserted_count: int`
|
|
53
|
+
- `para_id: str`
|
|
54
|
+
- `output_path: Path`
|
|
55
|
+
- `reason: str | None`
|
|
56
|
+
|
|
57
|
+
Possible `reason` values:
|
|
58
|
+
|
|
59
|
+
- `paragraph_not_found`
|
|
60
|
+
- `target_text_not_found`
|
|
61
|
+
|
|
62
|
+
## Limitations
|
|
63
|
+
|
|
64
|
+
- The target paragraph must already have a `w14:paraId`
|
|
65
|
+
- `target_text` must exist entirely within a single `w:t` node
|
|
66
|
+
- Only the first match in the selected paragraph is annotated
|
|
67
|
+
- The implementation modifies `word/document.xml` and `word/comments.xml`
|
|
68
|
+
|
|
69
|
+
## Development Release Check
|
|
70
|
+
|
|
71
|
+
Build artifacts locally:
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
python -m build
|
|
75
|
+
python -m twine check dist/*
|
|
76
|
+
```
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "word-comment"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Add Word comments to matching text in .docx files."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [
|
|
14
|
+
{ name = "AI-Part-A" }
|
|
15
|
+
]
|
|
16
|
+
dependencies = [
|
|
17
|
+
"lxml>=5.0.0"
|
|
18
|
+
]
|
|
19
|
+
keywords = ["docx", "word", "comments", "office", "xml"]
|
|
20
|
+
classifiers = [
|
|
21
|
+
"Development Status :: 3 - Alpha",
|
|
22
|
+
"Intended Audience :: Developers",
|
|
23
|
+
"Programming Language :: Python :: 3",
|
|
24
|
+
"Programming Language :: Python :: 3.10",
|
|
25
|
+
"Programming Language :: Python :: 3.11",
|
|
26
|
+
"Programming Language :: Python :: 3.12",
|
|
27
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
28
|
+
"Operating System :: OS Independent",
|
|
29
|
+
"Topic :: Office/Business",
|
|
30
|
+
"Topic :: Software Development :: Libraries :: Python Modules"
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[project.scripts]
|
|
34
|
+
word-comment = "word_comment.cli:main"
|
|
35
|
+
|
|
36
|
+
[project.urls]
|
|
37
|
+
Homepage = "https://github.com/AI-Part-A/word-comment"
|
|
38
|
+
Repository = "https://github.com/AI-Part-A/word-comment"
|
|
39
|
+
Issues = "https://github.com/AI-Part-A/word-comment/issues"
|
|
40
|
+
|
|
41
|
+
[tool.setuptools]
|
|
42
|
+
package-dir = {"" = "src"}
|
|
43
|
+
|
|
44
|
+
[tool.setuptools.packages.find]
|
|
45
|
+
where = ["src"]
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
from .core import add_comment
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
10
|
+
parser = argparse.ArgumentParser(
|
|
11
|
+
prog="word-comment",
|
|
12
|
+
description="Add a Word comment to matching text inside a specific paragraph ID.",
|
|
13
|
+
)
|
|
14
|
+
parser.add_argument("input_path", help="Source .docx file")
|
|
15
|
+
parser.add_argument("output_path", help="Destination .docx file")
|
|
16
|
+
parser.add_argument("para_id", help="Target paragraph ID (w14:paraId)")
|
|
17
|
+
parser.add_argument("target_text", help="Text to search for")
|
|
18
|
+
parser.add_argument("comment_text", help="Comment body to attach")
|
|
19
|
+
parser.add_argument(
|
|
20
|
+
"--author",
|
|
21
|
+
default="Auto_Bot",
|
|
22
|
+
help="Comment author name",
|
|
23
|
+
)
|
|
24
|
+
return parser
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def main() -> int:
|
|
28
|
+
parser = build_parser()
|
|
29
|
+
args = parser.parse_args()
|
|
30
|
+
|
|
31
|
+
result = add_comment(
|
|
32
|
+
input_path=Path(args.input_path),
|
|
33
|
+
output_path=Path(args.output_path),
|
|
34
|
+
para_id=args.para_id,
|
|
35
|
+
target_text=args.target_text,
|
|
36
|
+
comment_text=args.comment_text,
|
|
37
|
+
author=args.author,
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
print(f"output: {result.output_path}")
|
|
41
|
+
print(f"success: {result.success}")
|
|
42
|
+
print(f"inserted_comments: {result.inserted_count}")
|
|
43
|
+
print(f"para_id: {result.para_id}")
|
|
44
|
+
if result.reason:
|
|
45
|
+
print(f"reason: {result.reason}")
|
|
46
|
+
return 0
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
if __name__ == "__main__":
|
|
50
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import copy
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from datetime import datetime, timezone
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from shutil import copy2
|
|
8
|
+
from tempfile import TemporaryDirectory
|
|
9
|
+
from zipfile import ZIP_DEFLATED, ZipFile
|
|
10
|
+
|
|
11
|
+
from lxml import etree
|
|
12
|
+
|
|
13
|
+
NS_W = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
|
|
14
|
+
NS_R = "http://schemas.openxmlformats.org/package/2006/relationships"
|
|
15
|
+
NS_W14 = "http://schemas.microsoft.com/office/word/2010/wordml"
|
|
16
|
+
NS_CT = "http://schemas.openxmlformats.org/package/2006/content-types"
|
|
17
|
+
XML_SPACE = "http://www.w3.org/XML/1998/namespace"
|
|
18
|
+
|
|
19
|
+
NS_MAP = {
|
|
20
|
+
"w": NS_W,
|
|
21
|
+
"r": NS_R,
|
|
22
|
+
"w14": NS_W14,
|
|
23
|
+
"ct": NS_CT,
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@dataclass(frozen=True)
|
|
28
|
+
class AddCommentResult:
|
|
29
|
+
success: bool
|
|
30
|
+
inserted_count: int
|
|
31
|
+
para_id: str
|
|
32
|
+
output_path: Path
|
|
33
|
+
reason: str | None = None
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _extract_docx(docx_path: Path, work_dir: Path) -> None:
|
|
37
|
+
with ZipFile(docx_path, "r") as archive:
|
|
38
|
+
archive.extractall(work_dir)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _prepare_comments_xml(work_dir: Path) -> tuple[etree._ElementTree, etree._Element]:
|
|
42
|
+
comments_path = work_dir / "word" / "comments.xml"
|
|
43
|
+
if comments_path.exists():
|
|
44
|
+
tree = etree.parse(str(comments_path))
|
|
45
|
+
return tree, tree.getroot()
|
|
46
|
+
|
|
47
|
+
root = etree.Element(f"{{{NS_W}}}comments", nsmap=NS_MAP)
|
|
48
|
+
return etree.ElementTree(root), root
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _next_comment_id(root_comments: etree._Element) -> int:
|
|
52
|
+
existing_ids = [
|
|
53
|
+
int(node.get(f"{{{NS_W}}}id"))
|
|
54
|
+
for node in root_comments.findall(f".//{{{NS_W}}}comment")
|
|
55
|
+
if node.get(f"{{{NS_W}}}id") is not None
|
|
56
|
+
]
|
|
57
|
+
return max(existing_ids) + 1 if existing_ids else 0
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _append_comment(root_comments: etree._Element, comment_id: int, author: str, text: str) -> int:
|
|
61
|
+
created_at = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
|
|
62
|
+
|
|
63
|
+
comment = etree.Element(
|
|
64
|
+
f"{{{NS_W}}}comment",
|
|
65
|
+
{
|
|
66
|
+
f"{{{NS_W}}}id": str(comment_id),
|
|
67
|
+
f"{{{NS_W}}}author": author,
|
|
68
|
+
f"{{{NS_W}}}date": created_at,
|
|
69
|
+
},
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
paragraph = etree.SubElement(comment, f"{{{NS_W}}}p")
|
|
73
|
+
paragraph_props = etree.SubElement(paragraph, f"{{{NS_W}}}pPr")
|
|
74
|
+
etree.SubElement(paragraph_props, f"{{{NS_W}}}pStyle", {f"{{{NS_W}}}val": "CommentText"})
|
|
75
|
+
|
|
76
|
+
run = etree.SubElement(paragraph, f"{{{NS_W}}}r")
|
|
77
|
+
run_props = etree.SubElement(run, f"{{{NS_W}}}rPr")
|
|
78
|
+
etree.SubElement(run_props, f"{{{NS_W}}}rStyle", {f"{{{NS_W}}}val": "CommentReference"})
|
|
79
|
+
text_node = etree.SubElement(run, f"{{{NS_W}}}t")
|
|
80
|
+
text_node.text = text
|
|
81
|
+
|
|
82
|
+
root_comments.append(comment)
|
|
83
|
+
return comment_id
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _detect_target_para_ids(root_doc: etree._Element, target_text: str) -> list[str]:
|
|
87
|
+
para_ids: list[str] = []
|
|
88
|
+
for paragraph in root_doc.findall(".//w:p", namespaces=NS_MAP):
|
|
89
|
+
paragraph_text = "".join(
|
|
90
|
+
text_node.text or ""
|
|
91
|
+
for text_node in paragraph.findall(".//w:t", namespaces=NS_MAP)
|
|
92
|
+
)
|
|
93
|
+
if target_text in paragraph_text:
|
|
94
|
+
para_id = paragraph.get(f"{{{NS_W14}}}paraId")
|
|
95
|
+
if para_id:
|
|
96
|
+
para_ids.append(para_id)
|
|
97
|
+
return para_ids
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _make_text_run(text: str, source_run_props: etree._Element | None) -> etree._Element:
|
|
101
|
+
run = etree.Element(f"{{{NS_W}}}r")
|
|
102
|
+
if source_run_props is not None:
|
|
103
|
+
run.append(copy.deepcopy(source_run_props))
|
|
104
|
+
text_node = etree.SubElement(run, f"{{{NS_W}}}t")
|
|
105
|
+
if text.strip() != text:
|
|
106
|
+
text_node.set(f"{{{XML_SPACE}}}space", "preserve")
|
|
107
|
+
text_node.text = text
|
|
108
|
+
return run
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _split_run_and_insert_comment(
|
|
112
|
+
paragraph_elem: etree._Element,
|
|
113
|
+
run_element: etree._Element,
|
|
114
|
+
text_elem: etree._Element,
|
|
115
|
+
target_text: str,
|
|
116
|
+
comment_id: int,
|
|
117
|
+
) -> bool:
|
|
118
|
+
full_text = text_elem.text
|
|
119
|
+
if not full_text or target_text not in full_text:
|
|
120
|
+
return False
|
|
121
|
+
|
|
122
|
+
pre_text, post_text = full_text.split(target_text, 1)
|
|
123
|
+
insertion_index = paragraph_elem.index(run_element)
|
|
124
|
+
run_props = run_element.find("w:rPr", namespaces=NS_MAP)
|
|
125
|
+
|
|
126
|
+
new_elements: list[etree._Element] = []
|
|
127
|
+
|
|
128
|
+
if pre_text:
|
|
129
|
+
new_elements.append(_make_text_run(pre_text, run_props))
|
|
130
|
+
|
|
131
|
+
cid = str(comment_id)
|
|
132
|
+
new_elements.append(etree.Element(f"{{{NS_W}}}commentRangeStart", {f"{{{NS_W}}}id": cid}))
|
|
133
|
+
new_elements.append(_make_text_run(target_text, run_props))
|
|
134
|
+
new_elements.append(etree.Element(f"{{{NS_W}}}commentRangeEnd", {f"{{{NS_W}}}id": cid}))
|
|
135
|
+
|
|
136
|
+
reference_run = etree.Element(f"{{{NS_W}}}r")
|
|
137
|
+
reference_props = etree.SubElement(reference_run, f"{{{NS_W}}}rPr")
|
|
138
|
+
etree.SubElement(reference_props, f"{{{NS_W}}}rStyle", {f"{{{NS_W}}}val": "CommentReference"})
|
|
139
|
+
etree.SubElement(reference_run, f"{{{NS_W}}}commentReference", {f"{{{NS_W}}}id": cid})
|
|
140
|
+
new_elements.append(reference_run)
|
|
141
|
+
|
|
142
|
+
if post_text:
|
|
143
|
+
new_elements.append(_make_text_run(post_text, run_props))
|
|
144
|
+
|
|
145
|
+
paragraph_elem.remove(run_element)
|
|
146
|
+
for element in reversed(new_elements):
|
|
147
|
+
paragraph_elem.insert(insertion_index, element)
|
|
148
|
+
return True
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def _insert_comment_by_para_id(
|
|
152
|
+
root_doc: etree._Element,
|
|
153
|
+
target_para_id: str,
|
|
154
|
+
target_text: str,
|
|
155
|
+
comment_id: int,
|
|
156
|
+
) -> bool:
|
|
157
|
+
paragraphs = root_doc.xpath(
|
|
158
|
+
f".//w:p[@w14:paraId='{target_para_id}']",
|
|
159
|
+
namespaces=NS_MAP,
|
|
160
|
+
)
|
|
161
|
+
if not paragraphs:
|
|
162
|
+
return False
|
|
163
|
+
|
|
164
|
+
paragraph = paragraphs[0]
|
|
165
|
+
text_elements = [(text_node.getparent(), text_node) for text_node in paragraph.findall(".//w:t", namespaces=NS_MAP)]
|
|
166
|
+
|
|
167
|
+
for run_node, text_node in text_elements:
|
|
168
|
+
if text_node.text and target_text in text_node.text:
|
|
169
|
+
return _split_run_and_insert_comment(
|
|
170
|
+
paragraph_elem=paragraph,
|
|
171
|
+
run_element=run_node,
|
|
172
|
+
text_elem=text_node,
|
|
173
|
+
target_text=target_text,
|
|
174
|
+
comment_id=comment_id,
|
|
175
|
+
)
|
|
176
|
+
return False
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def _update_relationships(work_dir: Path) -> None:
|
|
180
|
+
rels_path = work_dir / "word" / "_rels" / "document.xml.rels"
|
|
181
|
+
tree = etree.parse(str(rels_path))
|
|
182
|
+
root = tree.getroot()
|
|
183
|
+
|
|
184
|
+
for rel in root.findall(f".//{{{NS_R}}}Relationship"):
|
|
185
|
+
if rel.get("Target") == "comments.xml":
|
|
186
|
+
return
|
|
187
|
+
|
|
188
|
+
existing_rel_ids = []
|
|
189
|
+
for rel in root.findall(f".//{{{NS_R}}}Relationship"):
|
|
190
|
+
rel_id = rel.get("Id", "")
|
|
191
|
+
if rel_id.startswith("rId"):
|
|
192
|
+
try:
|
|
193
|
+
existing_rel_ids.append(int(rel_id.replace("rId", "")))
|
|
194
|
+
except ValueError:
|
|
195
|
+
continue
|
|
196
|
+
|
|
197
|
+
next_id = max(existing_rel_ids) + 1 if existing_rel_ids else 1
|
|
198
|
+
relation = etree.Element(
|
|
199
|
+
f"{{{NS_R}}}Relationship",
|
|
200
|
+
{
|
|
201
|
+
"Id": f"rId{next_id}",
|
|
202
|
+
"Type": "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments",
|
|
203
|
+
"Target": "comments.xml",
|
|
204
|
+
},
|
|
205
|
+
)
|
|
206
|
+
root.append(relation)
|
|
207
|
+
tree.write(str(rels_path), xml_declaration=True, encoding="UTF-8", standalone="yes")
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def _update_content_types(work_dir: Path) -> None:
|
|
211
|
+
content_types_path = work_dir / "[Content_Types].xml"
|
|
212
|
+
tree = etree.parse(str(content_types_path))
|
|
213
|
+
root = tree.getroot()
|
|
214
|
+
|
|
215
|
+
existing = root.xpath(
|
|
216
|
+
".//ct:Override[@PartName='/word/comments.xml']",
|
|
217
|
+
namespaces=NS_MAP,
|
|
218
|
+
)
|
|
219
|
+
if existing:
|
|
220
|
+
return
|
|
221
|
+
|
|
222
|
+
etree.SubElement(
|
|
223
|
+
root,
|
|
224
|
+
f"{{{NS_CT}}}Override",
|
|
225
|
+
{
|
|
226
|
+
"PartName": "/word/comments.xml",
|
|
227
|
+
"ContentType": "application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml",
|
|
228
|
+
},
|
|
229
|
+
)
|
|
230
|
+
tree.write(str(content_types_path), xml_declaration=True, encoding="UTF-8", standalone="yes")
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def _save_and_repack(
|
|
234
|
+
work_dir: Path,
|
|
235
|
+
output_path: Path,
|
|
236
|
+
tree_doc: etree._ElementTree,
|
|
237
|
+
tree_comments: etree._ElementTree,
|
|
238
|
+
) -> None:
|
|
239
|
+
document_path = work_dir / "word" / "document.xml"
|
|
240
|
+
comments_path = work_dir / "word" / "comments.xml"
|
|
241
|
+
|
|
242
|
+
tree_doc.write(str(document_path), xml_declaration=True, encoding="UTF-8", standalone="yes")
|
|
243
|
+
tree_comments.write(str(comments_path), xml_declaration=True, encoding="UTF-8", standalone="yes")
|
|
244
|
+
|
|
245
|
+
output_path.parent.mkdir(parents=True, exist_ok=True)
|
|
246
|
+
with ZipFile(output_path, "w", compression=ZIP_DEFLATED) as archive:
|
|
247
|
+
for file_path in work_dir.rglob("*"):
|
|
248
|
+
if file_path.is_file():
|
|
249
|
+
archive.write(file_path, arcname=file_path.relative_to(work_dir))
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def add_comment(
|
|
253
|
+
input_path: str | Path,
|
|
254
|
+
output_path: str | Path,
|
|
255
|
+
para_id: str,
|
|
256
|
+
target_text: str,
|
|
257
|
+
comment_text: str,
|
|
258
|
+
author: str = "Auto_Bot",
|
|
259
|
+
) -> AddCommentResult:
|
|
260
|
+
source = Path(input_path)
|
|
261
|
+
destination = Path(output_path)
|
|
262
|
+
|
|
263
|
+
if not source.exists():
|
|
264
|
+
raise FileNotFoundError(f"Input .docx not found: {source}")
|
|
265
|
+
if not para_id:
|
|
266
|
+
raise ValueError("para_id must not be empty")
|
|
267
|
+
if not target_text:
|
|
268
|
+
raise ValueError("target_text must not be empty")
|
|
269
|
+
|
|
270
|
+
with TemporaryDirectory(prefix="word_comment_") as temp_dir:
|
|
271
|
+
work_dir = Path(temp_dir)
|
|
272
|
+
_extract_docx(source, work_dir)
|
|
273
|
+
|
|
274
|
+
document_path = work_dir / "word" / "document.xml"
|
|
275
|
+
tree_doc = etree.parse(str(document_path))
|
|
276
|
+
root_doc = tree_doc.getroot()
|
|
277
|
+
tree_comments, root_comments = _prepare_comments_xml(work_dir)
|
|
278
|
+
comment_id = _next_comment_id(root_comments)
|
|
279
|
+
inserted = _insert_comment_by_para_id(root_doc, para_id, target_text, comment_id)
|
|
280
|
+
|
|
281
|
+
if inserted:
|
|
282
|
+
_append_comment(root_comments, comment_id, author, comment_text)
|
|
283
|
+
_update_relationships(work_dir)
|
|
284
|
+
_update_content_types(work_dir)
|
|
285
|
+
_save_and_repack(work_dir, destination, tree_doc, tree_comments)
|
|
286
|
+
return AddCommentResult(
|
|
287
|
+
success=True,
|
|
288
|
+
inserted_count=1,
|
|
289
|
+
para_id=para_id,
|
|
290
|
+
output_path=destination,
|
|
291
|
+
)
|
|
292
|
+
|
|
293
|
+
destination.parent.mkdir(parents=True, exist_ok=True)
|
|
294
|
+
copy2(source, destination)
|
|
295
|
+
|
|
296
|
+
paragraph_exists = bool(
|
|
297
|
+
root_doc.xpath(
|
|
298
|
+
f".//w:p[@w14:paraId='{para_id}']",
|
|
299
|
+
namespaces=NS_MAP,
|
|
300
|
+
)
|
|
301
|
+
)
|
|
302
|
+
reason = "target_text_not_found" if paragraph_exists else "paragraph_not_found"
|
|
303
|
+
return AddCommentResult(
|
|
304
|
+
success=False,
|
|
305
|
+
inserted_count=0,
|
|
306
|
+
para_id=para_id,
|
|
307
|
+
output_path=destination,
|
|
308
|
+
reason=reason,
|
|
309
|
+
)
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: word-comment
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Add Word comments to matching text in .docx files.
|
|
5
|
+
Author: AI-Part-A
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/AI-Part-A/word-comment
|
|
8
|
+
Project-URL: Repository, https://github.com/AI-Part-A/word-comment
|
|
9
|
+
Project-URL: Issues, https://github.com/AI-Part-A/word-comment/issues
|
|
10
|
+
Keywords: docx,word,comments,office,xml
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
18
|
+
Classifier: Operating System :: OS Independent
|
|
19
|
+
Classifier: Topic :: Office/Business
|
|
20
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
21
|
+
Requires-Python: >=3.10
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Requires-Dist: lxml>=5.0.0
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# word-comment
|
|
28
|
+
|
|
29
|
+
`word-comment` adds a Microsoft Word comment to matching text in a `.docx` file.
|
|
30
|
+
|
|
31
|
+
It targets a specific paragraph by `w14:paraId`, finds the first matching occurrence of `target_text` inside that paragraph, and inserts a standard Word comment range plus comment metadata.
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
python -m pip install word-comment
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Python API
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from word_comment import add_comment
|
|
43
|
+
|
|
44
|
+
result = add_comment(
|
|
45
|
+
input_path="input.docx",
|
|
46
|
+
output_path="output.docx",
|
|
47
|
+
para_id="7BD69366",
|
|
48
|
+
target_text="keyword",
|
|
49
|
+
comment_text="comment body",
|
|
50
|
+
author="Auto_Bot",
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
print(result.success)
|
|
54
|
+
print(result.inserted_count)
|
|
55
|
+
print(result.reason)
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## CLI
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
word-comment input.docx output.docx 7BD69366 "keyword" "comment body" --author Auto_Bot
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Behavior
|
|
65
|
+
|
|
66
|
+
- Searches for the paragraph whose `w14:paraId` matches `para_id`
|
|
67
|
+
- Finds the first occurrence of `target_text` in that paragraph
|
|
68
|
+
- Inserts a Word comment range and a `commentReference`
|
|
69
|
+
- Creates `word/comments.xml` when it does not already exist
|
|
70
|
+
- Adds the required relationship and content-type entries when needed
|
|
71
|
+
- Copies the input file unchanged when the paragraph or target text is not found
|
|
72
|
+
|
|
73
|
+
## Return Value
|
|
74
|
+
|
|
75
|
+
`add_comment(...)` returns `AddCommentResult` with:
|
|
76
|
+
|
|
77
|
+
- `success: bool`
|
|
78
|
+
- `inserted_count: int`
|
|
79
|
+
- `para_id: str`
|
|
80
|
+
- `output_path: Path`
|
|
81
|
+
- `reason: str | None`
|
|
82
|
+
|
|
83
|
+
Possible `reason` values:
|
|
84
|
+
|
|
85
|
+
- `paragraph_not_found`
|
|
86
|
+
- `target_text_not_found`
|
|
87
|
+
|
|
88
|
+
## Limitations
|
|
89
|
+
|
|
90
|
+
- The target paragraph must already have a `w14:paraId`
|
|
91
|
+
- `target_text` must exist entirely within a single `w:t` node
|
|
92
|
+
- Only the first match in the selected paragraph is annotated
|
|
93
|
+
- The implementation modifies `word/document.xml` and `word/comments.xml`
|
|
94
|
+
|
|
95
|
+
## Development Release Check
|
|
96
|
+
|
|
97
|
+
Build artifacts locally:
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
python -m build
|
|
101
|
+
python -m twine check dist/*
|
|
102
|
+
```
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/word_comment/__init__.py
|
|
5
|
+
src/word_comment/__main__.py
|
|
6
|
+
src/word_comment/cli.py
|
|
7
|
+
src/word_comment/core.py
|
|
8
|
+
src/word_comment.egg-info/PKG-INFO
|
|
9
|
+
src/word_comment.egg-info/SOURCES.txt
|
|
10
|
+
src/word_comment.egg-info/dependency_links.txt
|
|
11
|
+
src/word_comment.egg-info/entry_points.txt
|
|
12
|
+
src/word_comment.egg-info/requires.txt
|
|
13
|
+
src/word_comment.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
lxml>=5.0.0
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
word_comment
|