zcode-app-cli 3.3.5-1
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.
- package/LICENSE +24 -0
- package/README.md +642 -0
- package/bin/zcode.js +848 -0
- package/config.example.json +110 -0
- package/package.json +81 -0
- package/vendor/.node-bundle-meta.json +6 -0
- package/vendor/extraction.json +11 -0
- package/vendor/node_modules/@zcode/tui/dist/index.js +161875 -0
- package/vendor/node_modules/@zcode/tui/package.json +14 -0
- package/vendor/packages/android-emulator-plugin/.mcp.json +19 -0
- package/vendor/packages/android-emulator-plugin/.zcode-plugin/plugin.json +63 -0
- package/vendor/packages/android-emulator-plugin/README.md +134 -0
- package/vendor/packages/android-emulator-plugin/commands/android-dev.md +11 -0
- package/vendor/packages/android-emulator-plugin/dist/lib/path.d.ts +4 -0
- package/vendor/packages/android-emulator-plugin/dist/lib/path.js +17 -0
- package/vendor/packages/android-emulator-plugin/dist/lib/result.d.ts +5 -0
- package/vendor/packages/android-emulator-plugin/dist/lib/result.js +22 -0
- package/vendor/packages/android-emulator-plugin/dist/lib/run.d.ts +18 -0
- package/vendor/packages/android-emulator-plugin/dist/lib/run.js +99 -0
- package/vendor/packages/android-emulator-plugin/dist/mcp/server.d.ts +3 -0
- package/vendor/packages/android-emulator-plugin/dist/mcp/server.js +47607 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/app.d.ts +39 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/app.js +50 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/avd.d.ts +37 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/avd.js +185 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/build.d.ts +68 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/build.js +131 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/config.d.ts +7 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/config.js +29 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/device.d.ts +20 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/device.js +91 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/logs.d.ts +15 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/logs.js +31 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/preflight.d.ts +15 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/preflight.js +123 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/project-template.d.ts +13 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/project-template.js +211 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/project.d.ts +33 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/project.js +195 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/screenshot.d.ts +14 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/screenshot.js +73 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/sdk.d.ts +14 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/sdk.js +185 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/ui.d.ts +92 -0
- package/vendor/packages/android-emulator-plugin/dist/providers/ui.js +146 -0
- package/vendor/packages/android-emulator-plugin/hooks/hooks.json +3 -0
- package/vendor/packages/android-emulator-plugin/package.json +35 -0
- package/vendor/packages/android-emulator-plugin/skills/android-dev/INSTALL_ENVIRONMENT.md +357 -0
- package/vendor/packages/android-emulator-plugin/skills/android-dev/SKILL.md +73 -0
- package/vendor/packages/android-emulator-plugin/templates/compose-app/README.md +5 -0
- package/vendor/packages/document-skills-plugin/.zcode-plugin/plugin.json +8 -0
- package/vendor/packages/document-skills-plugin/package.json +8 -0
- package/vendor/packages/document-skills-plugin/skills/docx/LICENSE.txt +13 -0
- package/vendor/packages/document-skills-plugin/skills/docx/SKILL.md +201 -0
- package/vendor/packages/document-skills-plugin/skills/docx/references/chart-templates.md +386 -0
- package/vendor/packages/document-skills-plugin/skills/docx/references/common-rules.md +419 -0
- package/vendor/packages/document-skills-plugin/skills/docx/references/decorations.md +537 -0
- package/vendor/packages/document-skills-plugin/skills/docx/references/design-system.md +1797 -0
- package/vendor/packages/document-skills-plugin/skills/docx/references/docx-js-advanced.md +257 -0
- package/vendor/packages/document-skills-plugin/skills/docx/references/docx-js-core.md +332 -0
- package/vendor/packages/document-skills-plugin/skills/docx/references/faq.md +323 -0
- package/vendor/packages/document-skills-plugin/skills/docx/references/math-formulas.md +276 -0
- package/vendor/packages/document-skills-plugin/skills/docx/references/ooxml.md +222 -0
- package/vendor/packages/document-skills-plugin/skills/docx/references/toc.md +264 -0
- package/vendor/packages/document-skills-plugin/skills/docx/routes/comment.md +88 -0
- package/vendor/packages/document-skills-plugin/skills/docx/routes/create.md +207 -0
- package/vendor/packages/document-skills-plugin/skills/docx/routes/edit.md +115 -0
- package/vendor/packages/document-skills-plugin/skills/docx/routes/format.md +120 -0
- package/vendor/packages/document-skills-plugin/skills/docx/routes/read.md +114 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scenes/academic.md +783 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scenes/contract.md +463 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scenes/copywriting.md +139 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scenes/exam.md +698 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scenes/official-doc.md +411 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scenes/report.md +340 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scenes/resume.md +534 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scripts/__init__.py +1 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scripts/add_toc_placeholders.py +749 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scripts/document.py +1333 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scripts/postcheck.py +807 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scripts/templates/comments.xml +3 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scripts/templates/commentsExtended.xml +3 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scripts/templates/commentsExtensible.xml +3 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scripts/templates/commentsIds.xml +3 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scripts/templates/people.xml +3 -0
- package/vendor/packages/document-skills-plugin/skills/docx/scripts/utilities.py +374 -0
- package/vendor/packages/document-skills-plugin/skills/docx/setup.sh +177 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/LICENSE.txt +13 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/SKILL.md +919 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/briefs/academic.md +1058 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/briefs/creative.md +770 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/briefs/poster.md +702 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/briefs/process-advanced.md +284 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/briefs/process.md +319 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/briefs/report.md +1659 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/configs/components.md +153 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/configs/fonts.md +93 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/configs/visual_framework.md +263 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/references/resume-academic.tex +130 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/references/resume-altacv.tex +190 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/scripts/cover_validate.js +367 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/scripts/design_engine.py +2816 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/scripts/html2pdf-next.js +754 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/scripts/html2poster.js +256 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/scripts/pdf.py +2959 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/scripts/pdf_qa.py +901 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/scripts/poster_validate.py +1337 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/scripts/setup.sh +269 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/scripts/toc_validate.py +2075 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/charts.md +320 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/cover-backgrounds.md +384 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/cover.md +1442 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/fill-engine.md +527 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/geometry.md +142 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/overflow.md +630 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/pagination.md +367 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/palette.md +217 -0
- package/vendor/packages/document-skills-plugin/skills/pdf/typesetting/typography.md +20 -0
- package/vendor/packages/ios-simulator-plugin/.mcp.json +15 -0
- package/vendor/packages/ios-simulator-plugin/.zcode-plugin/plugin.json +34 -0
- package/vendor/packages/ios-simulator-plugin/README.md +111 -0
- package/vendor/packages/ios-simulator-plugin/commands/ios-dev.md +11 -0
- package/vendor/packages/ios-simulator-plugin/dist/lib/path.d.ts +4 -0
- package/vendor/packages/ios-simulator-plugin/dist/lib/path.js +18 -0
- package/vendor/packages/ios-simulator-plugin/dist/lib/result.d.ts +5 -0
- package/vendor/packages/ios-simulator-plugin/dist/lib/result.js +20 -0
- package/vendor/packages/ios-simulator-plugin/dist/lib/run.d.ts +17 -0
- package/vendor/packages/ios-simulator-plugin/dist/lib/run.js +83 -0
- package/vendor/packages/ios-simulator-plugin/dist/mcp/server.d.ts +3 -0
- package/vendor/packages/ios-simulator-plugin/dist/mcp/server.js +46833 -0
- package/vendor/packages/ios-simulator-plugin/dist/providers/build.d.ts +112 -0
- package/vendor/packages/ios-simulator-plugin/dist/providers/build.js +99 -0
- package/vendor/packages/ios-simulator-plugin/dist/providers/logs.d.ts +21 -0
- package/vendor/packages/ios-simulator-plugin/dist/providers/logs.js +26 -0
- package/vendor/packages/ios-simulator-plugin/dist/providers/preflight.d.ts +12 -0
- package/vendor/packages/ios-simulator-plugin/dist/providers/preflight.js +62 -0
- package/vendor/packages/ios-simulator-plugin/dist/providers/project.d.ts +35 -0
- package/vendor/packages/ios-simulator-plugin/dist/providers/project.js +288 -0
- package/vendor/packages/ios-simulator-plugin/dist/providers/screenshot.d.ts +22 -0
- package/vendor/packages/ios-simulator-plugin/dist/providers/screenshot.js +30 -0
- package/vendor/packages/ios-simulator-plugin/dist/providers/sim.d.ts +60 -0
- package/vendor/packages/ios-simulator-plugin/dist/providers/sim.js +116 -0
- package/vendor/packages/ios-simulator-plugin/dist/providers/ui.d.ts +57 -0
- package/vendor/packages/ios-simulator-plugin/dist/providers/ui.js +67 -0
- package/vendor/packages/ios-simulator-plugin/hooks/hooks.json +3 -0
- package/vendor/packages/ios-simulator-plugin/package.json +35 -0
- package/vendor/packages/ios-simulator-plugin/skills/ios-dev/SKILL.md +45 -0
- package/vendor/packages/ios-simulator-plugin/templates/swiftui-app/README.md +5 -0
- package/vendor/packages/restore-legacy-sessions-plugin/.zcode-plugin/plugin.json +9 -0
- package/vendor/packages/restore-legacy-sessions-plugin/README.md +35 -0
- package/vendor/packages/restore-legacy-sessions-plugin/commands/restore-legacy-sessions.md +11 -0
- package/vendor/packages/restore-legacy-sessions-plugin/package.json +8 -0
- package/vendor/packages/restore-legacy-sessions-plugin/skills/restore-legacy-sessions/SKILL.md +92 -0
- package/vendor/packages/restore-legacy-sessions-plugin/skills/restore-legacy-sessions/scripts/restore-conversation.mjs +648 -0
- package/vendor/packages/restore-legacy-sessions-plugin/skills/restore-legacy-sessions/scripts/scan-legacy-sessions.mjs +422 -0
- package/vendor/packages/skill-creator-plugin/.zcode-plugin/plugin.json +8 -0
- package/vendor/packages/skill-creator-plugin/package.json +8 -0
- package/vendor/packages/skill-creator-plugin/skills/skill-creator/SKILL.md +159 -0
- package/vendor/packages/zcode-guide-plugin/.zcode-plugin/plugin.json +8 -0
- package/vendor/packages/zcode-guide-plugin/README.md +22 -0
- package/vendor/packages/zcode-guide-plugin/package.json +8 -0
- package/vendor/packages/zcode-guide-plugin/skills/diagnosing-commands/SKILL.md +66 -0
- package/vendor/packages/zcode-guide-plugin/skills/diagnosing-hooks/SKILL.md +66 -0
- package/vendor/packages/zcode-guide-plugin/skills/diagnosing-mcp/SKILL.md +97 -0
- package/vendor/packages/zcode-guide-plugin/skills/diagnosing-plugins/SKILL.md +59 -0
- package/vendor/packages/zcode-guide-plugin/skills/diagnosing-skills/SKILL.md +61 -0
- package/vendor/packages/zcode-guide-plugin/skills/zcode-configuration-guide/SKILL.md +99 -0
- package/vendor/zcode.cjs +2926 -0
- package/zcode-runtime.lock.json +8 -0
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
# OOXML Editing Reference — Document Library API
|
|
2
|
+
|
|
3
|
+
**Important: Read this entire document before editing.** This is the primary reference for modifying existing .docx files.
|
|
4
|
+
|
|
5
|
+
## Document Library (Python) — Primary API
|
|
6
|
+
|
|
7
|
+
Use the `Document` class from `"$DOCX_SCRIPTS/document.py"` for all edits, tracked changes, and comments. It handles infrastructure automatically (people.xml, RSIDs, settings.xml, comments, relationships, content types).
|
|
8
|
+
|
|
9
|
+
**Working with Unicode and Entities:**
|
|
10
|
+
- Both entity notation and Unicode work for search: `contains="“Company"` ≡ `contains="\u201cCompany"`
|
|
11
|
+
- Both work for replacement too
|
|
12
|
+
|
|
13
|
+
### Setup
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
# Find the docx skill root
|
|
17
|
+
find /mnt/skills -name "document.py" -path "*/docx/scripts/*" 2>/dev/null | head -1
|
|
18
|
+
# Skill root = parent of scripts/
|
|
19
|
+
|
|
20
|
+
# Run with PYTHONPATH
|
|
21
|
+
PYTHONPATH=/mnt/skills/docx python your_script.py
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
from scripts.document import Document, DocxXMLEditor
|
|
26
|
+
|
|
27
|
+
# Basic init (auto-creates temp copy, sets up infrastructure)
|
|
28
|
+
doc = Document('unpacked')
|
|
29
|
+
|
|
30
|
+
# Custom author/initials
|
|
31
|
+
doc = Document('unpacked', author="John Doe", initials="JD")
|
|
32
|
+
|
|
33
|
+
# Enable tracked changes
|
|
34
|
+
doc = Document('unpacked', track_revisions=True)
|
|
35
|
+
|
|
36
|
+
# Custom RSID (auto-generated if omitted)
|
|
37
|
+
doc = Document('unpacked', rsid="07DC5ECB")
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Finding Nodes
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
# By text
|
|
44
|
+
node = doc["word/document.xml"].get_node(tag="w:p", contains="specific text")
|
|
45
|
+
|
|
46
|
+
# By line range
|
|
47
|
+
para = doc["word/document.xml"].get_node(tag="w:p", line_number=range(100, 150))
|
|
48
|
+
|
|
49
|
+
# By attributes
|
|
50
|
+
node = doc["word/document.xml"].get_node(tag="w:del", attrs={"w:id": "1"})
|
|
51
|
+
|
|
52
|
+
# By exact line number
|
|
53
|
+
para = doc["word/document.xml"].get_node(tag="w:p", line_number=42)
|
|
54
|
+
|
|
55
|
+
# Combined filters (disambiguation)
|
|
56
|
+
node = doc["word/document.xml"].get_node(tag="w:r", contains="Section", line_number=range(2400, 2500))
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Tracked Changes
|
|
60
|
+
|
|
61
|
+
**CRITICAL**: Only mark text that actually changes. Keep unchanged text outside `<w:del>`/`<w:ins>` tags.
|
|
62
|
+
|
|
63
|
+
**Method Selection**:
|
|
64
|
+
- Regular text → `replace_node()` with `<w:del>`/`<w:ins>`, or `suggest_deletion()` for whole elements
|
|
65
|
+
- Partially modify another's tracked change → `replace_node()` to nest changes
|
|
66
|
+
- Reject another's insertion → `revert_insertion()` (NOT `suggest_deletion()`)
|
|
67
|
+
- Reject another's deletion → `revert_deletion()`
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
# Change one word: "monthly" → "quarterly"
|
|
71
|
+
node = doc["word/document.xml"].get_node(tag="w:r", contains="The report is monthly")
|
|
72
|
+
rpr = tags[0].toxml() if (tags := node.getElementsByTagName("w:rPr")) else ""
|
|
73
|
+
replacement = f'<w:r w:rsidR="00AB12CD">{rpr}<w:t>The report is </w:t></w:r><w:del><w:r>{rpr}<w:delText>monthly</w:delText></w:r></w:del><w:ins><w:r>{rpr}<w:t>quarterly</w:t></w:r></w:ins>'
|
|
74
|
+
doc["word/document.xml"].replace_node(node, replacement)
|
|
75
|
+
|
|
76
|
+
# Delete entire run
|
|
77
|
+
node = doc["word/document.xml"].get_node(tag="w:r", contains="text to delete")
|
|
78
|
+
doc["word/document.xml"].suggest_deletion(node)
|
|
79
|
+
|
|
80
|
+
# Delete entire paragraph
|
|
81
|
+
para = doc["word/document.xml"].get_node(tag="w:p", contains="paragraph to delete")
|
|
82
|
+
doc["word/document.xml"].suggest_deletion(para)
|
|
83
|
+
|
|
84
|
+
# Insert new content after a node
|
|
85
|
+
node = doc["word/document.xml"].get_node(tag="w:r", contains="existing text")
|
|
86
|
+
doc["word/document.xml"].insert_after(node, '<w:ins><w:r><w:t>new text</w:t></w:r></w:ins>')
|
|
87
|
+
|
|
88
|
+
# Add new numbered list item
|
|
89
|
+
target_para = doc["word/document.xml"].get_node(tag="w:p", contains="existing list item")
|
|
90
|
+
pPr = tags[0].toxml() if (tags := target_para.getElementsByTagName("w:pPr")) else ""
|
|
91
|
+
new_item = f'<w:p>{pPr}<w:r><w:t>New item</w:t></w:r></w:p>'
|
|
92
|
+
tracked_para = DocxXMLEditor.suggest_paragraph(new_item)
|
|
93
|
+
doc["word/document.xml"].insert_after(target_para, tracked_para)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Handling Other Authors' Changes
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
# Partially delete another author's insertion
|
|
100
|
+
node = doc["word/document.xml"].get_node(tag="w:ins", attrs={"w:id": "5"})
|
|
101
|
+
replacement = '''<w:ins w:author="Jane Smith" w:date="2025-01-15T10:00:00Z">
|
|
102
|
+
<w:r><w:t>quarterly </w:t></w:r>
|
|
103
|
+
<w:del><w:r><w:delText>financial </w:delText></w:r></w:del>
|
|
104
|
+
<w:r><w:t>report</w:t></w:r>
|
|
105
|
+
</w:ins>'''
|
|
106
|
+
doc["word/document.xml"].replace_node(node, replacement)
|
|
107
|
+
|
|
108
|
+
# Reject insertion (wraps in deletion)
|
|
109
|
+
ins = doc["word/document.xml"].get_node(tag="w:ins", attrs={"w:id": "5"})
|
|
110
|
+
doc["word/document.xml"].revert_insertion(ins)
|
|
111
|
+
|
|
112
|
+
# Reject deletion (restores deleted content)
|
|
113
|
+
del_elem = doc["word/document.xml"].get_node(tag="w:del", attrs={"w:id": "3"})
|
|
114
|
+
doc["word/document.xml"].revert_deletion(del_elem)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Comments
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
doc = Document('unpacked', author="Z.ai", initials="Z")
|
|
121
|
+
|
|
122
|
+
# Comment on a range
|
|
123
|
+
start = doc["word/document.xml"].get_node(tag="w:del", attrs={"w:id": "1"})
|
|
124
|
+
end = doc["word/document.xml"].get_node(tag="w:ins", attrs={"w:id": "2"})
|
|
125
|
+
doc.add_comment(start=start, end=end, text="Explanation of this change")
|
|
126
|
+
|
|
127
|
+
# Comment on paragraph
|
|
128
|
+
para = doc["word/document.xml"].get_node(tag="w:p", contains="text")
|
|
129
|
+
doc.add_comment(start=para, end=para, text="Comment here")
|
|
130
|
+
|
|
131
|
+
# Comment on newly created tracked change
|
|
132
|
+
node = doc["word/document.xml"].get_node(tag="w:r", contains="old")
|
|
133
|
+
new_nodes = doc["word/document.xml"].replace_node(
|
|
134
|
+
node, '<w:del><w:r><w:delText>old</w:delText></w:r></w:del><w:ins><w:r><w:t>new</w:t></w:r></w:ins>')
|
|
135
|
+
doc.add_comment(start=new_nodes[0], end=new_nodes[1], text="Changed per requirements")
|
|
136
|
+
|
|
137
|
+
# Reply to comment
|
|
138
|
+
doc.reply_to_comment(parent_comment_id=0, text="I agree")
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Images
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
from PIL import Image
|
|
145
|
+
import shutil, os
|
|
146
|
+
|
|
147
|
+
doc = Document('unpacked')
|
|
148
|
+
media_dir = os.path.join(doc.unpacked_path, 'word/media')
|
|
149
|
+
os.makedirs(media_dir, exist_ok=True)
|
|
150
|
+
shutil.copy('image.png', os.path.join(media_dir, 'image1.png'))
|
|
151
|
+
|
|
152
|
+
img = Image.open(os.path.join(media_dir, 'image1.png'))
|
|
153
|
+
width_emus = int(6.5 * 914400) # 6.5" usable width
|
|
154
|
+
height_emus = int(width_emus * img.size[1] / img.size[0])
|
|
155
|
+
|
|
156
|
+
# Add relationship
|
|
157
|
+
rels_editor = doc['word/_rels/document.xml.rels']
|
|
158
|
+
next_rid = rels_editor.get_next_rid()
|
|
159
|
+
rels_editor.append_to(rels_editor.dom.documentElement,
|
|
160
|
+
f'<Relationship Id="{next_rid}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.png"/>')
|
|
161
|
+
doc['[Content_Types].xml'].append_to(doc['[Content_Types].xml'].dom.documentElement,
|
|
162
|
+
'<Default Extension="png" ContentType="image/png"/>')
|
|
163
|
+
|
|
164
|
+
# Insert
|
|
165
|
+
node = doc["word/document.xml"].get_node(tag="w:p", line_number=100)
|
|
166
|
+
doc["word/document.xml"].insert_after(node, f'''<w:p><w:r><w:drawing>
|
|
167
|
+
<wp:inline distT="0" distB="0" distL="0" distR="0">
|
|
168
|
+
<wp:extent cx="{width_emus}" cy="{height_emus}"/>
|
|
169
|
+
<wp:docPr id="1" name="Picture 1"/>
|
|
170
|
+
<a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
|
|
171
|
+
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
|
|
172
|
+
<pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
|
|
173
|
+
<pic:nvPicPr><pic:cNvPr id="1" name="image1.png"/><pic:cNvPicPr/></pic:nvPicPr>
|
|
174
|
+
<pic:blipFill><a:blip r:embed="{next_rid}"/><a:stretch><a:fillRect/></a:stretch></pic:blipFill>
|
|
175
|
+
<pic:spPr><a:xfrm><a:ext cx="{width_emus}" cy="{height_emus}"/></a:xfrm><a:prstGeom prst="rect"><a:avLst/></a:prstGeom></pic:spPr>
|
|
176
|
+
</pic:pic>
|
|
177
|
+
</a:graphicData>
|
|
178
|
+
</a:graphic>
|
|
179
|
+
</wp:inline>
|
|
180
|
+
</w:drawing></w:r></w:p>''')
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### Saving
|
|
184
|
+
|
|
185
|
+
```python
|
|
186
|
+
doc.save() # Validates + copies back to original dir
|
|
187
|
+
doc.save('modified-unpacked') # Save to different location
|
|
188
|
+
doc.save(validate=False) # Skip validation (debug only)
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### Direct DOM Manipulation
|
|
192
|
+
|
|
193
|
+
```python
|
|
194
|
+
editor = doc["word/document.xml"]
|
|
195
|
+
node = doc["word/document.xml"].get_node(tag="w:p", line_number=5)
|
|
196
|
+
parent = node.parentNode
|
|
197
|
+
parent.removeChild(node)
|
|
198
|
+
|
|
199
|
+
# General replacement (without tracked changes)
|
|
200
|
+
old = doc["word/document.xml"].get_node(tag="w:p", contains="original")
|
|
201
|
+
doc["word/document.xml"].replace_node(old, "<w:p><w:r><w:t>replacement</w:t></w:r></w:p>")
|
|
202
|
+
|
|
203
|
+
# Chained insertions
|
|
204
|
+
node = doc["word/document.xml"].get_node(tag="w:r", line_number=100)
|
|
205
|
+
nodes = doc["word/document.xml"].insert_after(node, "<w:r><w:t>A</w:t></w:r>")
|
|
206
|
+
nodes = doc["word/document.xml"].insert_after(nodes[-1], "<w:r><w:t>B</w:t></w:r>")
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
## Schema Compliance Quick Reference
|
|
210
|
+
|
|
211
|
+
- **Element ordering in `<w:pPr>`**: `<w:pStyle>` → `<w:numPr>` → `<w:spacing>` → `<w:ind>` → `<w:jc>`
|
|
212
|
+
- **Whitespace**: `xml:space='preserve'` on `<w:t>` with leading/trailing spaces
|
|
213
|
+
- **RSIDs**: 8-digit hex only (0-9, A-F)
|
|
214
|
+
- **trackRevisions**: Add `<w:trackRevisions/>` after `<w:proofState>` in settings.xml
|
|
215
|
+
- **`<w:del>`/`<w:ins>` placement**: At paragraph level, containing complete `<w:r>` elements. Never nest inside `<w:r>`.
|
|
216
|
+
|
|
217
|
+
## Validation Rules
|
|
218
|
+
|
|
219
|
+
The validator ensures document text matches the original after reverting GLM's changes:
|
|
220
|
+
- **Never modify text inside another author's `<w:ins>` or `<w:del>` tags**
|
|
221
|
+
- **Use nested deletions** to remove another author's insertions
|
|
222
|
+
- **Every edit must be tracked** with `<w:ins>` or `<w:del>` tags
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
# Table of Contents (TOC) — Complete Reference
|
|
2
|
+
|
|
3
|
+
> **This is the single source of truth for all TOC rules.** Other files should reference this file instead of duplicating TOC instructions.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
DOCX TOC is a **3-step process**: Code → Post-process → User opens Word.
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
Step A: docx-js code generates empty TOC field structure
|
|
11
|
+
Step B: add_toc_placeholders.py fills it with visible placeholder entries
|
|
12
|
+
Step C: User opens Word → "Update Field" → real page numbers replace placeholders
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
All 3 steps are **mandatory**. Skipping any step results in a broken or empty TOC.
|
|
16
|
+
|
|
17
|
+
## When to Add TOC
|
|
18
|
+
|
|
19
|
+
- **Recommended**: Long or complex documents with many headings (reports, theses, papers, manuals)
|
|
20
|
+
- **Do NOT add**: Resumes, contracts, letters, exam papers, short documents
|
|
21
|
+
- **postcheck rule**: If document contains a "目录" title but no `TableOfContents` element → error
|
|
22
|
+
|
|
23
|
+
## Step A: Code Generation (docx-js)
|
|
24
|
+
|
|
25
|
+
Insert **4 elements** in sequence:
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
const { TableOfContents, Paragraph, TextRun, PageBreak, AlignmentType } = require("docx");
|
|
29
|
+
|
|
30
|
+
// 1. TOC title — ⛔ DO NOT use HeadingLevel (or TOC will index itself!)
|
|
31
|
+
new Paragraph({
|
|
32
|
+
alignment: AlignmentType.CENTER,
|
|
33
|
+
spacing: { before: 480, after: 360 },
|
|
34
|
+
children: [new TextRun({
|
|
35
|
+
text: "目 录", // or "Table of Contents" for English docs
|
|
36
|
+
bold: true, size: 32,
|
|
37
|
+
font: { eastAsia: "SimHei", ascii: "Times New Roman" }
|
|
38
|
+
})],
|
|
39
|
+
}),
|
|
40
|
+
|
|
41
|
+
// 2. TOC field element — ⚠️ first parameter is NOT displayed, it's internal name only
|
|
42
|
+
new TableOfContents("Table of Contents", {
|
|
43
|
+
hyperlink: true,
|
|
44
|
+
headingStyleRange: "1-3", // match HeadingLevel range used in document
|
|
45
|
+
}),
|
|
46
|
+
|
|
47
|
+
// 3. ★ MANDATORY Refresh Hint — tells user how to update page numbers
|
|
48
|
+
new Paragraph({
|
|
49
|
+
spacing: { before: 200 },
|
|
50
|
+
children: [new TextRun({
|
|
51
|
+
text: "Note: This Table of Contents is generated via field codes. To ensure page number accuracy after editing, please right-click the TOC and select \"Update Field.\"",
|
|
52
|
+
italics: true, size: 18, color: "888888"
|
|
53
|
+
})]
|
|
54
|
+
}),
|
|
55
|
+
|
|
56
|
+
// 4. ★ MANDATORY PageBreak after TOC — prevents TOC and body merging on same page
|
|
57
|
+
new Paragraph({ children: [new PageBreak()] }),
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Heading Requirements
|
|
61
|
+
|
|
62
|
+
**⚠️ CRITICAL**: TOC only picks up paragraphs with `heading: HeadingLevel.HEADING_X`.
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
// ✅ Correct — Heading style, TOC can index
|
|
66
|
+
new Paragraph({
|
|
67
|
+
heading: HeadingLevel.HEADING_1,
|
|
68
|
+
children: [new TextRun({ text: "第一章 引言", bold: true, size: 32, color: c(P.primary) })]
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
// ❌ Wrong — manual bold + large font, TOC cannot detect
|
|
72
|
+
new Paragraph({
|
|
73
|
+
children: [new TextRun({ text: "第一章 引言", bold: true, size: 32, color: c(P.primary) })]
|
|
74
|
+
})
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
**Exceptions:**
|
|
78
|
+
- Cover title: does NOT need Heading style (should not appear in TOC)
|
|
79
|
+
- "目录" title: **MUST NOT** use Heading style (prevents TOC from indexing itself)
|
|
80
|
+
|
|
81
|
+
## Step B: Post-Processing Script
|
|
82
|
+
|
|
83
|
+
**MUST** run after generating the DOCX file:
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
python3 "$DOCX_SCRIPTS/add_toc_placeholders.py" output.docx --auto
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### What the script does
|
|
90
|
+
|
|
91
|
+
1. Extracts Heading 1-3 from the document as TOC entries
|
|
92
|
+
2. Fixes docx-js fldChar structure bug (begin+instrText+separate merged in one `<w:r>`)
|
|
93
|
+
3. Patches `settings.xml` with `updateFields=true` (Word prompts to refresh on open)
|
|
94
|
+
4. Ensures Heading styles have `outlineLvl` (required for TOC field update)
|
|
95
|
+
5. Ensures TOC 1/2/3 styles exist in `styles.xml`
|
|
96
|
+
6. Injects placeholder entries with HYPERLINK + PAGEREF between `separate` and `end` fldChars
|
|
97
|
+
7. Handles duplicate heading texts (each gets its own bookmark)
|
|
98
|
+
|
|
99
|
+
### Error handling
|
|
100
|
+
|
|
101
|
+
The script **exits with code 1** if:
|
|
102
|
+
- No TOC field structure found (missing `TableOfContents` element)
|
|
103
|
+
- TOC field has `begin` but no `separate` fldChar (malformed structure)
|
|
104
|
+
- Field structure exists but no TOC instrText detected
|
|
105
|
+
|
|
106
|
+
**If exit code = 1 → the generated code is wrong. Fix the code and regenerate.**
|
|
107
|
+
|
|
108
|
+
### Options
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
# Auto mode (recommended — default behavior)
|
|
112
|
+
python3 "$DOCX_SCRIPTS/add_toc_placeholders.py" output.docx --auto
|
|
113
|
+
|
|
114
|
+
# Manual entries
|
|
115
|
+
python3 "$DOCX_SCRIPTS/add_toc_placeholders.py" output.docx \
|
|
116
|
+
--entries '[{"level":1,"text":"Chapter 1","page":"1"},{"level":2,"text":"Section 1.1","page":"2"}]'
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Step C: User Opens in Word/WPS
|
|
120
|
+
|
|
121
|
+
- **Word**: Detects `updateFields=true` → prompts "Update field?" → click Yes → real page numbers
|
|
122
|
+
- **WPS**: May NOT auto-prompt. User must: right-click TOC → "Update Field" → "Update entire table"
|
|
123
|
+
|
|
124
|
+
The placeholder entries ensure TOC is **not blank** even without updating — users see heading titles with approximate page numbers.
|
|
125
|
+
|
|
126
|
+
## Multi-Section Page Numbering
|
|
127
|
+
|
|
128
|
+
When a document has a TOC, the TOC MUST be in its own section so that body page numbering starts from 1. This applies to **all document types with a TOC** (reports, whitepapers, PRDs, academic papers, etc.) — not just academic papers.
|
|
129
|
+
|
|
130
|
+
**Mandatory 3-section architecture for documents with cover + TOC:**
|
|
131
|
+
|
|
132
|
+
```js
|
|
133
|
+
sections: [
|
|
134
|
+
{ /* Section 1: Cover — no page number, no footer */
|
|
135
|
+
properties: {
|
|
136
|
+
page: { size: pgSize, margin: pgMargin },
|
|
137
|
+
// ⚠️ Do NOT set page.pageNumbers here — docx-js emits empty <pgNumType/> which confuses WPS
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
{ /* Section 2: Front matter (abstract, TOC) — Roman numerals */
|
|
141
|
+
properties: {
|
|
142
|
+
type: SectionType.NEXT_PAGE,
|
|
143
|
+
page: {
|
|
144
|
+
size: pgSize, margin: pgMargin,
|
|
145
|
+
pageNumbers: { start: 1, formatType: NumberFormat.UPPER_ROMAN }, // I, II, III...
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
footers: { default: pageNumFooter() }, // see footer rules below
|
|
149
|
+
children: [/* abstract + TOC title + TableOfContents + PageBreak */]
|
|
150
|
+
},
|
|
151
|
+
{ /* Section 3: Body — Arabic numerals starting from 1 */
|
|
152
|
+
properties: {
|
|
153
|
+
type: SectionType.NEXT_PAGE,
|
|
154
|
+
page: {
|
|
155
|
+
size: pgSize, margin: pgMargin,
|
|
156
|
+
pageNumbers: { start: 1, formatType: NumberFormat.DECIMAL }, // 1, 2, 3...
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
footers: { default: pageNumFooter() },
|
|
160
|
+
children: [/* body content */]
|
|
161
|
+
},
|
|
162
|
+
]
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### ⚠️ Page Number API — Correct Nesting (CRITICAL)
|
|
166
|
+
|
|
167
|
+
Page number settings MUST be nested inside `page.pageNumbers`, NOT at properties top level:
|
|
168
|
+
|
|
169
|
+
```js
|
|
170
|
+
// ❌ WRONG — docx-js ignores these, pgNumType will be empty
|
|
171
|
+
properties: {
|
|
172
|
+
pageNumberStart: 1,
|
|
173
|
+
pageNumberFormatType: NumberFormat.DECIMAL,
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// ✅ CORRECT — docx-js writes start= and fmt= attributes
|
|
177
|
+
properties: {
|
|
178
|
+
page: {
|
|
179
|
+
pageNumbers: { start: 1, formatType: NumberFormat.DECIMAL },
|
|
180
|
+
},
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### ⚠️ Footer Field Instruction — WPS Compatibility (CRITICAL)
|
|
185
|
+
|
|
186
|
+
WPS may ignore `pgNumType fmt` in the section properties. To ensure correct display, the footer PAGE field **MUST** include an explicit format switch via **post-processing**:
|
|
187
|
+
|
|
188
|
+
After generating the docx, unzip and patch each footer XML:
|
|
189
|
+
- **Roman numeral footer**: replace `PAGE` with `PAGE \* ROMAN \\** MERGEFORMAT`
|
|
190
|
+
- **Arabic numeral footer**: replace `PAGE \* arabic \* MERGEFORMAT`
|
|
191
|
+
|
|
192
|
+
**⚠️ NEVER use `\* decimal` in instrText** — `decimal` is a docx-js API enum value (`NumberFormat.DECIMAL` for `pgNumType` XML attribute), NOT a valid Word field format switch. Using it causes page numbers to render as "1decimal", "2decimal". The correct Word field switch for Arabic numerals is always `\* arabic`.
|
|
193
|
+
|
|
194
|
+
```js
|
|
195
|
+
// Post-process footer XML:
|
|
196
|
+
footerXml = footerXml.replace(
|
|
197
|
+
/(<w:instrText[^>]*>)\s*PAGE\s*(<\/w:instrText>)/g,
|
|
198
|
+
'$1 PAGE \\* ROMAN \\** MERGEFORMAT $2' // or "arabic" for body section
|
|
199
|
+
);
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
Also remove any empty `<w:pgNumType/>` from the cover section (docx-js emits these even when no pageNumbers is set):
|
|
203
|
+
```js
|
|
204
|
+
docXml = docXml.replace(/<w:pgNumType\/>/g, "");
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### Page Numbering Rules
|
|
208
|
+
|
|
209
|
+
| Section | Content | Format | Start | Footer |
|
|
210
|
+
|---------|---------|--------|-------|--------|
|
|
211
|
+
| Cover | Title page | None | — | No footer |
|
|
212
|
+
| Front matter | Abstract, TOC | Roman (I, II, III) | 1 | `PAGE \* ROMAN` |
|
|
213
|
+
| Body | Main content | Arabic (1, 2, 3) | 1 | `PAGE \* arabic` |
|
|
214
|
+
|
|
215
|
+
⚠️ **The body section MUST set `pageNumbers: { start: 1 }`** — otherwise page numbers continue from the front matter pages, causing TOC page references to be offset. This is the #1 cause of "TOC page numbers are wrong".
|
|
216
|
+
|
|
217
|
+
### Common Causes of Incorrect Page Numbers
|
|
218
|
+
|
|
219
|
+
| Cause | Fix |
|
|
220
|
+
|-------|-----|
|
|
221
|
+
| `pageNumberStart` at properties top level | Move to `page: { pageNumbers: { start: 1 } }` |
|
|
222
|
+
| Cover section emits empty `<pgNumType/>` | Post-process to remove it |
|
|
223
|
+
| Footer uses bare `PAGE` without format switch | Post-process to add `\* roman` or `\* arabic` |
|
|
224
|
+
| Cover and body in same section | Separate cover into its own section |
|
|
225
|
+
| Multiple sections without pageNumbers.start | Explicitly set on each section needing independent counting |
|
|
226
|
+
| headingStyleRange doesn't match headings | Ensure `headingStyleRange: "1-3"` covers all HeadingLevel values used |
|
|
227
|
+
| Cover section has header/footer | Don't set header/footer on cover section |
|
|
228
|
+
|
|
229
|
+
## TOC Refresh Hint (MANDATORY)
|
|
230
|
+
|
|
231
|
+
**⚠️ When the document contains a TOC, you MUST add the following hint paragraph between the `TableOfContents` element and the PageBreak (so it appears on the TOC page, not the body page).** This ensures users know how to refresh page numbers after editing.
|
|
232
|
+
|
|
233
|
+
```js
|
|
234
|
+
new Paragraph({
|
|
235
|
+
spacing: { before: 200 },
|
|
236
|
+
children: [new TextRun({
|
|
237
|
+
text: "Note: This Table of Contents is generated via field codes. To ensure page number accuracy after editing, please right-click the TOC and select \"Update Field.\"",
|
|
238
|
+
italics: true, size: 18, color: "888888"
|
|
239
|
+
})]
|
|
240
|
+
}),
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## 5 Common TOC Bugs
|
|
244
|
+
|
|
245
|
+
| # | Bug | Symptom | Fix |
|
|
246
|
+
|---|-----|---------|-----|
|
|
247
|
+
| 1 | "目录" heading uses `HeadingLevel.HEADING_1` | TOC includes "目录" as an entry | Remove `heading:` from TOC title paragraph |
|
|
248
|
+
| 2 | No `PageBreak` after `TableOfContents` | TOC and body text on same page | Add `new Paragraph({ children: [new PageBreak()] })` after TOC |
|
|
249
|
+
| 3 | Missing `TableOfContents` element | Script cannot inject placeholders, TOC is empty | Always include `new TableOfContents(...)` in code |
|
|
250
|
+
| 4 | Headings use bold+large instead of `HeadingLevel` | TOC is empty even after running script | Change all body headings to `heading: HeadingLevel.HEADING_X` |
|
|
251
|
+
| 5 | Script not run or exit code ignored | TOC page shows only title + blank space | Always run script; if exit code = 1, fix code and regenerate |
|
|
252
|
+
|
|
253
|
+
## Checklist (for self-check during generation)
|
|
254
|
+
|
|
255
|
+
- [ ] Document has 3+ H1 → TOC is included
|
|
256
|
+
- [ ] "目录" heading does NOT use `HeadingLevel` (prevents self-indexing)
|
|
257
|
+
- [ ] `new TableOfContents(...)` element present (not just plain text)
|
|
258
|
+
- [ ] `PageBreak` exists after TOC element (prevents merging with body)
|
|
259
|
+
- [ ] All body chapter headings use `heading: HeadingLevel.HEADING_X`
|
|
260
|
+
- [ ] `add_toc_placeholders.py --auto` runs after generation
|
|
261
|
+
- [ ] Script exit code checked — if 1, fix code and regenerate
|
|
262
|
+
- [ ] TOC page has visible placeholder content (not empty)
|
|
263
|
+
- [ ] **TOC Refresh Hint present** — italic gray note after TOC PageBreak telling user to right-click → "Update Field"
|
|
264
|
+
- [ ] `outlineLevel: 0` for H1, `1` for H2, etc. (needed for TOC field update)
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# Route: Add Comments
|
|
2
|
+
|
|
3
|
+
## Method 1: python-docx (Recommended — Simple)
|
|
4
|
+
|
|
5
|
+
```python
|
|
6
|
+
from docx import Document
|
|
7
|
+
from docx.oxml.ns import qn
|
|
8
|
+
from docx.oxml import OxmlElement
|
|
9
|
+
from datetime import datetime
|
|
10
|
+
|
|
11
|
+
def add_comment(paragraph, comment_text, author="GLM", initials="G"):
|
|
12
|
+
"""Add a comment to an entire paragraph."""
|
|
13
|
+
# Create comment reference
|
|
14
|
+
comment_id = str(hash(comment_text) % 10000)
|
|
15
|
+
|
|
16
|
+
# Add to comments.xml (need to create if not exists)
|
|
17
|
+
# ... complex XML manipulation required
|
|
18
|
+
pass
|
|
19
|
+
|
|
20
|
+
# Simpler approach: use python-docx-ng or manipulate XML directly
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
**Note**: python-docx has limited native comment support. For reliable results, use the OOXML method.
|
|
24
|
+
|
|
25
|
+
## Method 2: OOXML Direct Manipulation (Reliable)
|
|
26
|
+
|
|
27
|
+
### Step 1: Unpack
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
mkdir work && cd work && unzip ../input.docx
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Step 2: Create/update word/comments.xml
|
|
34
|
+
|
|
35
|
+
```xml
|
|
36
|
+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
|
37
|
+
<w:comments xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
|
|
38
|
+
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
|
|
39
|
+
<w:comment w:id="1" w:author="Reviewer" w:date="2024-01-15T10:30:00Z" w:initials="R">
|
|
40
|
+
<w:p>
|
|
41
|
+
<w:r>
|
|
42
|
+
<w:t>This section needs more detail.</w:t>
|
|
43
|
+
</w:r>
|
|
44
|
+
</w:p>
|
|
45
|
+
</w:comment>
|
|
46
|
+
</w:comments>
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Step 3: Mark comment range in document.xml
|
|
50
|
+
|
|
51
|
+
```xml
|
|
52
|
+
<w:commentRangeStart w:id="1"/>
|
|
53
|
+
<w:r><w:t>Text being commented on</w:t></w:r>
|
|
54
|
+
<w:commentRangeEnd w:id="1"/>
|
|
55
|
+
<w:r>
|
|
56
|
+
<w:rPr><w:rStyle w:val="CommentReference"/></w:rPr>
|
|
57
|
+
<w:commentReference w:id="1"/>
|
|
58
|
+
</w:r>
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Step 4: Update relationships
|
|
62
|
+
|
|
63
|
+
In `word/_rels/document.xml.rels`, add:
|
|
64
|
+
```xml
|
|
65
|
+
<Relationship Id="rIdComments" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments" Target="comments.xml"/>
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Step 5: Update Content_Types
|
|
69
|
+
|
|
70
|
+
In `[Content_Types].xml`, ensure:
|
|
71
|
+
```xml
|
|
72
|
+
<Override PartName="/word/comments.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml"/>
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Step 6: Pack
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
zip -r ../output.docx . -x ".*"
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## When to Use Each Method
|
|
82
|
+
|
|
83
|
+
| Scenario | Method |
|
|
84
|
+
|----------|--------|
|
|
85
|
+
| Add 1-2 simple comments | OOXML |
|
|
86
|
+
| Batch review (many comments) | OOXML with Python script |
|
|
87
|
+
| Comment on specific words | OOXML (precise range control) |
|
|
88
|
+
| Quick annotation | python-docx if available |
|