dsh-office-kit 0.2.0 → 0.3.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.
- package/CAPABILITY.md +147 -68
- package/README.md +3 -1
- package/assets/skills/excel-live-control/SKILL.md +234 -0
- package/assets/skills/excel-live-control/container_tools/live_excel.py +1589 -0
- package/assets/skills/excel-live-control/domain_guidance/financial_models.md +86 -0
- package/assets/skills/excel-live-control/domain_guidance/healthcare.md +58 -0
- package/assets/skills/excel-live-control/domain_guidance/marketing_advertising.md +53 -0
- package/assets/skills/excel-live-control/domain_guidance/scientific_research.md +53 -0
- package/assets/skills/excel-live-control/references/office-automation.md +207 -0
- package/assets/skills/excel-live-control/references/read_only_qna.md +59 -0
- package/assets/skills/excel-live-control/style_guidelines.md +87 -0
- package/assets/skills/excel-live-control/workflows/create_workflows.md +78 -0
- package/assets/skills/excel-live-control/workflows/edit_workflows.md +72 -0
- package/assets/skills/office-documents/SKILL.md +90 -77
- package/assets/skills/office-documents/examples/end_to_end_smoke_test.md +86 -0
- package/assets/skills/office-documents/ooxml/comments.md +40 -0
- package/assets/skills/office-documents/ooxml/hyperlinks_and_fields.md +66 -0
- package/assets/skills/office-documents/ooxml/rels_and_content_types.md +62 -0
- package/assets/skills/office-documents/ooxml/tracked_changes.md +53 -0
- package/assets/skills/office-documents/references/docx-structure.md +83 -0
- package/assets/skills/office-documents/references/template-elicitation.md +49 -0
- package/assets/skills/office-documents/references/writing-quality.md +49 -0
- package/assets/skills/office-documents/render_docx.py +92 -0
- package/assets/skills/office-documents/scripts/a11y_audit.py +194 -0
- package/assets/skills/office-documents/scripts/accept_tracked_changes.py +151 -0
- package/assets/skills/office-documents/scripts/add_tracked_replacements.py +148 -0
- package/assets/skills/office-documents/scripts/append_blocks.py +108 -0
- package/assets/skills/office-documents/scripts/apply_template_styles.py +104 -0
- package/assets/skills/office-documents/scripts/captions_and_crossrefs.py +102 -0
- package/assets/skills/office-documents/scripts/comments_add.py +136 -0
- package/assets/skills/office-documents/scripts/comments_apply_patch.py +114 -0
- package/assets/skills/office-documents/scripts/comments_extract.py +85 -0
- package/assets/skills/office-documents/scripts/comments_strip.py +74 -0
- package/assets/skills/office-documents/scripts/content_controls.py +192 -0
- package/assets/skills/office-documents/scripts/core_properties.py +124 -0
- package/assets/skills/office-documents/scripts/docx_kit.py +587 -0
- package/assets/skills/office-documents/scripts/docx_ooxml_patch.py +225 -0
- package/assets/skills/office-documents/scripts/docx_table_to_csv.py +99 -0
- package/assets/skills/office-documents/scripts/fields_materialize.py +66 -0
- package/assets/skills/office-documents/scripts/fields_report.py +59 -0
- package/assets/skills/office-documents/scripts/flatten_ref_fields.py +62 -0
- package/assets/skills/office-documents/scripts/footnotes_report.py +77 -0
- package/assets/skills/office-documents/scripts/google_docs_title_sanitize.py +96 -0
- package/assets/skills/office-documents/scripts/heading_audit.py +81 -0
- package/assets/skills/office-documents/scripts/images_audit.py +80 -0
- package/assets/skills/office-documents/scripts/insert_note.py +126 -0
- package/assets/skills/office-documents/scripts/insert_ref_fields.py +90 -0
- package/assets/skills/office-documents/scripts/insert_toc.py +84 -0
- package/assets/skills/office-documents/scripts/internal_nav.py +146 -0
- package/assets/skills/office-documents/scripts/make_fixtures.py +169 -0
- package/assets/skills/office-documents/scripts/merge_docx_append.py +239 -0
- package/assets/skills/office-documents/scripts/privacy_scrub.py +131 -0
- package/assets/skills/office-documents/scripts/redact_docx.py +113 -0
- package/assets/skills/office-documents/scripts/render_and_diff.py +101 -0
- package/assets/skills/office-documents/scripts/replace_text.py +154 -0
- package/assets/skills/office-documents/scripts/section_audit.py +91 -0
- package/assets/skills/office-documents/scripts/set_protection.py +92 -0
- package/assets/skills/office-documents/scripts/style_lint.py +111 -0
- package/assets/skills/office-documents/scripts/style_normalize.py +103 -0
- package/assets/skills/office-documents/scripts/table_geometry.py +167 -0
- package/assets/skills/office-documents/scripts/watermark_add.py +48 -0
- package/assets/skills/office-documents/scripts/watermark_audit_remove.py +67 -0
- package/assets/skills/office-documents/scripts/xlsx_to_docx_table.py +117 -0
- package/assets/skills/office-documents/tasks/accessibility_a11y.md +45 -0
- package/assets/skills/office-documents/tasks/captions_crossrefs.md +49 -0
- package/assets/skills/office-documents/tasks/clean_tracked_changes.md +49 -0
- package/assets/skills/office-documents/tasks/comments_manage.md +59 -0
- package/assets/skills/office-documents/tasks/compare_diff.md +40 -0
- package/assets/skills/office-documents/tasks/create_edit.md +107 -0
- package/assets/skills/office-documents/tasks/equations.md +59 -0
- package/assets/skills/office-documents/tasks/fields_update.md +44 -0
- package/assets/skills/office-documents/tasks/fixtures_edge_cases.md +50 -0
- package/assets/skills/office-documents/tasks/footnotes_endnotes.md +38 -0
- package/assets/skills/office-documents/tasks/forms_content_controls.md +48 -0
- package/assets/skills/office-documents/tasks/headings_numbering.md +55 -0
- package/assets/skills/office-documents/tasks/images_figures.md +63 -0
- package/assets/skills/office-documents/tasks/multi_doc_merge.md +43 -0
- package/assets/skills/office-documents/tasks/navigation_internal_links.md +38 -0
- package/assets/skills/office-documents/tasks/privacy_scrub_metadata.md +45 -0
- package/assets/skills/office-documents/tasks/protection_restrict_editing.md +41 -0
- package/assets/skills/office-documents/tasks/read_review.md +50 -0
- package/assets/skills/office-documents/tasks/redaction_anonymization.md +42 -0
- package/assets/skills/office-documents/tasks/sections_layout.md +54 -0
- package/assets/skills/office-documents/tasks/style_lint_normalize.md +44 -0
- package/assets/skills/office-documents/tasks/tables_spreadsheets.md +64 -0
- package/assets/skills/office-documents/tasks/templates_style_packs.md +53 -0
- package/assets/skills/office-documents/tasks/toc_workflow.md +39 -0
- package/assets/skills/office-documents/tasks/verify_render.md +50 -0
- package/assets/skills/office-documents/tasks/watermarks_background.md +35 -0
- package/assets/skills/office-documents/troubleshooting/libreoffice_headless.md +47 -0
- package/assets/skills/office-documents/troubleshooting/run_splitting.md +36 -0
- package/assets/skills/office-pdf/SKILL.md +73 -56
- package/assets/skills/office-pdf/references/form-fields.md +56 -0
- package/assets/skills/office-pdf/references/geometry.md +44 -0
- package/assets/skills/office-pdf/references/layout-quality.md +41 -0
- package/assets/skills/office-pdf/references/limits.md +58 -0
- package/assets/skills/office-pdf/scripts/extract_text.py +172 -0
- package/assets/skills/office-pdf/scripts/forms.py +556 -0
- package/assets/skills/office-pdf/scripts/metadata.py +161 -0
- package/assets/skills/office-pdf/scripts/page_ops.py +198 -0
- package/assets/skills/office-pdf/scripts/redact_region.py +347 -0
- package/assets/skills/office-pdf/scripts/render_pages.py +207 -0
- package/assets/skills/office-pdf/scripts/split_pages.py +155 -0
- package/assets/skills/office-pdf/scripts/stamp_overlay.py +199 -0
- package/assets/skills/office-pdf/tasks/annotations.md +98 -0
- package/assets/skills/office-pdf/tasks/create.md +75 -0
- package/assets/skills/office-pdf/tasks/edit-pages.md +71 -0
- package/assets/skills/office-pdf/tasks/forms.md +67 -0
- package/assets/skills/office-pdf/tasks/read.md +62 -0
- package/assets/skills/office-pdf/tasks/redact.md +72 -0
- package/assets/skills/office-pdf/tasks/stamp.md +60 -0
- package/assets/skills/office-presentations/SKILL.md +141 -87
- package/assets/skills/office-presentations/container_tools/add_slides.py +136 -0
- package/assets/skills/office-presentations/container_tools/core_properties.py +120 -0
- package/assets/skills/office-presentations/container_tools/create_montage.py +126 -0
- package/assets/skills/office-presentations/container_tools/inspect_presentation_layout_geometry.py +285 -0
- package/assets/skills/office-presentations/container_tools/inspect_presentation_package_integrity.py +157 -0
- package/assets/skills/office-presentations/container_tools/native_chart_title_gate.py +144 -0
- package/assets/skills/office-presentations/container_tools/native_quantitative_chart_gate.py +199 -0
- package/assets/skills/office-presentations/container_tools/native_table_arithmetic_gate.py +212 -0
- package/assets/skills/office-presentations/container_tools/ooxml_helpers.py +345 -0
- package/assets/skills/office-presentations/container_tools/preview_render.py +375 -0
- package/assets/skills/office-presentations/container_tools/render_slides.py +139 -0
- package/assets/skills/office-presentations/container_tools/replace_text.py +168 -0
- package/assets/skills/office-presentations/container_tools/template_source_fidelity_gate.py +229 -0
- package/assets/skills/office-presentations/domain_guidance/finance.md +66 -0
- package/assets/skills/office-presentations/recipes/README.md +71 -0
- package/assets/skills/office-presentations/recipes/autolayout.md +88 -0
- package/assets/skills/office-presentations/recipes/connectors.md +83 -0
- package/assets/skills/office-presentations/recipes/images.md +80 -0
- package/assets/skills/office-presentations/recipes/master-and-layouts.md +127 -0
- package/assets/skills/office-presentations/recipes/native-charts.md +87 -0
- package/assets/skills/office-presentations/recipes/rich-text-runs.md +86 -0
- package/assets/skills/office-presentations/recipes/speaker-notes.md +65 -0
- package/assets/skills/office-presentations/recipes/tables.md +88 -0
- package/assets/skills/office-presentations/recipes/textbox-at-coordinate.md +71 -0
- package/assets/skills/office-presentations/recipes/theme-tokens.md +87 -0
- package/assets/skills/office-presentations/references/authoring-workflow.md +65 -0
- package/assets/skills/office-presentations/references/finalization.md +63 -0
- package/assets/skills/office-presentations/references/native-bullets.md +78 -0
- package/assets/skills/office-presentations/references/native-evidence.md +67 -0
- package/assets/skills/office-presentations/references/style-guidelines.md +66 -0
- package/assets/skills/office-presentations/references/templates.md +76 -0
- package/assets/skills/office-presentations/routing/google_slides.md +45 -0
- package/assets/skills/office-presentations/tasks/apply-annotations.md +83 -0
- package/assets/skills/office-presentations/tasks/charts-tables-notes.md +79 -0
- package/assets/skills/office-presentations/tasks/create-deck.md +106 -0
- package/assets/skills/office-presentations/tasks/edit-deck.md +93 -0
- package/assets/skills/office-presentations/tasks/inspect-and-verify.md +107 -0
- package/assets/skills/office-presentations/tasks/template-following.md +132 -0
- package/assets/skills/office-presentations/template_following_scripts/check_template_fidelity.py +176 -0
- package/assets/skills/office-presentations/template_following_scripts/inspect_template_deck.py +154 -0
- package/assets/skills/office-presentations/template_following_scripts/make_contact_sheet.py +111 -0
- package/assets/skills/office-presentations/template_following_scripts/prepare_template_starter_deck.py +239 -0
- package/assets/skills/office-presentations/template_following_scripts/validate_template_plan.py +185 -0
- package/assets/skills/office-spreadsheets/SKILL.md +105 -101
- package/assets/skills/office-spreadsheets/container_tools/add_chart.py +236 -0
- package/assets/skills/office-spreadsheets/container_tools/add_image.py +107 -0
- package/assets/skills/office-spreadsheets/container_tools/append_rows.py +122 -0
- package/assets/skills/office-spreadsheets/container_tools/diff_workbooks.py +306 -0
- package/assets/skills/office-spreadsheets/container_tools/inspect_workbook.py +398 -0
- package/assets/skills/office-spreadsheets/container_tools/recalc_gate.py +275 -0
- package/assets/skills/office-spreadsheets/container_tools/set_cells.py +135 -0
- package/assets/skills/office-spreadsheets/container_tools/sheet_ops.py +134 -0
- package/assets/skills/office-spreadsheets/container_tools/style_cells.py +133 -0
- package/assets/skills/office-spreadsheets/recipes/charts.md +66 -0
- package/assets/skills/office-spreadsheets/recipes/conditional-formats.md +69 -0
- package/assets/skills/office-spreadsheets/recipes/data-tables.md +58 -0
- package/assets/skills/office-spreadsheets/recipes/named-ranges.md +41 -0
- package/assets/skills/office-spreadsheets/recipes/number-formats.md +72 -0
- package/assets/skills/office-spreadsheets/recipes/sparklines.md +43 -0
- package/assets/skills/office-spreadsheets/references/charts.md +63 -0
- package/assets/skills/office-spreadsheets/references/domain/financial-models.md +40 -0
- package/assets/skills/office-spreadsheets/references/domain/healthcare.md +22 -0
- package/assets/skills/office-spreadsheets/references/domain/marketing.md +28 -0
- package/assets/skills/office-spreadsheets/references/domain/research.md +22 -0
- package/assets/skills/office-spreadsheets/references/formula-patterns.md +78 -0
- package/assets/skills/office-spreadsheets/references/style-guidelines.md +69 -0
- package/assets/skills/office-spreadsheets/references/workbook-structure.md +60 -0
- package/assets/skills/office-spreadsheets/routing/google-sheets.md +36 -0
- package/assets/skills/office-spreadsheets/tasks/analyze-workbook.md +37 -0
- package/assets/skills/office-spreadsheets/tasks/annotations.md +51 -0
- package/assets/skills/office-spreadsheets/tasks/create-workbook.md +66 -0
- package/assets/skills/office-spreadsheets/tasks/diff-workbooks.md +38 -0
- package/assets/skills/office-spreadsheets/tasks/edit-workbook.md +69 -0
- package/assets/skills/office-spreadsheets/tasks/inspect-workbook.md +38 -0
- package/assets/skills/office-spreadsheets/tasks/recalc-and-check.md +57 -0
- package/assets/skills/shared/annotation-recipes.md +252 -0
- package/assets/skills/shared/check_runtime.py +253 -0
- package/assets/skills/shared/common-workflow.md +139 -0
- package/assets/skills/shared/scripts/ensure_raster_image.py +187 -0
- package/dist/client.js +4 -4
- package/dist/client.js.map +1 -1
- package/dist/index.js +239 -89
- package/package.json +1 -1
- package/postinstall.mjs +92 -82
- package/python/office_cli.py +171 -246
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# Create or edit a document
|
|
2
|
+
|
|
3
|
+
**Goal.** Produce a `.docx` that opens cleanly in Word, with real heading styles, real lists and real tables, then change it later without destroying what is already there.
|
|
4
|
+
|
|
5
|
+
## Create
|
|
6
|
+
|
|
7
|
+
Write a JSON spec next to the output, then run:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
"<python>" "<cli>" docx create --spec "<spec.json>" --out "<out.docx>"
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```json
|
|
14
|
+
{
|
|
15
|
+
"title": "Regional revenue review",
|
|
16
|
+
"subtitle": "FY26 Q2",
|
|
17
|
+
"author": "Finance",
|
|
18
|
+
"header": "Internal — do not circulate",
|
|
19
|
+
"footer": "FY26 Q2 review",
|
|
20
|
+
"page_size": "Letter",
|
|
21
|
+
"orientation": "portrait",
|
|
22
|
+
"blocks": [
|
|
23
|
+
{ "type": "heading", "level": 1, "text": "Summary" },
|
|
24
|
+
{ "type": "paragraph", "text": "Revenue grew 18% year over year." },
|
|
25
|
+
{ "type": "bullets", "items": ["Renewals at 92%", "42 new accounts"] },
|
|
26
|
+
{ "type": "table", "headers": ["Region", "Revenue"], "rows": [["EMEA", "4.1M"], ["APAC", "3.2M"]] },
|
|
27
|
+
{ "type": "page_break" }
|
|
28
|
+
]
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Block types and keys: `heading` (`level`, `text`), `paragraph` (`text`, `bold`, `italic`, `size`, `color`, `align`, `style`), `bullets` / `numbered` (`items`), `table` (`headers`, `rows`, optional `style`, default `Table Grid`), `image` (`path`, optional `width` in inches), `equation` (`latex`), `quote`, `page_break`, `spacer`. Page keys: `page_size` (`Letter` or `A4`), `orientation`, and `top_margin` / `bottom_margin` / `left_margin` / `right_margin` in inches.
|
|
33
|
+
|
|
34
|
+
The command prints counts for headings, paragraphs, list items, tables, images and equations. Check `equation_fallbacks`: a non-zero value means some LaTeX was emitted as styled text instead of OMML.
|
|
35
|
+
|
|
36
|
+
## Edit
|
|
37
|
+
|
|
38
|
+
The helper CLI does not mutate documents. Every edit is a script that reads the original, writes a copy, and refuses to overwrite its input. Omit `--out` and the copy lands next to the source as `<name>-edited.docx`.
|
|
39
|
+
|
|
40
|
+
Replace text — body, tables, and every header and footer:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
"<python>" "<skill>/scripts/replace_text.py" --input "<original.docx>" --out "<original-edited.docx>" \
|
|
44
|
+
--find "18%" --replace "21%" --find "Hardware flat" --replace "Hardware steady"
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
`--find` / `--replace` are paired in order; `--spec edits.json` takes `{"replace": [{"find": "...", "replace": "..."}]}` instead. The command prints a per-pair count. A match inside one run keeps that run's formatting; a match that straddles runs collapses the paragraph into its first run, so keep needles inside a single styled span when mixed formatting matters. `--require-all` exits 1 if a pair matched nothing, which is the check you want for a scripted edit.
|
|
48
|
+
|
|
49
|
+
Append blocks — the same block types `docx create` accepts:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
"<python>" "<skill>/scripts/append_blocks.py" --input "<original.docx>" --out "<original-edited.docx>" --blocks "blocks.json"
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
```json
|
|
56
|
+
[{ "type": "heading", "level": 1, "text": "Appendix" }, { "type": "table", "headers": ["Region", "Revenue"], "rows": [["EMEA", "4.1M"]] }]
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
`--spec` takes a full document spec and appends its `blocks` array. The builder is the CLI's own, so anything that works in `docx create` works here, including images (`path`, `width`) and equations (`latex`).
|
|
60
|
+
|
|
61
|
+
Set properties:
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
"<python>" "<skill>/scripts/core_properties.py" --input "<original.docx>" --out "<original-edited.docx>" \
|
|
65
|
+
--title "Regional revenue review (final)" --author "Finance" --keywords "revenue, FY26"
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
`--show` reports the current properties without writing; the same names work in `--spec core.json` under a `core` object.
|
|
69
|
+
|
|
70
|
+
Three independent scripts means a pipeline writes an intermediate copy (append, then properties). That is deliberate: each step is re-runnable and its output is a valid document you can inspect.
|
|
71
|
+
|
|
72
|
+
## When the CLI is not enough
|
|
73
|
+
|
|
74
|
+
| Need | Route |
|
|
75
|
+
|---|---|
|
|
76
|
+
| Edit inside a header, footer, footnote or comment | `scripts/docx_kit.py xml <file> <part>` to inspect, then the snippet below or a purpose script |
|
|
77
|
+
| Mark edits as tracked changes | `scripts/add_tracked_replacements.py`, `tasks/clean_tracked_changes.md` |
|
|
78
|
+
| Header date, footer page numbers, first hyperlink | `scripts/docx_ooxml_patch.py` |
|
|
79
|
+
| Replace a paragraph but keep its formatting | `python-docx` snippet below |
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
"<python>" - <<'PY'
|
|
83
|
+
from docx import Document
|
|
84
|
+
d = Document(r"<file.docx>")
|
|
85
|
+
for p in d.paragraphs:
|
|
86
|
+
if "18%" in p.text:
|
|
87
|
+
runs = p.runs or [p.add_run()]
|
|
88
|
+
runs[0].text = p.text.replace("18%", "21%")
|
|
89
|
+
for r in runs[1:]:
|
|
90
|
+
r.text = ""
|
|
91
|
+
break
|
|
92
|
+
d.save(r"<file-edited.docx>")
|
|
93
|
+
PY
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Editing `w:t` text directly (via `docx_kit`) is safe for plain word swaps; anything that changes paragraph structure should go through `python-docx` so the surrounding XML stays consistent.
|
|
97
|
+
|
|
98
|
+
## Verify
|
|
99
|
+
|
|
100
|
+
`docx read` to confirm text and counts, then render and look at every page (`tasks/verify_render.md`). A spec that produces three pages instead of two means the table or margins need work, not that the renderer is wrong.
|
|
101
|
+
|
|
102
|
+
## Pitfalls
|
|
103
|
+
|
|
104
|
+
- Deleting and retyping a heading loses its outline level; edit the text, keep the style.
|
|
105
|
+
- `append_blocks.py` inserts before the final section properties, so a trailing landscape section or page setup keeps working.
|
|
106
|
+
- Replacing text that lives in a field result changes only the cached value; the field recalculates on open in Word.
|
|
107
|
+
- Copying content between documents by hand drops styles and relationships — use `scripts/merge_docx_append.py`.
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# Equations
|
|
2
|
+
|
|
3
|
+
**Goal.** Displayed mathematics that Word treats as maths: selectable, reflowable, editable — not a picture and not italic text.
|
|
4
|
+
|
|
5
|
+
Word stores native equations as OMML inside `m:oMathPara`/`m:oMath`. The helper CLI converts LaTeX to OMML on creation; a picture of an equation cannot be edited by the user and scales badly.
|
|
6
|
+
|
|
7
|
+
## Create
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
"<python>" "<cli>" docx create --spec "<spec.json>" --out "<out.docx>"
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```json
|
|
14
|
+
{
|
|
15
|
+
"blocks": [
|
|
16
|
+
{ "type": "heading", "level": 1, "text": "Model" },
|
|
17
|
+
{ "type": "equation", "latex": "E = mc^2" },
|
|
18
|
+
{ "type": "equation", "latex": "\\frac{a^2+b^2}{c^2}" }
|
|
19
|
+
]
|
|
20
|
+
}
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
The command reports `equations` and `equation_fallbacks`. Non-zero fallbacks mean some expression was emitted as italic text because the converter could not handle it — check the counter, then simplify the LaTeX (the converter covers fractions, superscripts, subscripts, roots, sums, Greek letters and common operators).
|
|
24
|
+
|
|
25
|
+
Displayed equations belong in their own paragraph; inline maths is ordinary runs plus OMML, which is harder to place and easy to break.
|
|
26
|
+
|
|
27
|
+
## Check what a file contains
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
"<python>" "<cli>" docx read --input "<file.docx>"
|
|
31
|
+
"<python>" "<skill>/scripts/docx_kit.py" xml '<file.docx>' word/document.xml | grep -c "oMath"
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
`oMath` in the XML is the honest test. Text that merely looks mathematical (italic Times, a slash) is not an equation and will reflow badly.
|
|
35
|
+
|
|
36
|
+
## When the converter cannot express it
|
|
37
|
+
|
|
38
|
+
Build the OMML by hand for the specific construct, or split the expression: a matrix or a piecewise definition is often clearer as a small table than as one enormous formula. If neither works, render the expression as an image with a caption and tell the user it is no longer editable.
|
|
39
|
+
|
|
40
|
+
The runtime ships the `latex2mathml` and `mathml2omml` packages, so a custom snippet can produce OMML directly:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
"<python>" - <<'PY'
|
|
44
|
+
from latex2mathml.converter import convert
|
|
45
|
+
from mathml2omml import convert as to_omml
|
|
46
|
+
mathml = convert(r"\sum_{i=1}^{n} x_i")
|
|
47
|
+
print(to_omml(mathml)[:200])
|
|
48
|
+
PY
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Verify
|
|
52
|
+
|
|
53
|
+
Render the page and look at the equation: fractions stacked, limits above and below the operator, Greek letters as glyphs not boxes, and the equation not clipped at the right margin. A fallback shows up as italic Latin text where a symbol should be.
|
|
54
|
+
|
|
55
|
+
## Pitfalls
|
|
56
|
+
|
|
57
|
+
- An equation inside a table cell or a caption is legal but almost always the wrong layout decision.
|
|
58
|
+
- Copying OMML between documents is safe; copying a picture of it is not.
|
|
59
|
+
- Numeric labels belong in the text (`(3)` right-aligned), not inside the equation object, if the user will renumber later.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Fields and field updates
|
|
2
|
+
|
|
3
|
+
**Goal.** Find out what a document computes, and decide whether to keep it live or freeze it.
|
|
4
|
+
|
|
5
|
+
A field is an instruction the reader's Word evaluates: `SEQ` numbers captions, `REF` repeats a bookmark, `PAGE`/`NUMPAGES` count pages, `TOC` builds the contents list, `DATE` prints today. Until Word recalculates it, the file shows a cached result — which is why a TOC in a freshly edited file can look stale.
|
|
6
|
+
|
|
7
|
+
## Inspect
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
"<python>" "<skill>/scripts/fields_report.py" "<file.docx>" --max-examples 5
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Reports the instruction, whether the field is a simple (`w:fldSimple`, one attribute) or complex field (begin/separate/end runs), and the cached result. `docx_kit.py xml` shows the raw runs when the report is not enough:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
"<python>" "<skill>/scripts/docx_kit.py" xml '<file.docx>' word/document.xml | grep -o 'instrText[^<]*' | head
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Freeze a field to its current value
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
"<python>" "<skill>/scripts/fields_materialize.py" "<file.docx>" --out "<file-static.docx>" --only SEQ REF
|
|
23
|
+
"<python>" "<skill>/scripts/flatten_ref_fields.py" "<file.docx>" --out "<file-static.docx>"
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Materialising replaces the field span with one run holding the cached text, keeping the run formatting. `flatten_ref_fields.py` is the narrow version for `REF` and `PAGEREF` only.
|
|
27
|
+
|
|
28
|
+
Freeze when: the document is going to a reader who must see exactly this number, the target bookmark is being deleted, or a downstream tool mangles fields. Keep it live when the document will be edited again — a frozen caption number stops following its table.
|
|
29
|
+
|
|
30
|
+
## Keep it live
|
|
31
|
+
|
|
32
|
+
- Word recalculates on open when `w:updateFields val="true"` is set in `word/settings.xml`. `insert_toc.py` sets it for a new TOC; if you need it for an existing file, add the element yourself and tell the user the first open will prompt.
|
|
33
|
+
- LibreOffice and the renderer also refresh fields; a stale cached value in a preview usually means the field was never recalculated, not that the field is broken.
|
|
34
|
+
- `insert_ref_fields.py` converts `[[REF:bookmark]]` placeholders into real `REF` fields, which is the reliable way to build cross-references after restructuring.
|
|
35
|
+
|
|
36
|
+
## Verify
|
|
37
|
+
|
|
38
|
+
`fields_report.py` after the change: frozen fields disappear from the report, live ones keep their instruction. Render and read the page to confirm the number the reader sees.
|
|
39
|
+
|
|
40
|
+
## Pitfalls
|
|
41
|
+
|
|
42
|
+
- A field split across paragraphs is invalid; keep begin/separate/end together.
|
|
43
|
+
- `w:fldSimple` and complex fields both appear in the report — do not assume the absence of `w:fldSimple` means there are no fields.
|
|
44
|
+
- Copying a field to another document copies the instruction but not the bookmark it refers to; recreate it there.
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# Fixtures and edge cases
|
|
2
|
+
|
|
3
|
+
**Goal.** Have a file that contains the awkward cases *before* you are asked to handle them, so a fix is tested against a 1600-word paragraph and not only against clean prose.
|
|
4
|
+
|
|
5
|
+
## Build the fixture set
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
"<python>" "<skill>/scripts/make_fixtures.py" --outdir "<work-dir>" --only all
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Writes:
|
|
12
|
+
|
|
13
|
+
| File | Contains |
|
|
14
|
+
|---|---|
|
|
15
|
+
| `tracked.docx` | A replacement as real `w:del`+`w:ins` markup, tracking switched on |
|
|
16
|
+
| `watermark.docx` | A DRAFT watermark in the header |
|
|
17
|
+
| `comments.docx` | One anchored comment in `word/comments.xml` |
|
|
18
|
+
| `edge_cases.docx` | Empty paragraph, H1 to H3 jump, 1600-word paragraph, email and phone, 2×10 unstyled table, image with no alt text |
|
|
19
|
+
|
|
20
|
+
`--only tracked|watermark|comments|edge` builds one.
|
|
21
|
+
|
|
22
|
+
These are inputs for testing a change, not templates. Do not ship them.
|
|
23
|
+
|
|
24
|
+
## The cases that break naive edits
|
|
25
|
+
|
|
26
|
+
- **Long paragraphs** — a replacement that rebuilds the paragraph loses run formatting across thousands of characters; check the result with `read_image`, not only with a text dump.
|
|
27
|
+
- **Nested tables** — text inside `table > cell > table > cell` is missed by `document.paragraphs`; walk `cell.tables` or use the pack scripts, which do.
|
|
28
|
+
- **Header/footer text** — a find-and-replace over the body never sees it.
|
|
29
|
+
- **Tables without a style** — borders come from the renderer default and change when the file is opened elsewhere; apply `Table Grid` or set explicit `#D9D9D9` borders.
|
|
30
|
+
- **Images with no alt text** — an accessibility finding, and also a sign the image was pasted rather than placed.
|
|
31
|
+
- **Manual spacing** — a document padded with empty paragraphs and spaces looks right in one renderer and wrong in the next; normalize instead (`tasks/style_lint_normalize.md`).
|
|
32
|
+
- **Mixed page sizes** — a landscape section inside a portrait document confuses footers and headers; `section_audit.py` reports it.
|
|
33
|
+
|
|
34
|
+
## Regression pass before delivery
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
for f in "<work-dir>"/*.docx; do
|
|
38
|
+
echo "== $f"
|
|
39
|
+
"<python>" "<skill>/scripts/heading_audit.py" "$f" || true
|
|
40
|
+
"<python>" "<skill>/scripts/style_lint.py" "$f" || true
|
|
41
|
+
"<python>" "<skill>/scripts/a11y_audit.py" "$f" || true
|
|
42
|
+
"<python>" "<skill>/scripts/section_audit.py" "$f" || true
|
|
43
|
+
done
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
An audit that now reports nothing where it previously reported a real problem is the signal that a fix regressed. Keep the fixture out of the user's workspace; put it under the temp directory.
|
|
47
|
+
|
|
48
|
+
## Verify
|
|
49
|
+
|
|
50
|
+
Whatever the script produced, render it. Fixtures exist to be looked at: a 1600-word paragraph and a table that is too wide both fail visually first.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Footnotes and endnotes
|
|
2
|
+
|
|
3
|
+
**Goal.** Attach a reference note at exactly one place in the text, and keep the note body in the notes part rather than in the body.
|
|
4
|
+
|
|
5
|
+
Notes live outside the flow: `word/footnotes.xml` (or `word/endnotes.xml`) holds the text, and a `w:footnoteReference w:id="N"` run in the body points at it. The part also needs the separator notes (ids -1 and 0) or Word repairs the file.
|
|
6
|
+
|
|
7
|
+
## Insert a note
|
|
8
|
+
|
|
9
|
+
Put a marker where the reference belongs, then convert it:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
"<python>" "<skill>/scripts/insert_note.py" "<file.docx>" --kind footnote --text "Source: ledger extract, June." --out "<file-notes.docx>"
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
The marker defaults to `[[NOTE]]`; change it with `--marker`. The script creates the notes part (with separator and continuation-separator entries) if it does not exist, allocates the next free id, splits the surrounding run so only the marker is replaced by the reference, and styles the note text with `FootnoteText` and the reference with `FootnoteReference`. `--kind endnote` writes to the endnotes part instead.
|
|
16
|
+
|
|
17
|
+
## Report
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
"<python>" "<skill>/scripts/footnotes_report.py" "<file.docx>"
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Lists each note's text, how many references point at it, and which references dangle or which notes are orphaned. Orphans appear when content is copied between documents; dangling references appear when a note is deleted but its marker stays.
|
|
24
|
+
|
|
25
|
+
## Renumbering
|
|
26
|
+
|
|
27
|
+
Note ids are numbers, but Word displays them sequentially by position, so inserting a note in the middle renumbers the rest automatically. Do not try to keep ids in display order — keep them unique.
|
|
28
|
+
|
|
29
|
+
## Verify
|
|
30
|
+
|
|
31
|
+
Render the page with the note and read the note area: the marker should sit immediately after the word it annotates, with no space before it, and the note text should match. Also check the last page — an endnote section that lands after the signature block is a layout defect.
|
|
32
|
+
|
|
33
|
+
## Pitfalls
|
|
34
|
+
|
|
35
|
+
- A footnote reference inside a table cell is allowed; inside a header it is not.
|
|
36
|
+
- Copying a paragraph with `python-docx` copies the reference but not the note body — recreate the note in the target document.
|
|
37
|
+
- Deleting note text without deleting the reference leaves a broken document that opens with a repair prompt.
|
|
38
|
+
- Field instructions are not allowed in the note separator; keep the built-in separator notes untouched.
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# Forms and content controls
|
|
2
|
+
|
|
3
|
+
**Goal.** Fill or create a structured field — a name, a date, a choice — as a real content control, so the value is labelled in the XML instead of being plain text between two labels.
|
|
4
|
+
|
|
5
|
+
A content control is `w:sdt` with `w:sdtPr` (tag, alias, id, type) and `w:sdtContent`. The tag is the machine name; the alias is what Word shows in the properties panel.
|
|
6
|
+
|
|
7
|
+
## List what exists
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
"<python>" "<skill>/scripts/content_controls.py" list "<file.docx>"
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Reports each control's part, tag, alias, id, type and current text, across the body, headers and footers. `--no_headers_footers` limits it to the body.
|
|
14
|
+
|
|
15
|
+
## Create
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
"<python>" "<skill>/scripts/content_controls.py" create "<file.docx>" \
|
|
19
|
+
--placeholder "Client name" --tag client_name --alias "Client name" --out "<file-form.docx>"
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
The script wraps the paragraph containing the placeholder text (or appends a new one) in a `w:sdt`, so the field is a drop-in replacement for a typed placeholder line. `--json` prints the created control instead of a summary.
|
|
23
|
+
|
|
24
|
+
## Fill
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
"<python>" "<skill>/scripts/content_controls.py" set "<file-form.docx>" \
|
|
28
|
+
--set client_name="Northwind" --set period="Q2 FY26" --out "<file-filled.docx>"
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
`--set TAG=VALUE` matches on the tag. The command exits 1 and lists `missing_tags` when a tag is not present — that is the check for a scripted fill, so a typo does not silently write nothing.
|
|
32
|
+
|
|
33
|
+
## Design
|
|
34
|
+
|
|
35
|
+
- One control per value; do not put a whole table into one control.
|
|
36
|
+
- Tag in `snake_case`, alias in the user's words.
|
|
37
|
+
- A choice control lists its options in `w:sdtPr`; a date control has `w:date` with a format. Plain text is the default and the safest thing to generate.
|
|
38
|
+
- Leave the surrounding labels in the body text; the alias is visible only in Word's properties panel.
|
|
39
|
+
|
|
40
|
+
## Verify
|
|
41
|
+
|
|
42
|
+
`content_controls.py list` after filling: tags present with the expected text. Then render — a control's content is normal text on the page, so the visual check is that nothing shifted and the filled value fits its line.
|
|
43
|
+
|
|
44
|
+
## Pitfalls
|
|
45
|
+
|
|
46
|
+
- Deleting an `sdt` wrapper while trying to "clear" a field deletes the value and the structure; set the text instead.
|
|
47
|
+
- Two controls with the same tag make `--set` ambiguous; the script updates the first and reports the rest.
|
|
48
|
+
- A content control cannot span two paragraphs without `w:sdtContent` holding both — keep one value per paragraph when generating.
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# Headings and numbering
|
|
2
|
+
|
|
3
|
+
**Goal.** A single outline a reader can follow and Word can turn into a TOC: no skipped levels, no headings faked with bold text, consistent spacing.
|
|
4
|
+
|
|
5
|
+
Heading level comes from the paragraph style (`Heading 1`…`Heading 9`, `Title` uses outline level 0), not from size or weight. `heading_audit.py` reads that structure and flags the common breakages.
|
|
6
|
+
|
|
7
|
+
## Audit
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
"<python>" "<skill>/scripts/heading_audit.py" "<file.docx>" --max_findings 20
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Findings:
|
|
14
|
+
|
|
15
|
+
- `skipped_level` — a level 3 heading directly under a level 1. Fix by inserting the missing level or by promoting the deeper heading.
|
|
16
|
+
- `empty_heading` — a heading with no text; a stale outline entry that will appear in the TOC as a blank line.
|
|
17
|
+
- `manual_formatting` — a real heading that also carries direct bold or a large font size; the style already does that, so the extra formatting fights restyling.
|
|
18
|
+
- `heading_not_styled` — body text that looks like a heading (bold, 13pt+) but is styled `Normal`. This is the expensive one: the TOC misses it and every later style change misses it.
|
|
19
|
+
|
|
20
|
+
## Fix
|
|
21
|
+
|
|
22
|
+
Prefer styles over direct formatting. `style_normalize.py --clear_paragraph_format --enforce_heading_spacing` strips direct run formatting and sets standard space-before/after on headings:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
"<python>" "<skill>/scripts/style_normalize.py" "<file.docx>" --out "<file-fixed.docx>" --clear_paragraph_format --enforce_heading_spacing
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
For a heading that is only bold text, promote it to the right style rather than making it bolder:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
"<python>" - <<'PY'
|
|
32
|
+
from docx import Document
|
|
33
|
+
d = Document(r"<file.docx>")
|
|
34
|
+
for p in d.paragraphs:
|
|
35
|
+
if p.text.strip() == "<exact heading text>":
|
|
36
|
+
p.style = d.styles["Heading 1"]
|
|
37
|
+
d.save(r"<file-fixed.docx>")
|
|
38
|
+
PY
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Numbered headings
|
|
42
|
+
|
|
43
|
+
Numbering is a numbering definition attached to the heading style, not text like `1.2`. Two consequences: a typed number keeps lying after an insert, and a numbered outline needs `numPr` on the style with an `abstractNum` that uses `%1.%2` level text. When a document must have numbered sections and the original numbering is lost, restore the style's `numPr` rather than typing numbers into the headings.
|
|
44
|
+
|
|
45
|
+
Lists are the same machinery at body level: `List Number` restarts only when the numbering definition says so, which is why two separate numbered lists sometimes continue each other.
|
|
46
|
+
|
|
47
|
+
## Verify
|
|
48
|
+
|
|
49
|
+
`heading_audit.py` should exit 0, then render and read the page: heading hierarchy visible, no orphaned heading at the bottom of a page (`keepNext`), and the same spacing before every heading of a level.
|
|
50
|
+
|
|
51
|
+
## Pitfalls
|
|
52
|
+
|
|
53
|
+
- Changing a heading's style does not change its text, but it can change its size and colour — re-render.
|
|
54
|
+
- A heading inside a table cell still counts for the outline, which is rarely wanted; use a bold paragraph there.
|
|
55
|
+
- Copying a heading from another document brings a direct-formatting payload with it; run the audit afterwards.
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Images and figures
|
|
2
|
+
|
|
3
|
+
**Goal.** Pictures that are described for screen readers, sized for the page, and captioned consistently.
|
|
4
|
+
|
|
5
|
+
An inline picture is a `w:drawing` containing `wp:inline` (or `wp:anchor` for a floating one), with the picture relationship in `a:blip r:embed` and the display size in `wp:extent` (EMU). Alt text lives in `wp:docPr @descr`.
|
|
6
|
+
|
|
7
|
+
## Audit
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
"<python>" "<skill>/scripts/images_audit.py" "<file.docx>" --max_rows 20
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
For every image: the part it sits in, its name, the media file behind it, its size in inches, and the alt text. Findings are `missing_alt` and `oversized` (wider than the text block). The workable check is "does the alt text describe what a reader loses if the picture is gone".
|
|
14
|
+
|
|
15
|
+
## Fix alt text
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
"<python>" "<skill>/scripts/a11y_audit.py" "<file.docx>" --fix_image_alt from_filename --out "<file-fixed.docx>"
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
That writes the file name when no description exists — better than nothing, not as good as real text. For real descriptions, set `descr` yourself; decorative images get an empty description and `title`, while informative images get a sentence:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
"<python>" - <<'PY'
|
|
25
|
+
import sys
|
|
26
|
+
from pathlib import Path
|
|
27
|
+
sys.path.insert(0, r"<skill>/scripts")
|
|
28
|
+
import docx_kit as kit
|
|
29
|
+
d = kit.Docx(r"<file.docx>")
|
|
30
|
+
for doc_pr in d.xml("word/document.xml").iter(kit.q("docPr", kit.WP)):
|
|
31
|
+
if doc_pr.get("name") == "Picture 3":
|
|
32
|
+
doc_pr.set("descr", "Bar chart: revenue by region, EMEA ahead of APAC.")
|
|
33
|
+
out = Path(r"<file-fixed.docx>")
|
|
34
|
+
d.save(out)
|
|
35
|
+
print("saved", out)
|
|
36
|
+
PY
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Insert
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
"<python>" "<cli>" docx create --spec "<spec.json>" --out "<out.docx>"
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```json
|
|
46
|
+
{ "type": "image", "path": "<absolute.png>", "width": 5.5 }
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Width is in inches; the height follows the aspect ratio. An image wider than the text block gets clipped or shrinks unpredictably — 5.5in is a safe full-width figure on Letter with 1in margins.
|
|
50
|
+
|
|
51
|
+
## Figures
|
|
52
|
+
|
|
53
|
+
Caption a picture the same way as a table (`tasks/captions_crossrefs.md`): `captions_and_crossrefs.py --figures` adds `Figure N` with a `SEQ` field and a `figN` bookmark. Keep the caption in the same block so the figure and its caption do not separate across a page break.
|
|
54
|
+
|
|
55
|
+
## Verify
|
|
56
|
+
|
|
57
|
+
Render and read the page: the image is not stretched, not blurry beyond the source resolution, not clipped at the margin, and its caption sits under it. Add alt text as a separate check — the render cannot show it.
|
|
58
|
+
|
|
59
|
+
## Pitfalls
|
|
60
|
+
|
|
61
|
+
- Cropping in Word is not lossy for the file but changes the displayed extent; alt text and size checks still apply.
|
|
62
|
+
- A floating image (`wp:anchor`) can overlap text at any zoom level; prefer inline for documents that will be edited.
|
|
63
|
+
- Replacing an image file in `word/media` in place keeps the relationship but not the dimensions — the extent is stored separately.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Merge documents
|
|
2
|
+
|
|
3
|
+
**Goal.** Put one document after another and keep both looking the same — styles, numbering, images, hyperlinks — instead of pasting content that loses half of it.
|
|
4
|
+
|
|
5
|
+
Concatenating packages by hand fails on the details: style ids collide, `numId` values point at the wrong list, media parts need new names and relationships, and `r:id` values are document-scoped. Word also requires one `sectPr` at the end of the body, so the appended content has to land before it.
|
|
6
|
+
|
|
7
|
+
## Append
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
"<python>" "<skill>/scripts/merge_docx_append.py" "<base.docx>" "<addition.docx>" --out "<merged.docx>"
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
What it carries across: body content (minus the addition's final `sectPr`), media parts renamed under `word/media` with fresh relationships and content types, style definitions referenced by the copied content (following `basedOn`/`next`/`link`), numbering definitions rebuilt with fresh ids and every `w:numId` rewritten, and hyperlink relationships recreated as external relationships.
|
|
14
|
+
|
|
15
|
+
Drawings are refused by default: a floating image or a text box carries anchor geometry tied to the original page. Pass `--allow_drawings` when the user accepts that the layout may shift, then check that page carefully.
|
|
16
|
+
|
|
17
|
+
## Merge into a template
|
|
18
|
+
|
|
19
|
+
The usual job is "put these sections into the house template". Apply the template first, then append, so the appended content inherits the template's style ids:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
"<python>" "<skill>/scripts/apply_template_styles.py" --template "<house.dotx>" --target "<draft.docx>" --out "<draft-styled.docx>"
|
|
23
|
+
"<python>" "<skill>/scripts/merge_docx_append.py" "<draft-styled.docx>" "<section2.docx>" --out "<merged.docx>"
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Then normalize direct formatting so the merged content matches:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
"<python>" "<skill>/scripts/style_normalize.py" "<merged.docx>" --out "<merged-final.docx>" --clear_paragraph_format
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Verify
|
|
33
|
+
|
|
34
|
+
- Page count should equal the sum of the inputs, unless a page break changed.
|
|
35
|
+
- Headers and footers come from the base document; say so if the addition had its own.
|
|
36
|
+
- Render and read the join: the page where the second document starts often shows a stray page break or a heading that lost its style.
|
|
37
|
+
- `style_lint.py` on the merged file catches undefined style references imported from the addition.
|
|
38
|
+
|
|
39
|
+
## Pitfalls
|
|
40
|
+
|
|
41
|
+
- Footnotes and comments in the addition are not merged; port them deliberately if they must survive.
|
|
42
|
+
- `w:id` values for bookmarks and comments can collide across documents; the merge keeps ids and names as they were.
|
|
43
|
+
- A landscape section in the addition is not carried over — section properties were dropped with the `sectPr`.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Internal navigation
|
|
2
|
+
|
|
3
|
+
**Goal.** Let a reader jump around a long document — from a contents list to a section, and back — without relying on the TOC field being recalculated.
|
|
4
|
+
|
|
5
|
+
A hyperlink to a place inside the document is `w:hyperlink w:anchor="bookmark"`; the target is a `w:bookmarkStart`/`w:bookmarkEnd` pair with that name. Both live in the body, so an export to PDF keeps the links.
|
|
6
|
+
|
|
7
|
+
## Build a navigation set
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
"<python>" "<skill>/scripts/internal_nav.py" "<file.docx>" --out "<file-nav.docx>" --levels 1 2 \
|
|
11
|
+
--toc_title "Contents"
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
What it does, in order:
|
|
15
|
+
|
|
16
|
+
1. Bookmarks every heading as `nav_h1`, `nav_h2`, … using its outline level.
|
|
17
|
+
2. Inserts a `Contents` heading and a quick-link bar (one link per heading) at the top.
|
|
18
|
+
3. Indents each contents entry by 360 twips per level so the hierarchy is visible.
|
|
19
|
+
4. Appends a small `Back to contents` link after each heading.
|
|
20
|
+
5. Bookmarks the top, the contents list and the bottom (`nav_top`, `nav_toc`, `nav_bottom`).
|
|
21
|
+
|
|
22
|
+
Flags: `--no_quicklinks` skips the horizontal bar, `--no_back_to_toc` skips the per-heading back link, `--no_top_bottom` skips the two edge targets.
|
|
23
|
+
|
|
24
|
+
The script reports `missing` when a link target could not be created — read that list, because a link without a bookmark is dead in Word and looks fine in a render.
|
|
25
|
+
|
|
26
|
+
## Static versus field-driven
|
|
27
|
+
|
|
28
|
+
`internal_nav.py` writes literal entries: it costs a rebuild when headings change, and it always looks the same in every renderer. `insert_toc.py` writes a live `TOC` field: it updates itself but needs a recalculation before it shows anything useful. A document that will be edited repeatedly wants the field; a document going out as a PDF export wants the static list.
|
|
29
|
+
|
|
30
|
+
## Verify
|
|
31
|
+
|
|
32
|
+
Render, then click through the pages you can see: entry indentation, that the contents heading is styled as a heading, and that `Back to contents` did not push a paragraph onto a new page. Dead anchors do not show in a screenshot — check `bookmarkStart` names against the `w:anchor` values when the script reports a miss.
|
|
33
|
+
|
|
34
|
+
## Pitfalls
|
|
35
|
+
|
|
36
|
+
- Bookmarks must be unique per document; re-running the script on its own output creates `nav_h1` twice, so start from the original.
|
|
37
|
+
- A hyperlink anchor is case-sensitive.
|
|
38
|
+
- Link styling comes from the `Hyperlink` character style; if links look like body text, the style is missing, not the link.
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Privacy: metadata and residue
|
|
2
|
+
|
|
3
|
+
**Goal.** A file that carries only the content the user meant to send: no author name, no tracked review history, no comments, no template identity, no editing session ids.
|
|
4
|
+
|
|
5
|
+
`.docx` stores more than the text: `docProps/core.xml` (author, last modifier, title, keywords, description), `docProps/app.xml` (company, manager, template), `docProps/custom.xml`, `word/comments.xml`, revision data and per-author `w:rsid` attributes.
|
|
6
|
+
|
|
7
|
+
## Scrub metadata
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
"<python>" "<skill>/scripts/privacy_scrub.py" "<file.docx>" --out "<file-clean.docx>"
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Clears creator, last-modified-by, keywords, description, subject, title, category, company, manager and application; resets the revision counter; drops `docProps/custom.xml` and its relationships and content-type override; strips `w:rsid*` attributes, `w:proofErr` and `w:lastRenderedPageBreak` across the Word parts.
|
|
14
|
+
|
|
15
|
+
`--keep_title` leaves the title in place when the title is real content the user wants.
|
|
16
|
+
|
|
17
|
+
## The rest of the residue
|
|
18
|
+
|
|
19
|
+
| Residue | Command |
|
|
20
|
+
|---|---|
|
|
21
|
+
| Comments and their authors | `comments_strip.py` |
|
|
22
|
+
| Tracked changes and reviewer names | `accept_tracked_changes.py --mode accept` (or reject) |
|
|
23
|
+
| Watermarks naming the old client | `watermark_audit_remove.py --mode remove` |
|
|
24
|
+
| Emails and phone numbers in the text | `redact_docx.py` |
|
|
25
|
+
|
|
26
|
+
Run them in that order — scrub metadata, resolve markup, then redact the text that survived.
|
|
27
|
+
|
|
28
|
+
## Check before delivery
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
"<python>" "<skill>/scripts/docx_kit.py" parts '<file-clean.docx>'
|
|
32
|
+
"<python>" "<skill>/scripts/docx_kit.py" xml '<file-clean.docx>' docProps/core.xml
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
`parts` shows whether `docProps/custom.xml` or `word/comments.xml` still exist; the core part shows what a recipient would see in File > Info. Both are cheap and both catch the mistake the scripts did not.
|
|
36
|
+
|
|
37
|
+
## Verify
|
|
38
|
+
|
|
39
|
+
Metadata is not visible in a page render. Verify with the commands above, and render to be sure the document still looks right after the scrub — clearing `w:lastRenderedPageBreak` can change where a viewer chooses to break a long line.
|
|
40
|
+
|
|
41
|
+
## Pitfalls
|
|
42
|
+
|
|
43
|
+
- Author names also appear in comment and revision entries, not only in core properties.
|
|
44
|
+
- `docProps/app.xml` names the editing application; it is regenerated on next save by whatever app opens the file.
|
|
45
|
+
- Removing revision ids does not undo content — the text is still there.
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# Restrict editing
|
|
2
|
+
|
|
3
|
+
**Goal.** Stop a reviewer from changing the body while still letting them comment or track changes — the "send for review" state, not encryption.
|
|
4
|
+
|
|
5
|
+
Editing restrictions live in `w:documentProtection` inside `word/settings.xml`. The modes are `readOnly`, `comments`, `trackedChanges`, `forms`, plus `enforcement` to switch the restriction on. This is a guardrail against accidental edits; it does not encrypt content and a determined reader can remove it.
|
|
6
|
+
|
|
7
|
+
## Set a mode
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
"<python>" "<skill>/scripts/set_protection.py" "<file.docx>" --mode comments --out "<file-review.docx>"
|
|
11
|
+
"<python>" "<skill>/scripts/set_protection.py" "<file.docx>" --mode readOnly --no_enforce --out "<file-open.docx>"
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Modes and aliases: `off`/`none`, `readOnly`/`read_only`, `comments`/`comment`, `trackedChanges`/`tracked_changes`, `forms`/`form`. `--no_enforce` writes the setting without switching it on, which is useful when the opening dialog should be avoided.
|
|
15
|
+
|
|
16
|
+
`--hash` also writes a password hash (SHA-512, 100000 spins; `--salt`, `--spin_count` to change it). Be honest with the user: the hash deters casual edits, it is not encryption, and losing the password means the restriction has to be stripped from the XML.
|
|
17
|
+
|
|
18
|
+
## Which mode to pick
|
|
19
|
+
|
|
20
|
+
| Intent | Mode |
|
|
21
|
+
|---|---|
|
|
22
|
+
| Review comments only, no text edits | `comments` |
|
|
23
|
+
| All edits should be tracked | `trackedChanges` |
|
|
24
|
+
| Fill a form, nothing else | `forms` |
|
|
25
|
+
| View only | `readOnly` |
|
|
26
|
+
|
|
27
|
+
## Remove
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
"<python>" "<skill>/scripts/set_protection.py" "<file-protected.docx>" --mode off --out "<file-open.docx>"
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Verify
|
|
34
|
+
|
|
35
|
+
`docx_kit.py xml '<file.docx>' word/settings.xml | grep documentProtection` shows the element and its enforcement attribute. Then open behaviour matters, not the page — render will not show a restriction. Say which mode you applied, and that removing it needs the same script or Word's Review pane.
|
|
36
|
+
|
|
37
|
+
## Pitfalls
|
|
38
|
+
|
|
39
|
+
- Protected documents that also contain tracked changes and comments often prompt on open; apply the restriction last.
|
|
40
|
+
- `forms` mode without form fields leaves a document nobody can edit usefully.
|
|
41
|
+
- Protection marked with a hash cannot be turned off in Word without the password, but `--mode off` still clears it — that is why it is a guardrail, not security.
|