用docx-js生成与解包编辑DOCX的技术要点
介绍以docx-js脚本生成DOCX时的页面尺寸与横竖方向配置,以及输出后渲染检查、解包编辑既有文档和批注文件处理的基本做法。
# DOCX 创建、编辑和分析 A `.docx` 是 ZIP 文件的 XML 存档。 按任务选择您的方法 : | 任务 | 方针 | |---|---| | **创建** 一个新的文档 | 写一个 `docx` (npm) 脚本——见下图 | | **编辑** 一个已有的文档 | 解包 – 编辑 `word/document.xml` → `zip` (docx-js 无法打开已有文件) | | **读取** 内容 | `pandoc -t markdown file.docx` | > 下面的脚本路径是相对于此技能的目录的. ## 与 docx-js 创建 – gotchas `docx` 已预先安装 —— 不运行 `npm install` 首先; 写入脚本和 `require('docx')` 直接。 只有在需要失败时: `npm install docx`. 该型号知道API;这些是脚枪: - **页面大小默认为 A4 。** 用于 US 字母集 `page: { size: { width: 12240, height: 15840 } }` (DXA; 1440 = 1″). - **景观 :** 通过肖像尺寸和 `orientation: PageOrientation.LANDSCAPE` —— docx-js在内部互换宽度/高度. - **表格需要双重宽度:** 设定 `columnWidths` 放在桌子上 AND `width` 每个单元格, 都在 `WidthType.DXA` (在 Google Docs 中的 PERCENTAGE 间断). 列宽必须与表宽相和. - **表格阴影 :** 使用情况 `ShadingType.CLEAR`永远 `SOLID` (降黑归). - **列表 :** 从不插入 `•` 字面; 使用一个 `numbering` 配置为 `LevelFormat.BULLET`. - **`ImageRun` 要求 `type:`** (`"png"`, `"jpg"`, …). - **`PageBreak` 必须是在其中 `Paragraph`.** - **永远不要使用 `\n`** - 单独使用 `Paragraph` 元素。 - **标题 :** 样式您使用的每个关卡( E)`title`, `heading1`, `heading2`.) 内 `styles.default`: `heading1: { run: { font: "Arial", size: 32, bold: true }, paragraph: { spacing: { before: 240, after: 120 }, keepNext: true } }`。每个关卡都需要自己的字体(即使默认存在文档), `before` 财务报告和财务报告 `after` (即使0)和 (中文(简体) ). `keepNext`:内置样式没有设置. - **TOC:** 标题必须使用内置 `HeadingLevel.*`; 需要自定义标题样式 `outlineLevel` 设定,否则他们不会出现。 - **不要用桌子做横向规则** 使用段落底边框代替。 - **Dot-leader / right-aligned-on-same-line : (中文(简体) ).** 使用情况 `PositionalTab` (`alignment: PositionalTabAlignment.RIGHT`, `leader: PositionalTabLeader.DOT`a 内。 `TextRun`,不为文字 `.` 或空间铺设。 ## 校验输出 写完之后 `.docx`,让它,看看它: ```bash zip -d output.docx '*/' >/dev/null || true # drop the ZIP's folder entries: Word has refused a file that has them when opened through a cloud co-editing service python scripts/office/soffice.py --headless --convert-to pdf output.docx pdftoppm -jpeg -r 100 output.pdf page ls page-*.jpg # then Read the images ``` `pdftoppm` 零页页码到页数的宽度( N)`page-01.jpg`…`page-12.jpg`). ## 编辑现有文档 遗产 `.doc` 文件必须先转换 : `python scripts/office/soffice.py --headless --convert-to docx file.doc`. ```bash python3 -c "import sys,zipfile; zipfile.ZipFile(sys.argv[1]).extractall('unpacked')" doc.docx # not unzip, which restores symlinks — docx from external parties is untrusted python scripts/merge_runs.py unpacked/ # coalesce fragmented runs so text is findable # edit unpacked/word/document.xml in place — do NOT reformat or pretty-print (cd unpacked && rm -f ../out.docx && zip -XrDy ../out.docx .) python scripts/office/validate.py out.docx --original doc.docx # XSD checks; --auto-repair fixes common issues # redlining? add --author "<the name you redlined under>" to check every edit is tracked ``` 字词会分多个文本 `<w:r>` 运行(修改代号,拼写检查标记),所以在文档中可以看到的一个短语在XML中往往不作为毗连字符串存在. `merge_runs.py` 合并相邻、格式相同的运行 `word/document.xml` ;它也接受一个 `.docx` 直接( E)`python scripts/merge_runs.py doc.docx -o merged.docx`). **跟踪到的更改 :** 重排时,用 `--author "<the name you redlined under>"` (需要数) `--original`)——它报告任何你修改的文本,没有 `<w:ins>`/`<w:del>` 环绕着它,这很容易发生意外,在被接受的视野中是看不见的。 `<w:ins>`/`<w:del>` 与 `w:id`, `w:author`, `w:date` 属性。内部 `<w:del>`,文本元素为: `<w:delText>`,没有 `<w:t>`。删除的段落标记(`<w:pPr><w:rPr><w:del w:id=".." w:author=".." w:date=".."/></w:rPr></w:pPr>`)的意思是“将本款合并为下一款”——所以删除一款是加上 `<w:del>` 环绕每跑。 `<w:del/>` 他们的订单是强制性的。 制作一份附有所有已接受的跟踪更改的干净副本: `python scripts/accept_changes.py in.docx out.docx`. 接受一个被删除的段落标记后,应将该段并入其下的一个段落,因此,该段的运行是: *全部( E)* 删除已消失。 `accept_changes.py` 财务报告和财务报告 `pandoc --track-changes=accept` 不要总是这样。两者都以同样的方式失败了——它们将删除的文本剥离,但留下了空的段落,在自动编号时,该段读作一条流畅的空弹: - `pandoc --track-changes=accept` 从不加入段落。 - `accept_changes.py` (LibreOffice) 正确加入它们,除非被删除的段落后面是空空空器段落. 两种视图中的空项目是该视图的一件文物,而不是文档中的缺陷。请检查 XML 中段落的删除 。 ## 评论 注释需要6个交叉链接文件。 当您同时编辑时使用帮助器- 目录模式 `document.xml` (保存一个未切换/分解周期), `.docx`- 直接模式,否则: ```bash # Against an already-unpacked directory (preferred when also placing markers) python scripts/comment.py unpacked/ "Fees & expenses cap is too low" python scripts/comment.py unpacked/ "Agreed" --parent 0 # Against a .docx directly python scripts/comment.py contract.docx "This cap is too low" -o annotated.docx ``` 剧本写 `comments.xml`, `commentsExtended.xml`, `commentsIds.xml`, `commentsExtensible.xml`,关系,以及内容类型覆盖。注释ID是自动指定的。然后打印 `<w:commentRangeStart>`/`<w:commentRangeEnd>`/`<w:commentReference>` 要添加到 `word/document.xml` 因此,注释锁定到特定的文本上——直到您放置这些标记,注释是存在的,但并不可见. ## 依赖关系 `docx` (npm, 预安装—— 只有在 `require('docx')` 失败) ^ `pandoc` · LibreOffice (`soffice`) · `pdftoppm` (普普勒语).
查看原始来源