@iasiv5/dsh-skills 1.0.6 → 1.2.0

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.
Files changed (192) hide show
  1. package/NOTICE.md +7 -10
  2. package/README.md +1 -1
  3. package/package.json +1 -1
  4. package/skills/answer-me-with-html/SKILL.md +3 -1
  5. package/skills/answer-me-with-html/references/settings.md +1 -1
  6. package/skills/answer-me-with-html/references/video.md +1 -0
  7. package/skills/answer-me-with-html/scripts/am.mjs +2567 -2064
  8. package/skills/docx/LICENSE.txt +202 -0
  9. package/skills/docx/SKILL.md +91 -0
  10. package/skills/docx/scripts/__init__.py +1 -0
  11. package/skills/docx/scripts/accept_changes.py +135 -0
  12. package/skills/docx/scripts/comment.py +368 -0
  13. package/skills/docx/scripts/merge_runs.py +310 -0
  14. package/skills/docx/scripts/office/helpers/__init__.py +111 -0
  15. package/skills/docx/scripts/office/helpers/pptx_chart.py +170 -0
  16. package/skills/docx/scripts/office/helpers/pptx_slide.py +60 -0
  17. package/skills/docx/scripts/office/helpers/pptx_theme.py +114 -0
  18. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chart.xsd +1499 -0
  19. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chartDrawing.xsd +146 -0
  20. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-diagram.xsd +1085 -0
  21. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-lockedCanvas.xsd +11 -0
  22. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-main.xsd +3081 -0
  23. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-picture.xsd +23 -0
  24. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-spreadsheetDrawing.xsd +185 -0
  25. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-wordprocessingDrawing.xsd +287 -0
  26. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/pml.xsd +1676 -0
  27. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-additionalCharacteristics.xsd +28 -0
  28. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-bibliography.xsd +144 -0
  29. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-commonSimpleTypes.xsd +174 -0
  30. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlDataProperties.xsd +25 -0
  31. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlSchemaProperties.xsd +18 -0
  32. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesCustom.xsd +59 -0
  33. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesExtended.xsd +56 -0
  34. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesVariantTypes.xsd +195 -0
  35. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-math.xsd +582 -0
  36. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-relationshipReference.xsd +25 -0
  37. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/sml.xsd +4439 -0
  38. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-main.xsd +570 -0
  39. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-officeDrawing.xsd +509 -0
  40. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-presentationDrawing.xsd +12 -0
  41. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-spreadsheetDrawing.xsd +108 -0
  42. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-wordprocessingDrawing.xsd +96 -0
  43. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/wml.xsd +3646 -0
  44. package/skills/docx/scripts/office/schemas/ISO-IEC29500-4_2016/xml.xsd +116 -0
  45. package/skills/docx/scripts/office/schemas/ecma/fouth-edition/opc-contentTypes.xsd +42 -0
  46. package/skills/docx/scripts/office/schemas/ecma/fouth-edition/opc-coreProperties.xsd +50 -0
  47. package/skills/docx/scripts/office/schemas/ecma/fouth-edition/opc-digSig.xsd +49 -0
  48. package/skills/docx/scripts/office/schemas/ecma/fouth-edition/opc-relationships.xsd +33 -0
  49. package/skills/docx/scripts/office/schemas/mce/mc.xsd +75 -0
  50. package/skills/docx/scripts/office/schemas/microsoft/wml-2010.xsd +560 -0
  51. package/skills/docx/scripts/office/schemas/microsoft/wml-2012.xsd +67 -0
  52. package/skills/docx/scripts/office/schemas/microsoft/wml-2018.xsd +14 -0
  53. package/skills/docx/scripts/office/schemas/microsoft/wml-cex-2018.xsd +20 -0
  54. package/skills/docx/scripts/office/schemas/microsoft/wml-cid-2016.xsd +13 -0
  55. package/skills/docx/scripts/office/schemas/microsoft/wml-sdtdatahash-2020.xsd +4 -0
  56. package/skills/docx/scripts/office/schemas/microsoft/wml-symex-2015.xsd +8 -0
  57. package/skills/docx/scripts/office/soffice.py +192 -0
  58. package/skills/docx/scripts/office/validate.py +173 -0
  59. package/skills/docx/scripts/office/validators/__init__.py +15 -0
  60. package/skills/docx/scripts/office/validators/base.py +875 -0
  61. package/skills/docx/scripts/office/validators/docx.py +466 -0
  62. package/skills/docx/scripts/office/validators/pptx.py +441 -0
  63. package/skills/docx/scripts/office/validators/redlining.py +299 -0
  64. package/skills/docx/scripts/templates/comments.xml +3 -0
  65. package/skills/docx/scripts/templates/commentsExtended.xml +3 -0
  66. package/skills/docx/scripts/templates/commentsExtensible.xml +3 -0
  67. package/skills/docx/scripts/templates/commentsIds.xml +3 -0
  68. package/skills/docx/scripts/templates/people.xml +3 -0
  69. package/skills/impeccable/SKILL.md +87 -0
  70. package/skills/impeccable/reference/adapt.md +318 -0
  71. package/skills/impeccable/reference/adapt.native.md +58 -0
  72. package/skills/impeccable/reference/android.md +46 -0
  73. package/skills/impeccable/reference/animate.md +89 -0
  74. package/skills/impeccable/reference/audit.md +150 -0
  75. package/skills/impeccable/reference/audit.native.md +143 -0
  76. package/skills/impeccable/reference/bolder.md +33 -0
  77. package/skills/impeccable/reference/clarify.md +94 -0
  78. package/skills/impeccable/reference/colorize.md +86 -0
  79. package/skills/impeccable/reference/component-review.md +63 -0
  80. package/skills/impeccable/reference/craft-floor.md +44 -0
  81. package/skills/impeccable/reference/craft.md +5 -0
  82. package/skills/impeccable/reference/critique.md +819 -0
  83. package/skills/impeccable/reference/degraded/asset-producer.md +42 -0
  84. package/skills/impeccable/reference/degraded/documenter.md +24 -0
  85. package/skills/impeccable/reference/degraded/finish-reviewer.md +38 -0
  86. package/skills/impeccable/reference/degraded/manual-edit-applier.md +93 -0
  87. package/skills/impeccable/reference/delight.md +70 -0
  88. package/skills/impeccable/reference/distill.md +111 -0
  89. package/skills/impeccable/reference/doctor.md +54 -0
  90. package/skills/impeccable/reference/document.md +416 -0
  91. package/skills/impeccable/reference/extract.md +69 -0
  92. package/skills/impeccable/reference/generate.md +101 -0
  93. package/skills/impeccable/reference/harden.md +362 -0
  94. package/skills/impeccable/reference/hooks.md +120 -0
  95. package/skills/impeccable/reference/init.md +133 -0
  96. package/skills/impeccable/reference/ios.md +51 -0
  97. package/skills/impeccable/reference/layout.md +84 -0
  98. package/skills/impeccable/reference/live-setup.md +104 -0
  99. package/skills/impeccable/reference/live.md +327 -0
  100. package/skills/impeccable/reference/mode-operate.md +21 -0
  101. package/skills/impeccable/reference/mode-persuade.md +19 -0
  102. package/skills/impeccable/reference/mode-read.md +21 -0
  103. package/skills/impeccable/reference/new-work.md +152 -0
  104. package/skills/impeccable/reference/onboard.md +235 -0
  105. package/skills/impeccable/reference/operate.md +61 -0
  106. package/skills/impeccable/reference/optimize.md +257 -0
  107. package/skills/impeccable/reference/overdrive.md +127 -0
  108. package/skills/impeccable/reference/polish.md +105 -0
  109. package/skills/impeccable/reference/quieter.md +99 -0
  110. package/skills/impeccable/reference/region-map.md +26 -0
  111. package/skills/impeccable/reference/routing.md +24 -0
  112. package/skills/impeccable/reference/shape.md +59 -0
  113. package/skills/impeccable/reference/typeset.md +90 -0
  114. package/skills/impeccable/reference/visualize.md +47 -0
  115. package/skills/impeccable/scripts/VERSION +1 -0
  116. package/skills/impeccable/scripts/command-metadata.json +98 -0
  117. package/skills/impeccable/scripts/data/font-index-failures.json +121 -0
  118. package/skills/impeccable/scripts/data/font-index.json +1 -0
  119. package/skills/impeccable/scripts/engine-checksums.txt +9 -0
  120. package/skills/impeccable/scripts/impeccable +291 -0
  121. package/skills/impeccable/scripts/impeccable.cmd +337 -0
  122. package/skills/impeccable/scripts/live-browser-dom.js +320 -0
  123. package/skills/impeccable/scripts/live-browser-ignores.js +246 -0
  124. package/skills/impeccable/scripts/live-browser-session.js +183 -0
  125. package/skills/impeccable/scripts/live-browser.js +13872 -0
  126. package/skills/impeccable/scripts/modern-screenshot.umd.js +14 -0
  127. package/skills/pdf/LICENSE.txt +202 -0
  128. package/skills/pdf/SKILL.md +314 -0
  129. package/skills/pdf/forms.md +294 -0
  130. package/skills/pdf/reference.md +612 -0
  131. package/skills/pdf/scripts/check_bounding_boxes.py +65 -0
  132. package/skills/pdf/scripts/check_fillable_fields.py +11 -0
  133. package/skills/pdf/scripts/convert_pdf_to_images.py +33 -0
  134. package/skills/pdf/scripts/create_validation_image.py +37 -0
  135. package/skills/pdf/scripts/extract_form_field_info.py +122 -0
  136. package/skills/pdf/scripts/extract_form_structure.py +115 -0
  137. package/skills/pdf/scripts/fill_fillable_fields.py +98 -0
  138. package/skills/pdf/scripts/fill_pdf_form_with_annotations.py +107 -0
  139. package/skills/xlsx/LICENSE.txt +202 -0
  140. package/skills/xlsx/SKILL.md +99 -0
  141. package/skills/xlsx/scripts/office/helpers/__init__.py +111 -0
  142. package/skills/xlsx/scripts/office/helpers/pptx_chart.py +170 -0
  143. package/skills/xlsx/scripts/office/helpers/pptx_slide.py +60 -0
  144. package/skills/xlsx/scripts/office/helpers/pptx_theme.py +114 -0
  145. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chart.xsd +1499 -0
  146. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-chartDrawing.xsd +146 -0
  147. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-diagram.xsd +1085 -0
  148. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-lockedCanvas.xsd +11 -0
  149. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-main.xsd +3081 -0
  150. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-picture.xsd +23 -0
  151. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-spreadsheetDrawing.xsd +185 -0
  152. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/dml-wordprocessingDrawing.xsd +287 -0
  153. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/pml.xsd +1676 -0
  154. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-additionalCharacteristics.xsd +28 -0
  155. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-bibliography.xsd +144 -0
  156. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-commonSimpleTypes.xsd +174 -0
  157. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlDataProperties.xsd +25 -0
  158. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-customXmlSchemaProperties.xsd +18 -0
  159. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesCustom.xsd +59 -0
  160. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesExtended.xsd +56 -0
  161. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesVariantTypes.xsd +195 -0
  162. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-math.xsd +582 -0
  163. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/shared-relationshipReference.xsd +25 -0
  164. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/sml.xsd +4439 -0
  165. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-main.xsd +570 -0
  166. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-officeDrawing.xsd +509 -0
  167. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-presentationDrawing.xsd +12 -0
  168. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-spreadsheetDrawing.xsd +108 -0
  169. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/vml-wordprocessingDrawing.xsd +96 -0
  170. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/wml.xsd +3646 -0
  171. package/skills/xlsx/scripts/office/schemas/ISO-IEC29500-4_2016/xml.xsd +116 -0
  172. package/skills/xlsx/scripts/office/schemas/ecma/fouth-edition/opc-contentTypes.xsd +42 -0
  173. package/skills/xlsx/scripts/office/schemas/ecma/fouth-edition/opc-coreProperties.xsd +50 -0
  174. package/skills/xlsx/scripts/office/schemas/ecma/fouth-edition/opc-digSig.xsd +49 -0
  175. package/skills/xlsx/scripts/office/schemas/ecma/fouth-edition/opc-relationships.xsd +33 -0
  176. package/skills/xlsx/scripts/office/schemas/mce/mc.xsd +75 -0
  177. package/skills/xlsx/scripts/office/schemas/microsoft/wml-2010.xsd +560 -0
  178. package/skills/xlsx/scripts/office/schemas/microsoft/wml-2012.xsd +67 -0
  179. package/skills/xlsx/scripts/office/schemas/microsoft/wml-2018.xsd +14 -0
  180. package/skills/xlsx/scripts/office/schemas/microsoft/wml-cex-2018.xsd +20 -0
  181. package/skills/xlsx/scripts/office/schemas/microsoft/wml-cid-2016.xsd +13 -0
  182. package/skills/xlsx/scripts/office/schemas/microsoft/wml-sdtdatahash-2020.xsd +4 -0
  183. package/skills/xlsx/scripts/office/schemas/microsoft/wml-symex-2015.xsd +8 -0
  184. package/skills/xlsx/scripts/office/soffice.py +192 -0
  185. package/skills/xlsx/scripts/office/validate.py +173 -0
  186. package/skills/xlsx/scripts/office/validators/__init__.py +15 -0
  187. package/skills/xlsx/scripts/office/validators/base.py +875 -0
  188. package/skills/xlsx/scripts/office/validators/docx.py +466 -0
  189. package/skills/xlsx/scripts/office/validators/pptx.py +441 -0
  190. package/skills/xlsx/scripts/office/validators/redlining.py +299 -0
  191. package/skills/xlsx/scripts/recalc.py +308 -0
  192. package/skills-manifest.json +36 -0
@@ -0,0 +1,368 @@
1
+ """Add comments to a DOCX document.
2
+
3
+ Accepts either an unpacked directory OR a .docx/.dotx file directly.
4
+
5
+ Usage:
6
+ # Against an unpacked directory (writes satellite files in place)
7
+ python comment.py unpacked/ "Comment text"
8
+ python comment.py unpacked/ "Reply text" --parent 0
9
+
10
+ # Against a .docx directly (extracts, writes satellite files, rezips)
11
+ python comment.py contract.docx "This cap is too low" -o annotated.docx
12
+ python comment.py contract.docx "Comment" --id 5 # explicit ID
13
+
14
+ The comment ID is auto-assigned (max existing + 1) unless --id is given.
15
+ Plain text is XML-escaped automatically; if you pass already-escaped text
16
+ (e.g. &, ’) use --raw to skip escaping.
17
+
18
+ After running, add markers to word/document.xml so the comment is visible:
19
+ <w:commentRangeStart w:id="N"/>
20
+ ... commented content ...
21
+ <w:commentRangeEnd w:id="N"/>
22
+ <w:r><w:rPr><w:rStyle w:val="CommentReference"/></w:rPr><w:commentReference w:id="N"/></w:r>
23
+ """
24
+
25
+ import argparse
26
+ import random
27
+ import shutil
28
+ import sys
29
+ import tempfile
30
+ import zipfile
31
+ from datetime import datetime, timezone
32
+ from pathlib import Path
33
+
34
+ import defusedxml.minidom
35
+ from xml.parsers.expat import ExpatError
36
+ from xml.sax.saxutils import escape as xml_escape
37
+
38
+ from office.helpers import opc_target, rezip as _rezip, safe_extract as _safe_extract
39
+
40
+ TEMPLATE_DIR = Path(__file__).parent / "templates"
41
+ NS = {
42
+ "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
43
+ "w14": "http://schemas.microsoft.com/office/word/2010/wordml",
44
+ "w15": "http://schemas.microsoft.com/office/word/2012/wordml",
45
+ "w16cid": "http://schemas.microsoft.com/office/word/2016/wordml/cid",
46
+ "w16cex": "http://schemas.microsoft.com/office/word/2018/wordml/cex",
47
+ }
48
+
49
+ COMMENT_XML = """\
50
+ <w:comment w:id="{id}" w:author="{author}" w:date="{date}" w:initials="{initials}">
51
+ <w:p w14:paraId="{para_id}" w14:textId="77777777">
52
+ <w:r>
53
+ <w:rPr><w:rStyle w:val="CommentReference"/></w:rPr>
54
+ <w:annotationRef/>
55
+ </w:r>
56
+ <w:r>
57
+ <w:rPr>
58
+ <w:color w:val="000000"/>
59
+ <w:sz w:val="20"/>
60
+ <w:szCs w:val="20"/>
61
+ </w:rPr>
62
+ <w:t xml:space="preserve">{text}</w:t>
63
+ </w:r>
64
+ </w:p>
65
+ </w:comment>"""
66
+
67
+ COMMENT_MARKER_TEMPLATE = """
68
+ Add to word/document.xml (markers must be direct children of w:p, never inside w:r):
69
+ <w:commentRangeStart w:id="{cid}"/>
70
+ <w:r>...</w:r>
71
+ <w:commentRangeEnd w:id="{cid}"/>
72
+ <w:r><w:rPr><w:rStyle w:val="CommentReference"/></w:rPr><w:commentReference w:id="{cid}"/></w:r>"""
73
+
74
+ REPLY_MARKER_TEMPLATE = """
75
+ Nest markers inside parent {pid}'s markers (direct children of w:p, never inside w:r):
76
+ <w:commentRangeStart w:id="{pid}"/><w:commentRangeStart w:id="{cid}"/>
77
+ <w:r>...</w:r>
78
+ <w:commentRangeEnd w:id="{cid}"/><w:commentRangeEnd w:id="{pid}"/>
79
+ <w:r><w:rPr><w:rStyle w:val="CommentReference"/></w:rPr><w:commentReference w:id="{pid}"/></w:r>
80
+ <w:r><w:rPr><w:rStyle w:val="CommentReference"/></w:rPr><w:commentReference w:id="{cid}"/></w:r>"""
81
+
82
+ SMART_QUOTE_ENTITIES = {
83
+ "“": "&#x201C;",
84
+ "”": "&#x201D;",
85
+ "‘": "&#x2018;",
86
+ "’": "&#x2019;",
87
+ }
88
+
89
+
90
+ def _generate_hex_id() -> str:
91
+ return f"{random.randint(0, 0x7FFFFFFE):08X}"
92
+
93
+
94
+ def _encode_smart_quotes(text: str) -> str:
95
+ for char, entity in SMART_QUOTE_ENTITIES.items():
96
+ text = text.replace(char, entity)
97
+ return text
98
+
99
+
100
+ def _append_xml(xml_path: Path, root_tag: str, content: str) -> None:
101
+ dom = defusedxml.minidom.parseString(xml_path.read_text(encoding="utf-8"))
102
+ root = dom.getElementsByTagName(root_tag)[0]
103
+ ns_attrs = " ".join(f'xmlns:{k}="{v}"' for k, v in NS.items())
104
+ wrapper_dom = defusedxml.minidom.parseString(f"<root {ns_attrs}>{content}</root>")
105
+ for child in wrapper_dom.documentElement.childNodes:
106
+ if child.nodeType == child.ELEMENT_NODE:
107
+ root.appendChild(dom.importNode(child, True))
108
+ output = _encode_smart_quotes(dom.toxml(encoding="UTF-8").decode("utf-8"))
109
+ xml_path.write_text(output, encoding="utf-8")
110
+
111
+
112
+ def _find_para_id(comments_path: Path, comment_id: int) -> str | None:
113
+ dom = defusedxml.minidom.parseString(comments_path.read_text(encoding="utf-8"))
114
+ for c in dom.getElementsByTagName("w:comment"):
115
+ if c.getAttribute("w:id") == str(comment_id):
116
+ for p in c.getElementsByTagName("w:p"):
117
+ if pid := p.getAttribute("w14:paraId"):
118
+ return pid
119
+ return None
120
+
121
+
122
+ def _next_comment_id(comments_path: Path) -> int:
123
+ if not comments_path.exists():
124
+ return 0
125
+ dom = defusedxml.minidom.parseString(comments_path.read_text(encoding="utf-8"))
126
+ ids = []
127
+ for c in dom.getElementsByTagName("w:comment"):
128
+ try:
129
+ ids.append(int(c.getAttribute("w:id")))
130
+ except ValueError:
131
+ pass
132
+ return (max(ids) + 1) if ids else 0
133
+
134
+
135
+ def _get_next_rid(rels_path: Path) -> int:
136
+ dom = defusedxml.minidom.parseString(rels_path.read_text(encoding="utf-8"))
137
+ max_rid = 0
138
+ for rel in dom.getElementsByTagName("Relationship"):
139
+ rid = rel.getAttribute("Id")
140
+ if rid and rid.startswith("rId"):
141
+ try:
142
+ max_rid = max(max_rid, int(rid[3:]))
143
+ except ValueError:
144
+ pass
145
+ return max_rid + 1
146
+
147
+
148
+ def _has_relationship(rels_path: Path, target: str) -> bool:
149
+ dom = defusedxml.minidom.parseString(rels_path.read_text(encoding="utf-8"))
150
+ return any(
151
+ rel.getAttribute("Target") == target
152
+ for rel in dom.getElementsByTagName("Relationship")
153
+ )
154
+
155
+
156
+ def _has_content_type(ct_path: Path, part_name: str) -> bool:
157
+ dom = defusedxml.minidom.parseString(ct_path.read_text(encoding="utf-8"))
158
+ return any(
159
+ o.getAttribute("PartName") == part_name
160
+ for o in dom.getElementsByTagName("Override")
161
+ )
162
+
163
+
164
+ _COMMENT_RELS = [
165
+ ("http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments", "comments.xml"),
166
+ ("http://schemas.microsoft.com/office/2011/relationships/commentsExtended", "commentsExtended.xml"),
167
+ ("http://schemas.microsoft.com/office/2016/09/relationships/commentsIds", "commentsIds.xml"),
168
+ ("http://schemas.microsoft.com/office/2018/08/relationships/commentsExtensible", "commentsExtensible.xml"),
169
+ ]
170
+ _COMMENT_OVERRIDES = [
171
+ ("/word/comments.xml", "application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml"),
172
+ ("/word/commentsExtended.xml", "application/vnd.openxmlformats-officedocument.wordprocessingml.commentsExtended+xml"),
173
+ ("/word/commentsIds.xml", "application/vnd.openxmlformats-officedocument.wordprocessingml.commentsIds+xml"),
174
+ ("/word/commentsExtensible.xml", "application/vnd.openxmlformats-officedocument.wordprocessingml.commentsExtensible+xml"),
175
+ ]
176
+
177
+
178
+ def _ensure_comment_relationships(unpacked_dir: Path) -> None:
179
+ rels_path = unpacked_dir / "word" / "_rels" / "document.xml.rels"
180
+ if not rels_path.exists():
181
+ return
182
+ dom = defusedxml.minidom.parseString(rels_path.read_text(encoding="utf-8"))
183
+ root = dom.documentElement
184
+ comment_types = {rel_type for rel_type, _ in _COMMENT_RELS}
185
+ existing = set()
186
+ for rel in dom.getElementsByTagName("Relationship"):
187
+ if rel.getAttribute("Type") not in comment_types:
188
+ continue
189
+ part = opc_target(
190
+ rel.getAttribute("Target"),
191
+ "word/document.xml",
192
+ rel.getAttribute("TargetMode"),
193
+ )
194
+ if part is not None:
195
+ existing.add(part)
196
+ next_rid = _get_next_rid(rels_path)
197
+ changed = False
198
+ for rel_type, target in _COMMENT_RELS:
199
+ if opc_target(target, "word/document.xml") in existing:
200
+ continue
201
+ rel = dom.createElement("Relationship")
202
+ rel.setAttribute("Id", f"rId{next_rid}")
203
+ rel.setAttribute("Type", rel_type)
204
+ rel.setAttribute("Target", target)
205
+ root.appendChild(rel)
206
+ next_rid += 1
207
+ changed = True
208
+ if changed:
209
+ rels_path.write_bytes(dom.toxml(encoding="UTF-8"))
210
+
211
+
212
+ def _ensure_comment_content_types(unpacked_dir: Path) -> None:
213
+ ct_path = unpacked_dir / "[Content_Types].xml"
214
+ if not ct_path.exists():
215
+ return
216
+ dom = defusedxml.minidom.parseString(ct_path.read_text(encoding="utf-8"))
217
+ root = dom.documentElement
218
+ existing = {
219
+ o.getAttribute("PartName")
220
+ for o in dom.getElementsByTagName("Override")
221
+ }
222
+ changed = False
223
+ for part_name, content_type in _COMMENT_OVERRIDES:
224
+ if part_name in existing:
225
+ continue
226
+ override = dom.createElement("Override")
227
+ override.setAttribute("PartName", part_name)
228
+ override.setAttribute("ContentType", content_type)
229
+ root.appendChild(override)
230
+ changed = True
231
+ if changed:
232
+ ct_path.write_bytes(dom.toxml(encoding="UTF-8"))
233
+
234
+
235
+ def add_comment(
236
+ unpacked_dir: Path | str,
237
+ text: str,
238
+ comment_id: int | None = None,
239
+ author: str = "Claude",
240
+ initials: str = "C",
241
+ parent_id: int | None = None,
242
+ raw: bool = False,
243
+ ) -> tuple[int, str, str]:
244
+ unpacked_dir = Path(unpacked_dir)
245
+ if not raw:
246
+ text = xml_escape(text)
247
+ author = xml_escape(author, {'"': "&quot;"})
248
+ initials = xml_escape(initials, {'"': "&quot;"})
249
+ word = unpacked_dir / "word"
250
+ if not word.exists():
251
+ raise FileNotFoundError(f"{word} not found (not an unpacked .docx?)")
252
+
253
+ comments = word / "comments.xml"
254
+ if comment_id is None:
255
+ comment_id = _next_comment_id(comments)
256
+
257
+ parent_para = None
258
+ if parent_id is not None:
259
+ parent_para = _find_para_id(comments, parent_id) if comments.exists() else None
260
+ if not parent_para:
261
+ raise ValueError(f"parent comment {parent_id} not found")
262
+
263
+ para_id, durable_id = _generate_hex_id(), _generate_hex_id()
264
+ ts = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
265
+
266
+ if not comments.exists():
267
+ shutil.copy(TEMPLATE_DIR / "comments.xml", comments)
268
+ _ensure_comment_relationships(unpacked_dir)
269
+ _ensure_comment_content_types(unpacked_dir)
270
+ _append_xml(
271
+ comments,
272
+ "w:comments",
273
+ COMMENT_XML.format(
274
+ id=comment_id, author=author, date=ts, initials=initials,
275
+ para_id=para_id, text=text,
276
+ ),
277
+ )
278
+
279
+ ext = word / "commentsExtended.xml"
280
+ if not ext.exists():
281
+ shutil.copy(TEMPLATE_DIR / "commentsExtended.xml", ext)
282
+ if parent_para is not None:
283
+ _append_xml(
284
+ ext, "w15:commentsEx",
285
+ f'<w15:commentEx w15:paraId="{para_id}" w15:paraIdParent="{parent_para}" w15:done="0"/>',
286
+ )
287
+ else:
288
+ _append_xml(
289
+ ext, "w15:commentsEx",
290
+ f'<w15:commentEx w15:paraId="{para_id}" w15:done="0"/>',
291
+ )
292
+
293
+ ids = word / "commentsIds.xml"
294
+ if not ids.exists():
295
+ shutil.copy(TEMPLATE_DIR / "commentsIds.xml", ids)
296
+ _append_xml(
297
+ ids, "w16cid:commentsIds",
298
+ f'<w16cid:commentId w16cid:paraId="{para_id}" w16cid:durableId="{durable_id}"/>',
299
+ )
300
+
301
+ extensible = word / "commentsExtensible.xml"
302
+ if not extensible.exists():
303
+ shutil.copy(TEMPLATE_DIR / "commentsExtensible.xml", extensible)
304
+ _append_xml(
305
+ extensible, "w16cex:commentsExtensible",
306
+ f'<w16cex:commentExtensible w16cex:durableId="{durable_id}" w16cex:dateUtc="{ts}"/>',
307
+ )
308
+
309
+ action = "reply" if parent_id is not None else "comment"
310
+ return comment_id, para_id, f"Added {action} id={comment_id} (paraId={para_id})"
311
+
312
+
313
+ def main() -> None:
314
+ p = argparse.ArgumentParser(description="Add a comment to a DOCX (directory or .docx file).")
315
+ p.add_argument("input", help="Unpacked DOCX directory OR a .docx/.dotx file")
316
+ p.add_argument("text", help="Comment text (plain text; XML-escaped automatically)")
317
+ p.add_argument("--raw", action="store_true",
318
+ help="Treat text as pre-escaped XML (skip automatic escaping)")
319
+ p.add_argument("--id", type=int, dest="comment_id",
320
+ help="Comment ID (default: auto-assign as max existing + 1)")
321
+ p.add_argument("--author", default="Claude", help="Author name")
322
+ p.add_argument("--initials", default="C", help="Author initials")
323
+ p.add_argument("--parent", type=int, help="Parent comment ID (makes this a reply)")
324
+ p.add_argument("-o", "--output",
325
+ help="Output .docx path (only used when input is a .docx; default: overwrite input)")
326
+ args = p.parse_args()
327
+
328
+ src = Path(args.input)
329
+
330
+ try:
331
+ if src.is_dir():
332
+ if args.output:
333
+ print("Warning: --output ignored for directory input", file=sys.stderr)
334
+ cid, _, msg = add_comment(
335
+ src, args.text, comment_id=args.comment_id,
336
+ author=args.author, initials=args.initials,
337
+ parent_id=args.parent, raw=args.raw,
338
+ )
339
+ print(msg)
340
+ elif src.is_file() and src.suffix.lower() in (".docx", ".dotx"):
341
+ out = Path(args.output) if args.output else src
342
+ with tempfile.TemporaryDirectory() as tmp:
343
+ tmp_path = Path(tmp)
344
+ with zipfile.ZipFile(src) as zf:
345
+ _safe_extract(zf, tmp_path)
346
+ cid, _, msg = add_comment(
347
+ tmp_path, args.text, comment_id=args.comment_id,
348
+ author=args.author, initials=args.initials,
349
+ parent_id=args.parent, raw=args.raw,
350
+ )
351
+ _rezip(tmp_path, out)
352
+ print(msg)
353
+ print(f"Wrote {out} (comment defined; add markers to word/document.xml to make it visible)")
354
+ else:
355
+ print(f"Error: {src} is neither a directory nor a .docx/.dotx file", file=sys.stderr)
356
+ sys.exit(1)
357
+ except (FileNotFoundError, ValueError, zipfile.BadZipFile, ExpatError) as e:
358
+ print(f"Error: {e}", file=sys.stderr)
359
+ sys.exit(1)
360
+
361
+ if args.parent is not None:
362
+ print(REPLY_MARKER_TEMPLATE.format(pid=args.parent, cid=cid))
363
+ else:
364
+ print(COMMENT_MARKER_TEMPLATE.format(cid=cid))
365
+
366
+
367
+ if __name__ == "__main__":
368
+ main()
@@ -0,0 +1,310 @@
1
+ """Merge adjacent identically-formatted runs in a DOCX.
2
+
3
+ Word fragments paragraph text across many <w:r> elements (revision ids,
4
+ spell-check markers, editing history), which makes find-and-replace on
5
+ word/document.xml unreliable — the string you're looking for is split
6
+ across runs. This coalesces adjacent runs whose formatting (<w:rPr>) is
7
+ identical, strips rsid attributes and proofErr markers, and consolidates the
8
+ text elements — <w:t>, and <w:delText> for text inside a tracked deletion.
9
+
10
+ Rendering is unchanged. The text you search is what Word draws, which is not
11
+ always the bytes in the file: an element without xml:space="preserve" has its
12
+ edge whitespace trimmed before it reaches the page, so `<w:t>Hello </w:t>`
13
+ followed by `<w:t>world</w:t>` reads "Helloworld" and merges to exactly that.
14
+
15
+ Runs in two different <w:ins>/<w:del> wrappers are never merged: that would
16
+ rewrite tracked-change structure, collapsing separate revisions into one.
17
+
18
+ Only word/document.xml is processed (not headers, footers, or footnotes).
19
+
20
+ Usage:
21
+ python merge_runs.py unpacked/ # after unzip, before editing
22
+ python merge_runs.py document.docx # rewrite in place
23
+ python merge_runs.py document.docx -o out.docx
24
+ """
25
+
26
+
27
+ import argparse
28
+ import sys
29
+ import tempfile
30
+ import zipfile
31
+ from pathlib import Path
32
+
33
+ import defusedxml.minidom
34
+
35
+ from office.helpers import XML_SPACE, rendered_text, rezip, safe_extract
36
+
37
+ WORDML_NS = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
38
+
39
+
40
+ def merge_runs(input_dir: str) -> tuple[int, str]:
41
+ doc_xml = Path(input_dir) / "word" / "document.xml"
42
+
43
+ if not doc_xml.exists():
44
+ return 0, f"Error: {doc_xml} not found"
45
+
46
+ try:
47
+ dom = defusedxml.minidom.parseString(doc_xml.read_text(encoding="utf-8"))
48
+ root = dom.documentElement
49
+ run_names = _run_tag_names(root)
50
+
51
+ _remove_elements(root, "proofErr")
52
+
53
+ runs = _find_runs(root, run_names)
54
+ _strip_rsid_attrs(runs)
55
+
56
+ merge_count = 0
57
+ for container in {run.parentNode for run in runs}:
58
+ merge_count += _merge_runs_in(container, run_names)
59
+
60
+ doc_xml.write_bytes(dom.toxml(encoding="UTF-8"))
61
+ return merge_count, f"Merged {merge_count} runs"
62
+
63
+ except Exception as e:
64
+ return 0, f"Error: {e}"
65
+
66
+
67
+
68
+
69
+ def _is_element(node, tag: str) -> bool:
70
+ name = node.localName or node.tagName
71
+ return name == tag or name.endswith(f":{tag}")
72
+
73
+
74
+ def _run_tag_names(root) -> set[str]:
75
+ names = set()
76
+ for attr in root.attributes.values():
77
+ if attr.value == WORDML_NS:
78
+ if attr.name == "xmlns":
79
+ names.add("r")
80
+ elif attr.name.startswith("xmlns:"):
81
+ names.add(attr.name.split(":", 1)[1] + ":r")
82
+ return names or {"w:r", "r"}
83
+
84
+
85
+ def _find_elements(root, tag: str) -> list:
86
+ results = []
87
+
88
+ def traverse(node):
89
+ if node.nodeType == node.ELEMENT_NODE:
90
+ if _is_element(node, tag):
91
+ results.append(node)
92
+ for child in node.childNodes:
93
+ traverse(child)
94
+
95
+ traverse(root)
96
+ return results
97
+
98
+
99
+ def _find_runs(root, run_names: set[str]) -> list:
100
+ return [e for e in _find_elements(root, "r") if _is_run(e, run_names)]
101
+
102
+
103
+ def _get_child(parent, tag: str):
104
+ return next(iter(_get_children(parent, tag)), None)
105
+
106
+
107
+ def _get_children(parent, tag: str) -> list:
108
+ return [
109
+ child
110
+ for child in parent.childNodes
111
+ if child.nodeType == child.ELEMENT_NODE and _is_element(child, tag)
112
+ ]
113
+
114
+
115
+ def _is_adjacent(elem1, elem2) -> bool:
116
+ node = elem1.nextSibling
117
+ while node:
118
+ if node == elem2:
119
+ return True
120
+ if node.nodeType == node.ELEMENT_NODE:
121
+ return False
122
+ if node.nodeType == node.TEXT_NODE and node.data.strip(XML_SPACE):
123
+ return False
124
+ node = node.nextSibling
125
+ return False
126
+
127
+
128
+
129
+
130
+ def _remove_elements(root, tag: str):
131
+ for elem in _find_elements(root, tag):
132
+ if elem.parentNode:
133
+ elem.parentNode.removeChild(elem)
134
+
135
+
136
+ def _strip_rsid_attrs(runs: list):
137
+ for run in runs:
138
+ for attr in list(run.attributes.values()):
139
+ if "rsid" in attr.name.lower():
140
+ run.removeAttribute(attr.name)
141
+
142
+
143
+
144
+
145
+ def _merge_runs_in(container, run_names: set[str]) -> int:
146
+ merge_count = 0
147
+ run = _first_child_run(container, run_names)
148
+
149
+ while run:
150
+ while True:
151
+ next_elem = _next_element_sibling(run)
152
+ if next_elem and _is_run(next_elem, run_names) and _can_merge(run, next_elem):
153
+ _merge_run_content(run, next_elem)
154
+ container.removeChild(next_elem)
155
+ merge_count += 1
156
+ else:
157
+ break
158
+
159
+ _consolidate_text(run)
160
+ run = _next_sibling_run(run, run_names)
161
+
162
+ return merge_count
163
+
164
+
165
+ def _first_child_run(container, run_names: set[str]):
166
+ for child in container.childNodes:
167
+ if child.nodeType == child.ELEMENT_NODE and _is_run(child, run_names):
168
+ return child
169
+ return None
170
+
171
+
172
+ def _next_element_sibling(node):
173
+ sibling = node.nextSibling
174
+ while sibling:
175
+ if sibling.nodeType == sibling.ELEMENT_NODE:
176
+ return sibling
177
+ sibling = sibling.nextSibling
178
+ return None
179
+
180
+
181
+ def _next_sibling_run(node, run_names: set[str]):
182
+ sibling = node.nextSibling
183
+ while sibling:
184
+ if sibling.nodeType == sibling.ELEMENT_NODE:
185
+ if _is_run(sibling, run_names):
186
+ return sibling
187
+ sibling = sibling.nextSibling
188
+ return None
189
+
190
+
191
+ def _is_run(node, run_names: set[str]) -> bool:
192
+ return node.tagName in run_names
193
+
194
+
195
+ def _can_merge(run1, run2) -> bool:
196
+ rpr1 = _get_child(run1, "rPr")
197
+ rpr2 = _get_child(run2, "rPr")
198
+
199
+ if (rpr1 is None) != (rpr2 is None):
200
+ return False
201
+ if rpr1 is None:
202
+ return True
203
+ return rpr1.toxml() == rpr2.toxml()
204
+
205
+
206
+ def _merge_run_content(target, source):
207
+ for child in list(source.childNodes):
208
+ if child.nodeType == child.ELEMENT_NODE:
209
+ name = child.localName or child.tagName
210
+ if name != "rPr" and not name.endswith(":rPr"):
211
+ target.appendChild(child)
212
+
213
+
214
+ def _element_text(elem) -> str:
215
+ return "".join(
216
+ child.data
217
+ for child in elem.childNodes
218
+ if child.nodeType in (child.TEXT_NODE, child.CDATA_SECTION_NODE)
219
+ )
220
+
221
+
222
+ def _has_preserve(elem) -> bool:
223
+ return elem.getAttribute("xml:space") == "preserve"
224
+
225
+
226
+ def _rendered_text(elem) -> str:
227
+ return rendered_text(_element_text(elem), _has_preserve(elem))
228
+
229
+
230
+ def _consolidate_text(run):
231
+ for tag in ("t", "delText"):
232
+ _consolidate_text_elements(run, tag)
233
+
234
+
235
+ def _consolidate_text_elements(run, tag: str):
236
+ t_elements = _get_children(run, tag)
237
+
238
+ for i in range(len(t_elements) - 1, 0, -1):
239
+ curr, prev = t_elements[i], t_elements[i - 1]
240
+
241
+ if _is_adjacent(prev, curr):
242
+ merged = _rendered_text(prev) + _rendered_text(curr)
243
+ had_preserve = _has_preserve(prev) or _has_preserve(curr)
244
+
245
+ new_text = run.ownerDocument.createTextNode(merged)
246
+ for node in list(prev.childNodes):
247
+ if node.nodeType in (node.TEXT_NODE, node.CDATA_SECTION_NODE):
248
+ prev.removeChild(node)
249
+ else:
250
+ run.insertBefore(node, curr)
251
+ prev.appendChild(new_text)
252
+ for node in list(curr.childNodes):
253
+ if node.nodeType not in (node.TEXT_NODE, node.CDATA_SECTION_NODE):
254
+ run.insertBefore(node, curr)
255
+
256
+ if merged != merged.strip(XML_SPACE) or had_preserve:
257
+ prev.setAttribute("xml:space", "preserve")
258
+ elif prev.hasAttribute("xml:space"):
259
+ prev.removeAttribute("xml:space")
260
+
261
+ run.removeChild(curr)
262
+
263
+
264
+
265
+
266
+ def _merge_or_die(path: Path) -> str:
267
+ _, msg = merge_runs(str(path))
268
+ if msg.startswith("Error"):
269
+ print(msg, file=sys.stderr)
270
+ sys.exit(1)
271
+ return msg
272
+
273
+
274
+ def main() -> None:
275
+ p = argparse.ArgumentParser(
276
+ description="Merge adjacent identically-formatted runs in a DOCX (directory or .docx file)."
277
+ )
278
+ p.add_argument("input", help="Unpacked DOCX directory OR a .docx/.dotx file")
279
+ p.add_argument(
280
+ "-o", "--output",
281
+ help="Output .docx path (only valid when input is a .docx; default: overwrite input)",
282
+ )
283
+ args = p.parse_args()
284
+
285
+ src = Path(args.input)
286
+
287
+ try:
288
+ if src.is_dir():
289
+ if args.output:
290
+ p.error("--output is only valid for .docx input; directory input is modified in place")
291
+ print(_merge_or_die(src))
292
+ elif src.is_file() and src.suffix.lower() in (".docx", ".dotx"):
293
+ out = Path(args.output) if args.output else src
294
+ with tempfile.TemporaryDirectory() as tmp:
295
+ tmp_path = Path(tmp)
296
+ with zipfile.ZipFile(src) as zf:
297
+ safe_extract(zf, tmp_path)
298
+ msg = _merge_or_die(tmp_path)
299
+ rezip(tmp_path, out)
300
+ print(f"{msg}; wrote {out}")
301
+ else:
302
+ print(f"Error: {src} is neither a directory nor a .docx/.dotx file", file=sys.stderr)
303
+ sys.exit(1)
304
+ except (OSError, ValueError, zipfile.BadZipFile) as e:
305
+ print(f"Error: {e}", file=sys.stderr)
306
+ sys.exit(1)
307
+
308
+
309
+ if __name__ == "__main__":
310
+ main()